-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Strict] Handle may be unitialized property on DisallowedEmptyRuleFix…
…erRector (#5409) * [Strict] Handle may be unitialized property on DisallowedEmptyRuleFixerRector * fixed 🎉
- Loading branch information
1 parent
a64a344
commit 9f93708
Showing
5 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
.../DisallowedEmptyRuleFixerRector/Fixture/may_unitialized_property_no_default_value.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class MayUnitializedPropertyNoDefaultValue | ||
{ | ||
public array $items; | ||
|
||
public function isEmpty() | ||
{ | ||
return empty($this->items); | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return ! empty($this->items); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class MayUnitializedPropertyNoDefaultValue | ||
{ | ||
public array $items; | ||
|
||
public function isEmpty() | ||
{ | ||
return !isset($this->items) || $this->items === []; | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return isset($this->items) && $this->items !== []; | ||
} | ||
} | ||
|
||
?> |
51 changes: 51 additions & 0 deletions
51
...Empty_/DisallowedEmptyRuleFixerRector/Fixture/property_with_assign_in_constructor.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class PropertyWithAssignInConstructor | ||
{ | ||
public array $items; | ||
|
||
public function __construct() | ||
{ | ||
$this->items = []; | ||
} | ||
|
||
public function isEmpty() | ||
{ | ||
return empty($this->items); | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return ! empty($this->items); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class PropertyWithAssignInConstructor | ||
{ | ||
public array $items; | ||
|
||
public function __construct() | ||
{ | ||
$this->items = []; | ||
} | ||
|
||
public function isEmpty() | ||
{ | ||
return $this->items === []; | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return $this->items !== []; | ||
} | ||
} | ||
|
||
?> |
41 changes: 41 additions & 0 deletions
41
.../Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/property_with_default_value.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class PropertyWithDefaultValue | ||
{ | ||
public array $items = []; | ||
|
||
public function isEmpty() | ||
{ | ||
return empty($this->items); | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return ! empty($this->items); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture; | ||
|
||
final class PropertyWithDefaultValue | ||
{ | ||
public array $items = []; | ||
|
||
public function isEmpty() | ||
{ | ||
return $this->items === []; | ||
} | ||
|
||
public function isNotEmpty() | ||
{ | ||
return $this->items !== []; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Strict\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticPropertyFetch; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\Type\ThisType; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Rector\Core\PhpParser\AstResolver; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\NodeTypeResolver; | ||
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; | ||
|
||
final class UnitializedPropertyAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly AstResolver $astResolver, | ||
private readonly NodeTypeResolver $nodeTypeResolver, | ||
private readonly ConstructorAssignDetector $constructorAssignDetector, | ||
private readonly NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function isUnitialized(Expr $expr): bool | ||
{ | ||
if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) { | ||
return false; | ||
} | ||
|
||
$varType = $this->nodeTypeResolver->getType($expr->var); | ||
|
||
if ($varType instanceof ThisType) { | ||
$varType = $varType->getStaticObjectType(); | ||
} | ||
|
||
if (! $varType instanceof TypeWithClassName) { | ||
return false; | ||
} | ||
|
||
$className = $varType->getClassName(); | ||
$classLike = $this->astResolver->resolveClassFromName($className); | ||
|
||
if (! $classLike instanceof ClassLike) { | ||
return false; | ||
} | ||
|
||
$propertyName = (string) $this->nodeNameResolver->getName($expr); | ||
$property = $classLike->getProperty($propertyName); | ||
|
||
if (! $property instanceof Property) { | ||
return false; | ||
} | ||
|
||
if (count($property->props) !== 1) { | ||
return false; | ||
} | ||
|
||
if ($property->props[0]->default instanceof Expr) { | ||
return false; | ||
} | ||
|
||
return ! $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters