Skip to content

Commit

Permalink
[PHP 8.1] Skip trait in NullToStrictStringFuncCallArgRector as unknow…
Browse files Browse the repository at this point in the history
…n context (#3180)

Co-authored-by: Stephan Vierkant <stephan@vierkant.net>
  • Loading branch information
TomasVotruba and stephanvierkant committed Dec 10, 2022
1 parent 44ebace commit 91f4cae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector\Fixture;

trait SkipTrait
{
public function getTitle()
{
if ($this->title === null) {
return null;
}

return trim($this->title);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -33,7 +34,7 @@
/**
* @see \Rector\Tests\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector\NullToStrictStringFuncCallArgRectorTest
*/
final class NullToStrictStringFuncCallArgRector extends AbstractRector implements MinPhpVersionInterface
final class NullToStrictStringFuncCallArgRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @var array<string, string[]>
Expand Down Expand Up @@ -375,9 +376,9 @@ public function getNodeTypes(): array
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($this->shouldSkip($node)) {
if ($this->shouldSkip($node, $scope)) {
return null;
}

Expand Down Expand Up @@ -550,13 +551,18 @@ private function resolveOriginalPositions(FuncCall $funcCall): array
return $positions;
}

private function shouldSkip(FuncCall $funcCall): bool
private function shouldSkip(FuncCall $funcCall, Scope $scope): bool
{
$functionNames = array_keys(self::ARG_POSITION_NAME_NULL_TO_STRICT_STRING);
if (! $this->nodeNameResolver->isNames($funcCall, $functionNames)) {
return true;
}

$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
return true;
}

return $funcCall->isFirstClassCallable();
}
}

0 comments on commit 91f4cae

Please sign in to comment.