mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
[TypeDeclaration] Fix static property type resolution (#2518)
[TypeDeclaration] Fix static property type resolution
This commit is contained in:
commit
6980a999e0
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user