Skip to content

Commit

Permalink
Support for Stringable (PHP 8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 27, 2020
1 parent 38fe693 commit 3143833
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"nette/utils": "^3.1.3",
"nikic/php-parser": "4.10.2",
"ondram/ci-detector": "^3.4.0",
"ondrejmirtes/better-reflection": "4.3.39",
"ondrejmirtes/better-reflection": "4.3.40",
"phpdocumentor/reflection-docblock": "4.3.4",
"phpstan/php-8-stubs": "^0.1.6",
"phpstan/phpdoc-parser": "^0.4.9",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Broker\Broker;
use PHPStan\Command\CommandHelper;
use PHPStan\File\FileHelper;
use PHPStan\Php\PhpVersion;
use Roave\BetterReflection\BetterReflection;
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
use function sys_get_temp_dir;
Expand Down Expand Up @@ -101,6 +102,8 @@ public function create(

$container = $configurator->createContainer();

BetterReflection::$phpVersion = $container->getByType(PhpVersion::class)->getVersionId();

// @phpstan-ignore-next-line
BetterReflection::populate(
$container->getService('betterReflectionSourceLocator'),
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1502,4 +1502,16 @@ public function testBug3683(): void
]);
}

public function testStringable(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/stringable.php'], []);
}

}
53 changes: 53 additions & 0 deletions tests/PHPStan/Rules/Methods/data/stringable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace TestStringables;

use Stringable;

class Foo
{

public function __toString(): string
{
return 'foo';
}

}

class Bar implements Stringable
{

public function __toString(): string
{
return 'foo';
}

}

interface Lorem extends Stringable
{

}

class Baz
{

public function doFoo(Stringable $s): void
{

}

public function doBar(Lorem $l): void
{
$this->doFoo(new Foo());
$this->doFoo(new Bar());
$this->doFoo($l);
$this->doBaz($l);
}

public function doBaz(string $s): void
{

}

}

0 comments on commit 3143833

Please sign in to comment.