-
-
Notifications
You must be signed in to change notification settings - Fork 429
[type-declaration] Add AddParamStringTypeFromSprintfUseRector #7477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...hod/AddParamStringTypeFromSprintfUseRector/AddParamStringTypeFromSprintfUseRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddParamStringTypeFromSprintfUseRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...on/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector/Fixture/pass_sprintf.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture; | ||
|
|
||
| final class PassSprintf | ||
| { | ||
| public function test($name) | ||
| { | ||
| return sprintf('Hello %s', $name); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture; | ||
|
|
||
| final class PassSprintf | ||
| { | ||
| public function test(string $name) | ||
| { | ||
| return sprintf('Hello %s', $name); | ||
| } | ||
| } | ||
|
|
||
| ?> |
11 changes: 11 additions & 0 deletions
11
...r/ClassMethod/AddParamStringTypeFromSprintfUseRector/Fixture/skip_non_string_mask.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\Fixture; | ||
|
|
||
| final class SkipNonStringMask | ||
| { | ||
| public function test($name) | ||
| { | ||
| return sprintf('Hello %d', $name); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...tion/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(AddParamStringTypeFromSprintfUseRector::class); | ||
| }; |
75 changes: 0 additions & 75 deletions
75
rules/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector.php
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\NodeAnalyzer; | ||
|
|
||
| use Nette\Utils\Strings; | ||
| 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\PhpParser\Node\BetterNodeFinder; | ||
|
|
||
| final readonly class VariableInSprintfMaskMatcher | ||
| { | ||
| public function __construct( | ||
| private BetterNodeFinder $betterNodeFinder, | ||
| private NodeNameResolver $nodeNameResolver, | ||
| ) { | ||
|
|
||
| } | ||
|
|
||
| public function matchMask(ClassMethod|Function_ $functionLike, string $variableName, string $mask): bool | ||
| { | ||
| $funcCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $functionLike->stmts, FuncCall::class); | ||
|
|
||
| foreach ($funcCalls as $funcCall) { | ||
| if (! $this->nodeNameResolver->isName($funcCall->name, 'sprintf')) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($funcCall->isFirstClassCallable()) { | ||
| continue; | ||
| } | ||
|
|
||
| $args = $funcCall->getArgs(); | ||
| if (count($args) < 2) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var Arg $messageArg */ | ||
| $messageArg = array_shift($args); | ||
| if (! $messageArg->value instanceof String_) { | ||
| continue; | ||
| } | ||
|
|
||
| $string = $messageArg->value; | ||
|
|
||
| // match all %s, %d types by position | ||
| $masks = Strings::match($string->value, '#%[sd]#'); | ||
|
|
||
| foreach ($args as $position => $arg) { | ||
| if (! $arg->value instanceof Variable) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $this-> nodeNameResolver->isName($arg->value, $variableName)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! isset($masks[$position])) { | ||
| continue; | ||
| } | ||
|
|
||
| $knownMaskOnPosition = $masks[$position]; | ||
| if ($knownMaskOnPosition !== $mask) { | ||
| continue; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Function_; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\TypeDeclaration\NodeAnalyzer\VariableInSprintfMaskMatcher; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\AddParamStringTypeFromSprintfUseRectorTest | ||
| */ | ||
| final class AddParamStringTypeFromSprintfUseRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly VariableInSprintfMaskMatcher $variableInSprintfMaskMatcher, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Add string type to parameters used in sprintf calls', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function formatMessage($name) | ||
| { | ||
| return sprintf('My name is %s', $name); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function formatMessage(string $name) | ||
| { | ||
| return sprintf('My name is %s', $name); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| )] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class, Function_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethod|Function_ $node | ||
| */ | ||
| public function refactor(Node $node): ClassMethod|Function_|null | ||
| { | ||
| if ($node->stmts === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->getParams() === []) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
| foreach ($node->getParams() as $param) { | ||
| if ($param->type instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var string $variableName */ | ||
| $variableName = $this->getName($param->var); | ||
|
|
||
| if (! $this->variableInSprintfMaskMatcher->matchMask($node, $variableName, '%s')) { | ||
| continue; | ||
| } | ||
|
|
||
| $param->type = new Identifier('string'); | ||
| $hasChanged = true; | ||
| } | ||
|
|
||
| if (! $hasChanged) { | ||
| return null; | ||
| } | ||
|
|
||
| return $node; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check parent method exists guard is needed here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created PR
for it.