|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\TypeDeclaration\Rector\FuncCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\ArrayDimFetch; |
| 9 | +use PhpParser\Node\Expr\ArrowFunction; |
| 10 | +use PhpParser\Node\Expr\FuncCall; |
| 11 | +use PhpParser\Node\Expr\Ternary; |
| 12 | +use PhpParser\Node\Identifier; |
| 13 | +use PhpParser\NodeFinder; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Rector\ValueObject\PhpVersionFeature; |
| 16 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see \Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrowFunctionParamArrayWhereDimFetchRector\AddArrowFunctionParamArrayWhereDimFetchRectorTest |
| 22 | + */ |
| 23 | +final class AddArrowFunctionParamArrayWhereDimFetchRector extends AbstractRector implements MinPhpVersionInterface |
| 24 | +{ |
| 25 | + public function getRuleDefinition(): RuleDefinition |
| 26 | + { |
| 27 | + return new RuleDefinition('Add function/closure param array type, if dim fetch is inside', [ |
| 28 | + new CodeSample( |
| 29 | + <<<'CODE_SAMPLE' |
| 30 | +$array = [['name' => 'John']]; |
| 31 | +
|
| 32 | +$result = array_map(fn ($item) => $item['name'], $array); |
| 33 | +CODE_SAMPLE |
| 34 | + |
| 35 | + , |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +$array = [['name' => 'John']]; |
| 38 | +
|
| 39 | +$result = array_map(fn ($item) => $item['name'], $array); |
| 40 | +CODE_SAMPLE |
| 41 | + ), |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return array<class-string<Node>> |
| 47 | + */ |
| 48 | + public function getNodeTypes(): array |
| 49 | + { |
| 50 | + return [FuncCall::class]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param FuncCall $node |
| 55 | + */ |
| 56 | + public function refactor(Node $node): ?Node |
| 57 | + { |
| 58 | + if (! $this->isName($node, 'array_map')) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if ($node->isFirstClassCallable()) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + $firstArgExpr = $node->getArgs()[0] |
| 67 | + ->value; |
| 68 | + if (! $firstArgExpr instanceof ArrowFunction) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + $arrowFunction = $firstArgExpr; |
| 73 | + $arrowFunctionParam = $arrowFunction->getParams()[0]; |
| 74 | + |
| 75 | + // param is known already |
| 76 | + if ($arrowFunctionParam->type instanceof Node) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + if ($this->hasTernary($arrowFunction)) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + $paramName = $this->getName($arrowFunctionParam); |
| 85 | + if (! $this->isParamArrayDimFetched($arrowFunction, $paramName)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $arrowFunctionParam->type = new Identifier('array'); |
| 90 | + |
| 91 | + return $node; |
| 92 | + } |
| 93 | + |
| 94 | + public function provideMinPhpVersion(): int |
| 95 | + { |
| 96 | + return PhpVersionFeature::SCALAR_TYPES; |
| 97 | + } |
| 98 | + |
| 99 | + private function isParamArrayDimFetched(ArrowFunction $arrowFunction, string $paramName): bool |
| 100 | + { |
| 101 | + $nodeFinder = new NodeFinder(); |
| 102 | + |
| 103 | + $arrayDimFetches = $nodeFinder->findInstanceOf($arrowFunction->expr, ArrayDimFetch::class); |
| 104 | + foreach ($arrayDimFetches as $arrayDimFetch) { |
| 105 | + if ($this->isName($arrayDimFetch->var, $paramName)) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + private function hasTernary(ArrowFunction $arrowFunction): bool |
| 114 | + { |
| 115 | + $nodeFinder = new NodeFinder(); |
| 116 | + return (bool) $nodeFinder->findFirstInstanceOf($arrowFunction->expr, Ternary::class); |
| 117 | + } |
| 118 | +} |
0 commit comments