Updated Rector to commit e2a184cb16dfe1964feecbe44f7d7cc6130ac010

e2a184cb16 [NodeTypeResolver] Using built int ->isArray()->yes() on ArrayTypeAnalyzer::isArrayType() (#5870)
This commit is contained in:
Tomas Votruba 2024-05-12 15:56:38 +00:00
parent af81b5db1b
commit 49d837e2a6
2 changed files with 4 additions and 77 deletions

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'b18da7d458c2d240dd50682b31670e134f2b1ed3';
public const PACKAGE_VERSION = 'e2a184cb16dfe1964feecbe44f7d7cc6130ac010';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-05-12 01:14:59';
public const RELEASE_DATE = '2024-05-12 22:52:35';
/**
* @var int
*/

View File

@ -4,19 +4,7 @@ declare (strict_types=1);
namespace Rector\NodeTypeResolver\TypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Reflection\ReflectionResolver;
final class ArrayTypeAnalyzer
{
/**
@ -24,74 +12,13 @@ final class ArrayTypeAnalyzer
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @readonly
* @var \Rector\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(NodeTypeResolver $nodeTypeResolver, ReflectionResolver $reflectionResolver)
public function __construct(NodeTypeResolver $nodeTypeResolver)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->reflectionResolver = $reflectionResolver;
}
public function isArrayType(Expr $expr) : bool
{
$nodeType = $this->nodeTypeResolver->getNativeType($expr);
if ($this->isIntersectionArrayType($nodeType)) {
return \true;
}
// PHPStan false positive, when variable has type[] docblock, but default array is missing
if (($expr instanceof PropertyFetch || $expr instanceof StaticPropertyFetch) && $this->isPropertyFetchWithArrayDefault($expr)) {
return \true;
}
if ($nodeType instanceof MixedType) {
if ($nodeType->isExplicitMixed()) {
return \false;
}
if ($this->isPropertyFetchWithArrayDefault($expr)) {
return \true;
}
}
return $nodeType instanceof ArrayType;
}
private function isIntersectionArrayType(Type $nodeType) : bool
{
if (!$nodeType instanceof IntersectionType) {
return \false;
}
foreach ($nodeType->getTypes() as $intersectionNodeType) {
if ($intersectionNodeType instanceof ArrayType) {
continue;
}
if ($intersectionNodeType instanceof HasOffsetType) {
continue;
}
if ($intersectionNodeType instanceof NonEmptyArrayType) {
continue;
}
return \false;
}
return \true;
}
/**
* phpstan bug workaround - https://phpstan.org/r/0443f283-244c-42b8-8373-85e7deb3504c
*/
private function isPropertyFetchWithArrayDefault(Expr $expr) : bool
{
if (!$expr instanceof PropertyFetch && !$expr instanceof StaticPropertyFetch) {
return \false;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($expr);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($expr);
if ($phpPropertyReflection instanceof PhpPropertyReflection) {
$reflectionProperty = $phpPropertyReflection->getNativeReflection();
$betterReflection = $reflectionProperty->getBetterReflection();
$defaultValueExpr = $betterReflection->getDefaultValueExpression();
return $defaultValueExpr instanceof Array_;
}
return \false;
return $nodeType->isArray()->yes();
}
}