Skip to content

Commit

Permalink
[Php71] Skip static or global on CountOnNullRector (#1742)
Browse files Browse the repository at this point in the history
* [Php71] Skip static or global on CountOnNullRector

* Fixed 🎉
  • Loading branch information
samsonasik committed Jan 29, 2022
1 parent 70d6465 commit 9db5159
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php71\Rector\FuncCall\CountOnNullRector\Fixture;

$data = ['test'];

class SkipGlobal
{
public function run()
{
global $data;
echo count($data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php71\Rector\FuncCall\CountOnNullRector\Fixture;

class SkipStatic
{
public function run()
{
static $data = [];
echo count($data);
}
}
15 changes: 13 additions & 2 deletions rules/Php71/Rector/FuncCall/CountOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
Expand All @@ -22,6 +23,7 @@
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -45,7 +47,8 @@ final class CountOnNullRector extends AbstractRector implements MinPhpVersionInt

public function __construct(
private readonly CountableTypeAnalyzer $countableTypeAnalyzer,
private readonly CountableAnalyzer $countableAnalyzer
private readonly CountableAnalyzer $countableAnalyzer,
private readonly VariableAnalyzer $variableAnalyzer
) {
}

Expand Down Expand Up @@ -193,7 +196,15 @@ private function shouldSkip(FuncCall $funcCall): bool

// skip node in trait, as impossible to analyse
$trait = $this->betterNodeFinder->findParentType($funcCall, Trait_::class);
return $trait instanceof Trait_;
if ($trait instanceof Trait_) {
return true;
}

if (! $funcCall->args[0]->value instanceof Variable) {
return false;
}

return $this->variableAnalyzer->isStaticOrGlobal($funcCall->args[0]->value);
}

private function castToArray(Expr $countedExpr, FuncCall $funcCall): FuncCall
Expand Down

0 comments on commit 9db5159

Please sign in to comment.