From 7bce920b72852f0c6e06731a10f9f8345d8629e2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 24 Nov 2026 17:57:24 +0100 Subject: [PATCH] [CodeQuality] Skip uninitialized typed array property in SimplifyEmptyCheckOnEmptyArrayRector --- ...uninitialized_typed_array_property.php.inc | 20 +++++++++ ...y_property_assigned_in_constructor.php.inc | 41 +++++++++++++++++++ .../SimplifyEmptyCheckOnEmptyArrayRector.php | 39 ++++++++++++++++-- 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/skip_uninitialized_typed_array_property.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/typed_array_property_assigned_in_constructor.php.inc diff --git a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/skip_uninitialized_typed_array_property.php.inc b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/skip_uninitialized_typed_array_property.php.inc new file mode 100644 index 00000000000..f484891ee79 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/skip_uninitialized_typed_array_property.php.inc @@ -0,0 +1,20 @@ +items = $items; + } + + public function isEmpty(): bool + { + return empty($this->items); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/typed_array_property_assigned_in_constructor.php.inc b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/typed_array_property_assigned_in_constructor.php.inc new file mode 100644 index 00000000000..62c2b71ebf9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector/Fixture/typed_array_property_assigned_in_constructor.php.inc @@ -0,0 +1,41 @@ +items = $items; + } + + public function isEmpty(): bool + { + return empty($this->items); + } +} + +?> +----- +items = $items; + } + + public function isEmpty(): bool + { + return $this->items === []; + } +} + +?> diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 42eb0dd46b4..8ccb24dd731 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -14,9 +14,11 @@ use PhpParser\Node\Expr\StaticPropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Param; use PhpParser\Node\Stmt\Property; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; +use PHPStan\Reflection\Php\PhpPropertyReflection; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\NodeAnalyzer\ExprAnalyzer; @@ -27,6 +29,7 @@ use Rector\Reflection\ReflectionResolver; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; +use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer; /** @@ -39,7 +42,8 @@ public function __construct( private readonly ReflectionResolver $reflectionResolver, private readonly AstResolver $astResolver, private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, - private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer + private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer, + private readonly ConstructorAssignDetector $constructorAssignDetector ) { } @@ -128,8 +132,12 @@ private function isAllowedExpr(Variable|PropertyFetch|StaticPropertyFetch $expr, $nativeType = $phpPropertyReflection->getNativeType(); if (! $nativeType instanceof MixedType) { - return $nativeType->isArray() - ->yes(); + if (! $nativeType->isArray()->yes()) { + return false; + } + + // reading an uninitialized typed property throws, while empty() returns true + return $this->isInitializedProperty($phpPropertyReflection, $propertyName); } $property = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection); @@ -152,4 +160,29 @@ private function isAllowedExpr(Variable|PropertyFetch|StaticPropertyFetch $expr, return $type->isArray() ->yes(); } + + private function isInitializedProperty(PhpPropertyReflection $phpPropertyReflection, string $propertyName): bool + { + $propertyNode = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection); + + // promoted property is assigned on construct + if ($propertyNode instanceof Param) { + return true; + } + + if (! $propertyNode instanceof Property) { + return false; + } + + if ($propertyNode->props[0]->default !== null) { + return true; + } + + $classLike = $this->astResolver->resolveClassFromClassReflection($phpPropertyReflection->getDeclaringClass()); + if ($classLike === null) { + return false; + } + + return $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName); + } }