Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture;

final class SkipConditionalNonString
{
/**
* @param false|float|int|string $cell
*
* @return bool|float|int|string
*/
function run($data)
{
if (is_string($data)) {
$data = sprintf('"%s"', $data);
}

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\Guard\ParamTypeAddGuard;
use Rector\TypeDeclaration\NodeAnalyzer\VariableInSprintfMaskMatcher;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -26,6 +27,7 @@ final class AddParamStringTypeFromSprintfUseRector extends AbstractRector
public function __construct(
private readonly VariableInSprintfMaskMatcher $variableInSprintfMaskMatcher,
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly ParamTypeAddGuard $paramTypeAddGuard
) {
}

Expand Down Expand Up @@ -94,8 +96,11 @@ public function refactor(Node $node): ClassMethod|Function_|Closure|ArrowFunctio
continue;
}

$variableName = $this->getName($param);
if (! $this->paramTypeAddGuard->isLegal($param, $node)) {
continue;
}

$variableName = $this->getName($param);
if (! $this->variableInSprintfMaskMatcher->matchMask($node, $variableName, '%s')) {
continue;
}
Expand Down