Skip to content

Commit

Permalink
Skip traits in CountOnNullRector (#4556)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 20, 2023
1 parent ebe8c2e commit 7b2f2e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
@@ -0,0 +1,11 @@
<?php

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

trait SkipTrait
{
public function run()
{
return count($this->getItems());
}
}
15 changes: 15 additions & 0 deletions rules/Php71/Rector/FuncCall/CountOnNullRector.php
Expand Up @@ -19,6 +19,7 @@
use PhpParser\Node\Scalar\LNumber;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -85,6 +86,10 @@ public function getNodeTypes(): array
*/
public function refactorWithScope(Node $node, Scope $scope): int|Ternary|null|FuncCall
{
if ($this->isInsideTrait($scope)) {
return null;
}

if ($node instanceof Ternary) {
if ($this->shouldSkipTernaryIfElseCountFuncCall($node)) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
Expand Down Expand Up @@ -218,4 +223,14 @@ private function refactorArrayType(

return $this->castToArray($countedExpr, $funcCall);
}

private function isInsideTrait(Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

return $classReflection->isTrait();
}
}

0 comments on commit 7b2f2e8

Please sign in to comment.