[TypeDeclaration] Fix static property type resolution (#2518)

[TypeDeclaration] Fix static property type resolution
This commit is contained in:
Tomas Votruba 2019-12-29 13:00:42 +01:00 committed by GitHub
commit 6980a999e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 3 deletions

View File

@ -11,7 +11,6 @@ use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class TypedPropertyRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP >= 7.4
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
@ -24,6 +23,20 @@ final class TypedPropertyRectorTest extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
/**
* @requires PHP >= 7.4
* @dataProvider provideDataForTest()
*/
public function testPhp74(string $file): void
{
$this->doTestFile($file);
}
public function provideDataForTestPhp74(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp74');
}
protected function getRectorClass(): string
{
return TypedPropertyRector::class;

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
@ -57,7 +58,7 @@ final class AssignToPropertyTypeInferer extends AbstractTypeInferer
*/
private function matchPropertyAssignExpr(Assign $assign, string $propertyName): ?Expr
{
if ($assign->var instanceof PropertyFetch) {
if ($this->isPropertyFetch($assign->var)) {
if (! $this->nameResolver->isName($assign->var, $propertyName)) {
return null;
}
@ -65,7 +66,7 @@ final class AssignToPropertyTypeInferer extends AbstractTypeInferer
return $assign->expr;
}
if ($assign->var instanceof ArrayDimFetch && $assign->var->var instanceof PropertyFetch) {
if ($assign->var instanceof ArrayDimFetch && $this->isPropertyFetch($assign->var->var)) {
if (! $this->nameResolver->isName($assign->var->var, $propertyName)) {
return null;
}
@ -75,4 +76,9 @@ final class AssignToPropertyTypeInferer extends AbstractTypeInferer
return null;
}
private function isPropertyFetch(Node $node): bool
{
return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch;
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\FunctionLike\PropertyTypeDeclarationRector\Fixture;
class StaticPropertyWithDefaultNull
{
protected static $cacheFile = null;
protected static $cacheFiles = null;
/**
* @return string
*/
public static function coreCache($file = '')
{
return $file;
}
/**
* Register rex_autoload in spl autoloader.
*/
public static function register()
{
self::$cacheFile = self::coreCache();
self::$cacheFiles[] = self::coreCache();
}
}
?>
-----
<?php
namespace Rector\TypeDeclaration\Tests\Rector\FunctionLike\PropertyTypeDeclarationRector\Fixture;
class StaticPropertyWithDefaultNull
{
/**
* @var string|null
*/
protected static $cacheFile = null;
/**
* @var string[]|null
*/
protected static $cacheFiles = null;
/**
* @return string
*/
public static function coreCache($file = '')
{
return $file;
}
/**
* Register rex_autoload in spl autoloader.
*/
public static function register()
{
self::$cacheFile = self::coreCache();
self::$cacheFiles[] = self::coreCache();
}
}
?>