diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector/Fixture/constant_variable.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector/Fixture/constant_variable.php.inc new file mode 100644 index 00000000000..ee917caad7f --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector/Fixture/constant_variable.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php b/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php index b7ccb574b5a..b9f76999273 100644 --- a/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php +++ b/rules/DeadCode/Rector/Stmt/RemoveConditionExactReturnRector.php @@ -8,8 +8,6 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; diff --git a/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php b/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php index b69a16007b9..e0200020f0b 100644 --- a/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php +++ b/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php @@ -8,17 +8,20 @@ use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use Rector\NodeNameResolver\NodeNameResolver; +use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpParser\Node\BetterNodeFinder; +use Rector\PhpParser\Node\Value\ValueResolver; final readonly class VariableInSprintfMaskMatcher { public function __construct( private BetterNodeFinder $betterNodeFinder, private NodeNameResolver $nodeNameResolver, + private NodeTypeResolver $nodeTypeResolver, + private ValueResolver $valueResolver, ) { } @@ -43,14 +46,16 @@ public function matchMask(ClassMethod|Function_ $functionLike, string $variableN /** @var Arg $messageArg */ $messageArg = array_shift($args); - if (! $messageArg->value instanceof String_) { + + $type = $this->nodeTypeResolver->getType($messageArg->value); + + $messageValue = $this->valueResolver->getValue($messageArg->value); + if (! is_string($messageValue)) { continue; } - $string = $messageArg->value; - // match all %s, %d types by position - $masks = Strings::match($string->value, '#%[sd]#'); + $masks = Strings::match($messageValue, '#%[sd]#'); foreach ($args as $position => $arg) { if (! $arg->value instanceof Variable) { diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php index 1b5bc35f8c8..4b6bdc6a10d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php @@ -23,7 +23,7 @@ final class AddParamStringTypeFromSprintfUseRector extends AbstractRector { public function __construct( private readonly VariableInSprintfMaskMatcher $variableInSprintfMaskMatcher, - private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard + private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, ) { }