Skip to content

Commit

Permalink
[Strict] Add StaticProperty supporton may be unitialized static prope…
Browse files Browse the repository at this point in the history
…rty on DisallowedEmptyRuleFixerRector (#5428)
  • Loading branch information
samsonasik committed Jan 4, 2024
1 parent f85e8c2 commit 1f9829d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
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 MayUnitializedStaticPropertyNoDefaultValue
{
public static array $items;

public function isEmpty()
{
return empty(self::$items);
}

public function isNotEmpty()
{
return ! empty(self::$items);
}
}

?>
-----
<?php

namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;

final class MayUnitializedStaticPropertyNoDefaultValue
{
public static array $items;

public function isEmpty()
{
return !isset(self::$items) || self::$items === [];
}

public function isNotEmpty()
{
return isset(self::$items) && self::$items !== [];
}
}

?>
7 changes: 5 additions & 2 deletions rules/Strict/NodeAnalyzer/UnitializedPropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;
Expand All @@ -27,11 +28,13 @@ public function __construct(

public function isUnitialized(Expr $expr): bool
{
if (! $expr instanceof PropertyFetch) {
if (! $expr instanceof PropertyFetch && ! $expr instanceof StaticPropertyFetch) {
return false;
}

$varType = $this->nodeTypeResolver->getType($expr->var);
$varType = $expr instanceof PropertyFetch
? $this->nodeTypeResolver->getType($expr->var)
: $this->nodeTypeResolver->getType($expr->class);

if ($varType instanceof ThisType) {
$varType = $varType->getStaticObjectType();
Expand Down

0 comments on commit 1f9829d

Please sign in to comment.