-
-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c803b81
commit 619b849
Showing
4 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
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
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 !== []; | ||
} | ||
} | ||
|
||
?> |
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