Skip to content

Commit 34c76d8

Browse files
authored
[Strict] Skip class re-parse for typed default property in UninitializedPropertyAnalyzer (#8101)
A typed property with an explicit default value is always initialized, so empty()/isset() narrowing never applies to it. Detect that case up front via ReflectionProvider and return early, avoiding the heavy AstResolver::resolveClassFromName() parse of the declaring class. The hasType() guard keeps untyped properties (implicit null default) on the existing path, preserving current behaviour.
1 parent 069bb91 commit 34c76d8

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;
4+
5+
final class SkipUntypedProperty
6+
{
7+
public $items;
8+
9+
public function isEmpty()
10+
{
11+
return empty($this->items);
12+
}
13+
14+
public function isNotEmpty()
15+
{
16+
return ! empty($this->items);
17+
}
18+
}

‎rules/Strict/NodeAnalyzer/UninitializedPropertyAnalyzer.php‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\StaticPropertyFetch;
1010
use PhpParser\Node\Stmt\ClassLike;
1111
use PhpParser\Node\Stmt\Property;
12+
use PHPStan\Reflection\ReflectionProvider;
1213
use PHPStan\Type\ThisType;
1314
use Rector\NodeNameResolver\NodeNameResolver;
1415
use Rector\NodeTypeResolver\NodeTypeResolver;
@@ -22,7 +23,8 @@ public function __construct(
2223
private AstResolver $astResolver,
2324
private NodeTypeResolver $nodeTypeResolver,
2425
private ConstructorAssignDetector $constructorAssignDetector,
25-
private NodeNameResolver $nodeNameResolver
26+
private NodeNameResolver $nodeNameResolver,
27+
private ReflectionProvider $reflectionProvider
2628
) {
2729
}
2830

@@ -45,13 +47,20 @@ public function isUninitialized(Expr $expr): bool
4547
return false;
4648
}
4749

50+
$propertyName = (string) $this->nodeNameResolver->getName($expr);
51+
52+
// fast-path: a typed property with an explicit default is always initialized,
53+
// so we can skip the heavy class parsing below
54+
if ($this->hasTypedDefaultProperty($className, $propertyName)) {
55+
return false;
56+
}
57+
4858
$classLike = $this->astResolver->resolveClassFromName($className);
4959

5060
if (! $classLike instanceof ClassLike) {
5161
return false;
5262
}
5363

54-
$propertyName = (string) $this->nodeNameResolver->getName($expr);
5564
$property = $classLike->getProperty($propertyName);
5665

5766
if (! $property instanceof Property) {
@@ -68,4 +77,22 @@ public function isUninitialized(Expr $expr): bool
6877

6978
return ! $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
7079
}
80+
81+
private function hasTypedDefaultProperty(string $className, string $propertyName): bool
82+
{
83+
if (! $this->reflectionProvider->hasClass($className)) {
84+
return false;
85+
}
86+
87+
$classReflection = $this->reflectionProvider->getClass($className);
88+
if (! $classReflection->hasNativeProperty($propertyName)) {
89+
return false;
90+
}
91+
92+
$nativeReflectionProperty = $classReflection->getNativeProperty($propertyName)
93+
->getNativeReflection();
94+
95+
// an untyped property has an implicit null default, which is not an explicit initialization
96+
return $nativeReflectionProperty->hasType() && $nativeReflectionProperty->hasDefaultValue();
97+
}
7198
}

0 commit comments

Comments
 (0)