-
-
Notifications
You must be signed in to change notification settings - Fork 432
[type-declaration] Add AddArrayFilterClosureParamTypeRector #7111
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
TomasVotruba
merged 3 commits into
main
from
tv-type-declaration-array-filter-closure-param
Aug 6, 2025
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
...all/AddArrayFunctionClosureParamTypeRector/AddArrayFunctionClosureParamTypeRectorTest.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\FuncCall\AddArrayFunctionClosureParamTypeRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AddArrayFunctionClosureParamTypeRectorTest 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'; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...ll/AddArrayFunctionClosureParamTypeRector/Fixture/array_filter_with_docblock_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,33 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class ArrayFilterWithDocblockType | ||
| { | ||
| /** | ||
| * @param int[] $items | ||
| */ | ||
| public function run(array $items) | ||
| { | ||
| $result = array_filter($items, fn ($item) => $item * 2); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class ArrayFilterWithDocblockType | ||
| { | ||
| /** | ||
| * @param int[] $items | ||
| */ | ||
| public function run(array $items) | ||
| { | ||
| $result = array_filter($items, fn (int $item) => $item * 2); | ||
| } | ||
| } | ||
|
|
||
| ?> |
33 changes: 33 additions & 0 deletions
33
.../Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/include_array_map.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,33 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class IncludeArrayMap | ||
| { | ||
| /** | ||
| * @param int[] $items | ||
| */ | ||
| public function run(array $items) | ||
| { | ||
| $result = array_map(fn ($item) => $item * 2, $items); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class IncludeArrayMap | ||
| { | ||
| /** | ||
| * @param int[] $items | ||
| */ | ||
| public function run(array $items) | ||
| { | ||
| $result = array_map(fn (int $item) => $item * 2, $items); | ||
| } | ||
| } | ||
|
|
||
| ?> |
14 changes: 14 additions & 0 deletions
14
...Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/skip_unclear_param.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,14 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class SkipUnclearParam | ||
| { | ||
| /** | ||
| * @param mixed[] $items | ||
| */ | ||
| public function run(array $items) | ||
| { | ||
| $result = array_filter($items, fn ($item) => $item * 2); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
...tion/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/some_function.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,29 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class SomeFunction | ||
| { | ||
| public function run() | ||
| { | ||
| $array = [1, 2, 3]; | ||
| $result = array_filter($array, fn ($item) => $item * 2); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\Fixture; | ||
|
|
||
| final class SomeFunction | ||
| { | ||
| public function run() | ||
| { | ||
| $array = [1, 2, 3]; | ||
| $result = array_filter($array, fn (int $item) => $item * 2); | ||
| } | ||
| } | ||
|
|
||
| ?> |
9 changes: 9 additions & 0 deletions
9
...aration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/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\FuncCall\AddArrayFunctionClosureParamTypeRector; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([AddArrayFunctionClosureParamTypeRector::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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\Enum; | ||
|
|
||
| final class NativeFuncCallPositions | ||
| { | ||
| /** | ||
| * @var array<string, array<string, int>> | ||
| */ | ||
| public const ARRAY_AND_CALLBACK_POSITIONS = [ | ||
| 'array_walk' => [ | ||
| 'array' => 0, | ||
| 'callback' => 1, | ||
| ], | ||
| 'array_map' => [ | ||
| 'array' => 1, | ||
| 'callback' => 0, | ||
| ], | ||
| 'usort' => [ | ||
| 'array' => 0, | ||
| 'callback' => 1, | ||
| ], | ||
| 'array_filter' => [ | ||
| 'array' => 0, | ||
| 'callback' => 1, | ||
| ], | ||
| ]; | ||
| } |
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
126 changes: 126 additions & 0 deletions
126
rules/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector.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,126 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\TypeDeclaration\Rector\FuncCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\ArrowFunction; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use PHPStan\Type\MixedType; | ||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||
| use Rector\TypeDeclaration\Enum\NativeFuncCallPositions; | ||
| use Rector\ValueObject\PhpVersionFeature; | ||
| use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector\AddArrayFunctionClosureParamTypeRectorTest | ||
| */ | ||
| final class AddArrayFunctionClosureParamTypeRector extends AbstractRector implements MinPhpVersionInterface | ||
| { | ||
| public function __construct( | ||
| private readonly StaticTypeMapper $staticTypeMapper | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Add array_filter()/array_map() function closure param type, based on passed iterable', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| $items = [1, 2, 3]; | ||
| $result = array_filter($items, fn ($item) => $item > 1); | ||
| CODE_SAMPLE | ||
|
|
||
| , | ||
| <<<'CODE_SAMPLE' | ||
| $items = [1, 2, 3]; | ||
| $result = array_filter($items, fn (int $item) => $item > 1 | ||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [FuncCall::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param FuncCall $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| foreach (NativeFuncCallPositions::ARRAY_AND_CALLBACK_POSITIONS as $functionName => $positions) { | ||
| if (! $this->isName($node, $functionName)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($node->isFirstClassCallable()) { | ||
| continue; | ||
| } | ||
|
|
||
| $arrayPosition = $positions['array']; | ||
| $callbackPosition = $positions['callback']; | ||
|
|
||
| $firstArgExpr = $node->getArgs()[$callbackPosition] | ||
| ->value; | ||
| if (! $firstArgExpr instanceof ArrowFunction && ! $firstArgExpr instanceof Closure) { | ||
| continue; | ||
| } | ||
|
|
||
| $arrowFunction = $firstArgExpr; | ||
| $arrowFunctionParam = $arrowFunction->getParams()[0]; | ||
|
|
||
| // param is known already | ||
| if ($arrowFunctionParam->type instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| $passedExprType = $this->getType($node->getArgs()[$arrayPosition]->value); | ||
| if ($passedExprType instanceof ConstantArrayType || $passedExprType instanceof ArrayType) { | ||
| $singlePassedExprType = $passedExprType->getItemType(); | ||
|
|
||
| if ($singlePassedExprType instanceof MixedType) { | ||
| continue; | ||
| } | ||
|
|
||
| $paramType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( | ||
| $singlePassedExprType, | ||
| TypeKind::PARAM | ||
| ); | ||
|
|
||
| if (! $paramType instanceof Node) { | ||
| continue; | ||
| } | ||
|
|
||
| $arrowFunctionParam->type = $paramType; | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| public function provideMinPhpVersion(): int | ||
| { | ||
| return PhpVersionFeature::SCALAR_TYPES; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.