-
-
Notifications
You must be signed in to change notification settings - Fork 430
[type-declaration] Add AddParamTypeFromStrictMethodCallPassRector #7488
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
Closed
Closed
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
...aramTypeFromStrictMethodCallPassRector/AddParamTypeFromStrictMethodCallPassRectorTest.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\AddParamTypeFromStrictMethodCallPassRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddParamTypeFromStrictMethodCallPassRectorTest 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'; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
...ion/Rector/ClassMethod/AddParamTypeFromStrictMethodCallPassRector/Fixture/fixture.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,35 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture; | ||
|
|
||
| final class SomeClass | ||
| { | ||
| public function setValue($number) | ||
| { | ||
| $this->checkInt($number); | ||
| } | ||
|
|
||
| private function checkInt(int $integer) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture; | ||
|
|
||
| final class SomeClass | ||
| { | ||
| public function setValue(int $number) | ||
| { | ||
| $this->checkInt($number); | ||
| } | ||
|
|
||
| private function checkInt(int $integer) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
19 changes: 19 additions & 0 deletions
19
...ctor/ClassMethod/AddParamTypeFromStrictMethodCallPassRector/Fixture/skip_if_check.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,19 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\Fixture; | ||
|
|
||
| final class SomeClass | ||
| { | ||
| public function setValue($number) | ||
| { | ||
| if (is_string($number)) { | ||
| return; | ||
| } | ||
|
|
||
| $this->checkInt($number); | ||
| } | ||
|
|
||
| private function checkInt(int $integer) | ||
| { | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../Rector/ClassMethod/AddParamTypeFromStrictMethodCallPassRector/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,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([AddParamTypeFromStrictMethodCallPassRector::class]); |
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
39 changes: 39 additions & 0 deletions
39
rules/TypeDeclaration/NodeTypeAnalyzer/MethodCallParamTypeResolver.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,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\NodeTypeAnalyzer; | ||
|
|
||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Type\Type; | ||
| use Rector\Reflection\ReflectionResolver; | ||
|
|
||
| final readonly class MethodCallParamTypeResolver | ||
| { | ||
| public function __construct( | ||
| private ReflectionResolver $reflectionResolver, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return array<int, Type> | ||
| */ | ||
| public function resolve(MethodCall $methodCall): array | ||
| { | ||
| $methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall); | ||
| if (! $methodReflection instanceof MethodReflection) { | ||
| return []; | ||
| } | ||
|
|
||
| $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants()); | ||
|
|
||
| $typeByPosition = []; | ||
| foreach ($extendedParametersAcceptor->getParameters() as $position => $parameterReflection) { | ||
| $typeByPosition[$position] = $parameterReflection->getNativeType(); | ||
| } | ||
|
|
||
| return $typeByPosition; | ||
| } | ||
| } |
167 changes: 167 additions & 0 deletions
167
rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromStrictMethodCallPassRector.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,167 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node\Expr\Closure; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\Variable; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PhpParser\Node\Stmt\Function_; | ||
| use PHPStan\Type\Type; | ||
| use Rector\PhpParser\Node\BetterNodeFinder; | ||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||
| use Rector\TypeDeclaration\NodeTypeAnalyzer\MethodCallParamTypeResolver; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromStrictMethodCallPassRector\AddParamTypeFromStrictMethodCallPassRectorTest | ||
| */ | ||
| final class AddParamTypeFromStrictMethodCallPassRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly BetterNodeFinder $betterNodeFinder, | ||
| private readonly MethodCallParamTypeResolver $methodCallParamTypeResolver, | ||
| private readonly StaticTypeMapper $staticTypeMapper, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Change param type from strict type of passed-to method call', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| final class SomeClass | ||
| { | ||
| public function run($value) | ||
| { | ||
| $this->resolve($value); | ||
| } | ||
|
|
||
| private function resolve(int $value) | ||
| { | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| final class SomeClass | ||
| { | ||
| public function run(int $value) | ||
| { | ||
| $this->resolve($value); | ||
| } | ||
|
|
||
| private function resolve(int $value) | ||
| { | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [ClassMethod::class, Function_::class, Closure::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassMethod|Function_|Closure $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if ($node->getParams() === []) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node instanceof ClassMethod && $node->stmts === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $hasChanged = false; | ||
|
|
||
| $usedVariables = $this->betterNodeFinder->findInstancesOfScoped((array) $node->stmts, Variable::class); | ||
|
|
||
| foreach ($node->getParams() as $param) { | ||
| // already known type | ||
| if ($param->type instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var string $paramName */ | ||
| $paramName = $this->getName($param->var); | ||
|
|
||
| $paramVariables = array_filter( | ||
| $usedVariables, | ||
| fn (Variable $variable): bool => $this->isName($variable, $paramName) | ||
| ); | ||
|
|
||
| if (count($paramVariables) >= 2) { | ||
| // skip for now, as we look for sole use | ||
| continue; | ||
| } | ||
|
|
||
| $methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $node->stmts, MethodCall::class); | ||
| foreach ($methodCalls as $methodCall) { | ||
| if ($methodCall->isFirstClassCallable()) { | ||
| continue; | ||
| } | ||
|
|
||
| $typesByPosition = $this->methodCallParamTypeResolver->resolve($methodCall); | ||
|
|
||
| $usedPosition = $this->matchParamMethodCallUsedPosition($methodCall, $paramName); | ||
| if (! is_int($usedPosition)) { | ||
| continue; | ||
| } | ||
|
|
||
| $paramType = $typesByPosition[$usedPosition] ?? null; | ||
| if (! $paramType instanceof Type) { | ||
| continue; | ||
| } | ||
|
|
||
| $paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM); | ||
| if (! $paramTypeNode instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| $param->type = $paramTypeNode; | ||
| $hasChanged = true; | ||
|
|
||
| // go to next param | ||
| continue 2; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function matchParamMethodCallUsedPosition(MethodCall $methodCall, string $paramName): int|null | ||
| { | ||
| foreach ($methodCall->getArgs() as $position => $arg) { | ||
| if (! $arg->value instanceof Variable) { | ||
| continue; | ||
| } | ||
|
|
||
| if (! $this->isName($arg->value, $paramName)) { | ||
| continue; | ||
| } | ||
|
|
||
| return $position; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@TomasVotruba there is already rule
ParamTypeByMethodCallTypeRectorfor it, seehttps://getrector.com/demo/e925c962-1957-42dc-8464-c943e7c2850b
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.
Thanks 👍 I knew we had some similar rule :D . It missed an edge case, I'll check why.
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.
This one: https://getrector.com/demo/31f70072-2606-48c0-aafe-6d8b9df5428c