-
-
Notifications
You must be signed in to change notification settings - Fork 431
[type-declaration] Add AddParamFromDimFetchKeyUseRector #7424
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
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
...tor/ClassMethod/AddParamFromDimFetchKeyUseRector/AddParamFromDimFetchKeyUseRectorTest.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\AddParamFromDimFetchKeyUseRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddParamFromDimFetchKeyUseRectorTest 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'; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...peDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/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,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class Fixture | ||
| { | ||
| public function process($key) | ||
| { | ||
| $items = [ | ||
| 'first' => 'Firstitem', | ||
| 'second' => 'Seconditem', | ||
| ]; | ||
|
|
||
| return $items[$key]; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class Fixture | ||
| { | ||
| public function process(string $key) | ||
| { | ||
| $items = [ | ||
| 'first' => 'Firstitem', | ||
| 'second' => 'Seconditem', | ||
| ]; | ||
|
|
||
| return $items[$key]; | ||
| } | ||
| } | ||
|
|
||
| ?> |
39 changes: 39 additions & 0 deletions
39
...aration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/int_or_string.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,39 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class IntOrString | ||
| { | ||
| public function process($key) | ||
| { | ||
| $items = [ | ||
| 'first' => 'Firstitem', | ||
| 111 => 'Seconditem', | ||
| 'second' => 'Seconditem', | ||
| ]; | ||
|
|
||
| return $items[$key]; | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class IntOrString | ||
| { | ||
| public function process(string|int $key) | ||
| { | ||
| $items = [ | ||
| 'first' => 'Firstitem', | ||
| 111 => 'Seconditem', | ||
| 'second' => 'Seconditem', | ||
| ]; | ||
|
|
||
| return $items[$key]; | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...on/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_existing_type.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,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class SkipExistingType | ||
| { | ||
| public function process(int $key): void | ||
| { | ||
| $items = [ | ||
| 'first' => 'Firstitem', | ||
| 'second' => 'Seconditem', | ||
| ]; | ||
|
|
||
| return $items[$key]; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
...ion/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_unclear_type.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\AddParamFromDimFetchKeyUseRector\Fixture; | ||
|
|
||
| final class SkipUnclearType | ||
| { | ||
| public function process($key, $items) | ||
| { | ||
| return $items[$key]; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...eclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/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\Config\RectorConfig; | ||
| use Rector\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(AddParamFromDimFetchKeyUseRector::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
131 changes: 131 additions & 0 deletions
131
rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.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,131 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\Rector\ClassMethod; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||
| use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\AddParamFromDimFetchKeyUseRectorTest | ||
| */ | ||
| final class AddParamFromDimFetchKeyUseRector extends AbstractRector | ||
| { | ||
| public function __construct( | ||
| private readonly ArrayDimFetchFinder $arrayDimFetchFinder, | ||
| private readonly StaticTypeMapper $staticTypeMapper | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Add method param type based on use in array dim fetch of known keys', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| final class SomeClass | ||
| { | ||
| public function get($key) | ||
| { | ||
| $data = [ | ||
| 'name' => 'John', | ||
| 'age' => 30, | ||
| ]; | ||
|
|
||
| return $data[$key]; | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| final class SomeClass | ||
| { | ||
| public function get(string $key) | ||
| { | ||
| $data = [ | ||
| 'name' => 'John', | ||
| 'age' => 30, | ||
| ]; | ||
|
|
||
| return $data[$key]; | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Class_::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Class_ $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| $hasChanged = false; | ||
|
|
||
| foreach ($node->getMethods() as $classMethod) { | ||
| if ($classMethod->params === []) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($classMethod->getParams() as $param) { | ||
| if ($param->type instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var string $paramName */ | ||
| $paramName = $this->getName($param->var); | ||
|
|
||
| $dimFetches = $this->arrayDimFetchFinder->findByDimName($classMethod, $paramName); | ||
| if ($dimFetches === []) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($dimFetches as $dimFetch) { | ||
| $dimFetchType = $this->getType($dimFetch->var); | ||
|
|
||
| if (! $dimFetchType instanceof ArrayType && ! $dimFetchType instanceof ConstantArrayType) { | ||
| continue; | ||
| } | ||
|
|
||
| $paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( | ||
| $dimFetchType->getKeyType(), | ||
| TypeKind::PARAM | ||
| ); | ||
|
|
||
| if (! $paramTypeNode instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| $param->type = $paramTypeNode; | ||
| $hasChanged = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($hasChanged) { | ||
| return $node; | ||
| } | ||
|
|
||
| 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
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.
Parent guard check 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 new PR for it: