|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\TypeDeclaration\Rector\FunctionLike; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\Closure; |
| 8 | +use PhpParser\Node\Expr\FuncCall; |
| 9 | +use PhpParser\Node\Param; |
| 10 | +use PHPStan\Reflection\Native\NativeFunctionReflection; |
| 11 | +use PHPStan\Type\ClosureType; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\UnionType; |
| 15 | +use PHPStan\Type\UnionTypeHelper; |
| 16 | +use Rector\NodeTypeResolver\TypeComparator\TypeComparator; |
| 17 | +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Rector\Reflection\ReflectionResolver; |
| 20 | +use Rector\StaticTypeMapper\StaticTypeMapper; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 23 | +/** |
| 24 | + * @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayReduceRector\AddClosureParamTypeForArrayReduceRectorTest |
| 25 | + */ |
| 26 | +final class AddClosureParamTypeForArrayReduceRector extends AbstractRector |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @readonly |
| 30 | + */ |
| 31 | + private TypeComparator $typeComparator; |
| 32 | + /** |
| 33 | + * @readonly |
| 34 | + */ |
| 35 | + private StaticTypeMapper $staticTypeMapper; |
| 36 | + /** |
| 37 | + * @readonly |
| 38 | + */ |
| 39 | + private ReflectionResolver $reflectionResolver; |
| 40 | + public function __construct(TypeComparator $typeComparator, StaticTypeMapper $staticTypeMapper, ReflectionResolver $reflectionResolver) |
| 41 | + { |
| 42 | + $this->typeComparator = $typeComparator; |
| 43 | + $this->staticTypeMapper = $staticTypeMapper; |
| 44 | + $this->reflectionResolver = $reflectionResolver; |
| 45 | + } |
| 46 | + public function getRuleDefinition() : RuleDefinition |
| 47 | + { |
| 48 | + return new RuleDefinition('Applies type hints to array_map closures', [new CodeSample(<<<'CODE_SAMPLE' |
| 49 | +array_reduce($strings, function ($carry, $value, $key): string { |
| 50 | + return $carry . $value; |
| 51 | +}, $initialString); |
| 52 | +CODE_SAMPLE |
| 53 | +, <<<'CODE_SAMPLE' |
| 54 | +array_reduce($strings, function (string $carry, string $value): string { |
| 55 | + return $carry . $value; |
| 56 | +}, $initialString); |
| 57 | +CODE_SAMPLE |
| 58 | +)]); |
| 59 | + } |
| 60 | + public function getNodeTypes() : array |
| 61 | + { |
| 62 | + return [FuncCall::class]; |
| 63 | + } |
| 64 | + /** |
| 65 | + * @param FuncCall $node |
| 66 | + */ |
| 67 | + public function refactor(Node $node) : ?Node |
| 68 | + { |
| 69 | + if ($node->isFirstClassCallable()) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + if (!$this->isName($node, 'array_reduce')) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + $funcReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); |
| 76 | + if (!$funcReflection instanceof NativeFunctionReflection) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + $args = $node->getArgs(); |
| 80 | + if (!isset($args[1]) || !$args[1]->value instanceof Closure) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + $closureType = $this->getType($args[1]->value); |
| 84 | + if (!$closureType instanceof ClosureType) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + $carryType = $closureType->getReturnType(); |
| 88 | + if (isset($args[2])) { |
| 89 | + $carryType = $this->combineTypes([$this->getType($args[2]->value), $carryType]); |
| 90 | + } |
| 91 | + $type = $this->getType($args[0]->value); |
| 92 | + $valueType = $type->getIterableValueType(); |
| 93 | + if ($this->updateClosureWithTypes($args[1]->value, $valueType, $carryType)) { |
| 94 | + return $node; |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + private function updateClosureWithTypes(Closure $closure, ?Type $valueType, ?Type $carryType) : bool |
| 99 | + { |
| 100 | + $changes = \false; |
| 101 | + $carryParam = $closure->params[0] ?? null; |
| 102 | + $valueParam = $closure->params[1] ?? null; |
| 103 | + if ($valueParam instanceof Param && $valueType instanceof Type && $this->refactorParameter($valueParam, $valueType)) { |
| 104 | + $changes = \true; |
| 105 | + } |
| 106 | + if ($carryParam instanceof Param && $carryType instanceof Type && $this->refactorParameter($carryParam, $carryType)) { |
| 107 | + return \true; |
| 108 | + } |
| 109 | + return $changes; |
| 110 | + } |
| 111 | + private function refactorParameter(Param $param, Type $type) : bool |
| 112 | + { |
| 113 | + if ($type instanceof MixedType) { |
| 114 | + return \false; |
| 115 | + } |
| 116 | + // already set → no change |
| 117 | + if ($param->type instanceof Node) { |
| 118 | + $currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type); |
| 119 | + if ($this->typeComparator->areTypesEqual($currentParamType, $type)) { |
| 120 | + return \false; |
| 121 | + } |
| 122 | + } |
| 123 | + $paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM); |
| 124 | + if (!$paramTypeNode instanceof Node) { |
| 125 | + return \false; |
| 126 | + } |
| 127 | + $param->type = $paramTypeNode; |
| 128 | + return \true; |
| 129 | + } |
| 130 | + /** |
| 131 | + * @param Type[] $types |
| 132 | + */ |
| 133 | + private function combineTypes(array $types) : ?Type |
| 134 | + { |
| 135 | + if ($types === []) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + $types = \array_reduce($types, function (array $types, Type $type) : array { |
| 139 | + foreach ($types as $previousType) { |
| 140 | + if ($this->typeComparator->areTypesEqual($type, $previousType)) { |
| 141 | + return $types; |
| 142 | + } |
| 143 | + } |
| 144 | + $types[] = $type; |
| 145 | + return $types; |
| 146 | + }, []); |
| 147 | + if (\count($types) === 1) { |
| 148 | + return $types[0]; |
| 149 | + } |
| 150 | + return new UnionType(UnionTypeHelper::sortTypes($types)); |
| 151 | + } |
| 152 | +} |
0 commit comments