|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\TypeDeclarationDocblocks\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Stmt\Class_; |
| 10 | +use PhpParser\Node\Stmt\Property; |
| 11 | +use PHPStan\Type\ArrayType; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\UnionType; |
| 14 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 15 | +use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Rector\TypeDeclarationDocblocks\NodeDocblockTypeDecorator; |
| 18 | +use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder; |
| 19 | +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\AddVarArrayDocblockFromDimFetchAssignRector\AddVarArrayDocblockFromDimFetchAssignRectorTest |
| 25 | + */ |
| 26 | +final class AddVarArrayDocblockFromDimFetchAssignRector extends AbstractRector |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly TypeFactory $typeFactory, |
| 30 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 31 | + private readonly UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer, |
| 32 | + private readonly NodeDocblockTypeDecorator $nodeDocblockTypeDecorator, |
| 33 | + private readonly ArrayDimFetchFinder $arrayDimFetchFinder, |
| 34 | + ) { |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public function getRuleDefinition(): RuleDefinition |
| 39 | + { |
| 40 | + return new RuleDefinition('Add @param array docblock if array_map is used on the parameter', [ |
| 41 | + new CodeSample( |
| 42 | + <<<'CODE_SAMPLE' |
| 43 | +final class SomeClass |
| 44 | +{ |
| 45 | + private array $items = []; |
| 46 | +
|
| 47 | + public function run() |
| 48 | + { |
| 49 | + $this->items[] = [ |
| 50 | + 'name' => 'John', |
| 51 | + ]; |
| 52 | + } |
| 53 | +} |
| 54 | +CODE_SAMPLE |
| 55 | + , |
| 56 | + <<<'CODE_SAMPLE' |
| 57 | +final class SomeClass |
| 58 | +{ |
| 59 | + /** |
| 60 | + * @var array<array<string, string>> |
| 61 | + */ |
| 62 | + private array $items = []; |
| 63 | +
|
| 64 | + public function run() |
| 65 | + { |
| 66 | + $this->items[] = [ |
| 67 | + 'name' => 'John', |
| 68 | + ]; |
| 69 | + } |
| 70 | +} |
| 71 | +CODE_SAMPLE |
| 72 | + ), |
| 73 | + ]); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return array<class-string<Node>> |
| 78 | + */ |
| 79 | + public function getNodeTypes(): array |
| 80 | + { |
| 81 | + return [Class_::class]; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param Class_ $node |
| 86 | + */ |
| 87 | + public function refactor(Node $node): ?Node |
| 88 | + { |
| 89 | + $hasChanged = false; |
| 90 | + |
| 91 | + foreach ($node->getProperties() as $property) { |
| 92 | + if (! $this->isPropertyTypeArray($property)) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); |
| 97 | + |
| 98 | + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($propertyPhpDocInfo->getVarTagValueNode())) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $propertyName = $this->getName($property); |
| 103 | + |
| 104 | + $assignedExprs = $this->arrayDimFetchFinder->findDimFetchAssignToPropertyName($node, $propertyName); |
| 105 | + |
| 106 | + $assignedExprTypes = []; |
| 107 | + foreach ($assignedExprs as $assignedExpr) { |
| 108 | + $assignedExprTypes[] = $this->getType($assignedExpr); |
| 109 | + } |
| 110 | + |
| 111 | + // nothing to add |
| 112 | + if ($assignedExprTypes === []) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $uniqueGeneralizedUnionTypes = $this->typeFactory->uniquateTypes($assignedExprTypes); |
| 117 | + |
| 118 | + if (count($uniqueGeneralizedUnionTypes) > 1) { |
| 119 | + $generalizedUnionedTypes = new UnionType($uniqueGeneralizedUnionTypes); |
| 120 | + } else { |
| 121 | + $generalizedUnionedTypes = $uniqueGeneralizedUnionTypes[0]; |
| 122 | + } |
| 123 | + |
| 124 | + $arrayReturnType = new ArrayType(new MixedType(), $generalizedUnionedTypes); |
| 125 | + |
| 126 | + $hasPropertyChanged = $this->nodeDocblockTypeDecorator->decorateGenericIterableVarType( |
| 127 | + $arrayReturnType, |
| 128 | + $propertyPhpDocInfo, |
| 129 | + $property |
| 130 | + ); |
| 131 | + |
| 132 | + if ($hasPropertyChanged === false) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + $hasChanged = true; |
| 137 | + } |
| 138 | + |
| 139 | + if (! $hasChanged) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + return $node; |
| 144 | + } |
| 145 | + |
| 146 | + private function isPropertyTypeArray(Property $property): bool |
| 147 | + { |
| 148 | + if (! $property->type instanceof Identifier) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + return $this->isName($property->type, 'array'); |
| 153 | + } |
| 154 | +} |
0 commit comments