|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\Array_; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\Variable; |
| 10 | +use PhpParser\Node\Stmt\ClassMethod; |
| 11 | +use PhpParser\Node\Stmt\Expression; |
| 12 | +use PhpParser\Node\Stmt\Return_; |
| 13 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\UnionType; |
| 16 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 17 | +use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; |
| 20 | +use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; |
| 21 | +use Rector\TypeDeclarationDocblocks\TypeResolver\ConstantArrayTypeGeneralizer; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | +/** |
| 25 | + * @see \Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForDimFetchArrayFromAssignsRector\AddReturnDocblockForDimFetchArrayFromAssignsRectorTest |
| 26 | + */ |
| 27 | +final class AddReturnDocblockForDimFetchArrayFromAssignsRector extends AbstractRector |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @readonly |
| 31 | + */ |
| 32 | + private PhpDocInfoFactory $phpDocInfoFactory; |
| 33 | + /** |
| 34 | + * @readonly |
| 35 | + */ |
| 36 | + private UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer; |
| 37 | + /** |
| 38 | + * @readonly |
| 39 | + */ |
| 40 | + private ReturnNodeFinder $returnNodeFinder; |
| 41 | + /** |
| 42 | + * @readonly |
| 43 | + */ |
| 44 | + private ConstantArrayTypeGeneralizer $constantArrayTypeGeneralizer; |
| 45 | + /** |
| 46 | + * @readonly |
| 47 | + */ |
| 48 | + private PhpDocTypeChanger $phpDocTypeChanger; |
| 49 | + public function __construct(PhpDocInfoFactory $phpDocInfoFactory, UsefulArrayTagNodeAnalyzer $usefulArrayTagNodeAnalyzer, ReturnNodeFinder $returnNodeFinder, ConstantArrayTypeGeneralizer $constantArrayTypeGeneralizer, PhpDocTypeChanger $phpDocTypeChanger) |
| 50 | + { |
| 51 | + $this->phpDocInfoFactory = $phpDocInfoFactory; |
| 52 | + $this->usefulArrayTagNodeAnalyzer = $usefulArrayTagNodeAnalyzer; |
| 53 | + $this->returnNodeFinder = $returnNodeFinder; |
| 54 | + $this->constantArrayTypeGeneralizer = $constantArrayTypeGeneralizer; |
| 55 | + $this->phpDocTypeChanger = $phpDocTypeChanger; |
| 56 | + } |
| 57 | + public function getRuleDefinition(): RuleDefinition |
| 58 | + { |
| 59 | + return new RuleDefinition('Add @return docblock for methods returning array from dim fetch of assigned arrays', [new CodeSample(<<<'CODE_SAMPLE' |
| 60 | +final class SomeClass |
| 61 | +{ |
| 62 | + public function toArray(): array |
| 63 | + { |
| 64 | + $items = []; |
| 65 | +
|
| 66 | + if (mt_rand(0, 1)) { |
| 67 | + $items['key'] = 'value'; |
| 68 | + } |
| 69 | +
|
| 70 | + if (mt_rand(0, 1)) { |
| 71 | + $items['another_key'] = 'another_value'; |
| 72 | + } |
| 73 | +
|
| 74 | + return $items; |
| 75 | + } |
| 76 | +} |
| 77 | +CODE_SAMPLE |
| 78 | +, <<<'CODE_SAMPLE' |
| 79 | +final class SomeClass |
| 80 | +{ |
| 81 | + /** |
| 82 | + * @return array<string, string> |
| 83 | + */ |
| 84 | + public function toArray() |
| 85 | + { |
| 86 | + $items = []; |
| 87 | +
|
| 88 | + if (mt_rand(0, 1)) { |
| 89 | + $items['key'] = 'value'; |
| 90 | + } |
| 91 | +
|
| 92 | + if (mt_rand(0, 1)) { |
| 93 | + $items['another_key'] = 'another_value'; |
| 94 | + } |
| 95 | +
|
| 96 | + return $items; |
| 97 | + } |
| 98 | +} |
| 99 | +CODE_SAMPLE |
| 100 | +)]); |
| 101 | + } |
| 102 | + public function getNodeTypes(): array |
| 103 | + { |
| 104 | + return [ClassMethod::class]; |
| 105 | + } |
| 106 | + /** |
| 107 | + * @param ClassMethod $node |
| 108 | + */ |
| 109 | + public function refactor(Node $node): ?ClassMethod |
| 110 | + { |
| 111 | + if ($node->stmts === null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
| 115 | + if ($this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($phpDocInfo->getReturnTagValue())) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + $soleReturn = $this->returnNodeFinder->findOnlyReturnWithExpr($node); |
| 119 | + if (!$soleReturn instanceof Return_) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + // only variable |
| 123 | + if (!$soleReturn->expr instanceof Variable) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + // @todo check type here |
| 127 | + $returnedExprType = $this->getType($soleReturn->expr); |
| 128 | + if (!$this->isConstantArrayType($returnedExprType)) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + // find stmts with $item = []; |
| 132 | + $returnedVariableName = $this->getName($soleReturn->expr); |
| 133 | + if (!is_string($returnedVariableName)) { |
| 134 | + return null; |
| 135 | + } |
| 136 | + if (!$this->isVariableInstantiated($node, $returnedVariableName)) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + if ($returnedExprType->getReferencedClasses() !== []) { |
| 140 | + // better handled by shared-interface/class rule, to avoid turning objects to mixed |
| 141 | + return null; |
| 142 | + } |
| 143 | + // conditional assign |
| 144 | + $genericUnionedTypeNodes = []; |
| 145 | + if ($returnedExprType instanceof UnionType) { |
| 146 | + foreach ($returnedExprType->getTypes() as $unionedType) { |
| 147 | + if ($unionedType instanceof ConstantArrayType) { |
| 148 | + // skip empty array |
| 149 | + if ($unionedType->getKeyTypes() === [] && $unionedType->getValueTypes() === []) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + $genericUnionedTypeNode = $this->constantArrayTypeGeneralizer->generalize($unionedType); |
| 153 | + $genericUnionedTypeNodes[] = $genericUnionedTypeNode; |
| 154 | + } |
| 155 | + } |
| 156 | + } else { |
| 157 | + /** @var ConstantArrayType $returnedExprType */ |
| 158 | + $genericTypeNode = $this->constantArrayTypeGeneralizer->generalize($returnedExprType); |
| 159 | + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $genericTypeNode); |
| 160 | + return $node; |
| 161 | + } |
| 162 | + // @todo handle multiple type nodes |
| 163 | + $this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $genericUnionedTypeNodes[0]); |
| 164 | + return $node; |
| 165 | + } |
| 166 | + private function isVariableInstantiated(ClassMethod $classMethod, string $returnedVariableName): bool |
| 167 | + { |
| 168 | + foreach ((array) $classMethod->stmts as $stmt) { |
| 169 | + if (!$stmt instanceof Expression) { |
| 170 | + continue; |
| 171 | + } |
| 172 | + if (!$stmt->expr instanceof Assign) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + $assign = $stmt->expr; |
| 176 | + if (!$assign->var instanceof Variable) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + if (!$this->isName($assign->var, $returnedVariableName)) { |
| 180 | + continue; |
| 181 | + } |
| 182 | + // must be array assignment |
| 183 | + if (!$assign->expr instanceof Array_) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + return \true; |
| 187 | + } |
| 188 | + return \false; |
| 189 | + } |
| 190 | + private function isConstantArrayType(Type $returnedExprType): bool |
| 191 | + { |
| 192 | + if ($returnedExprType instanceof UnionType) { |
| 193 | + foreach ($returnedExprType->getTypes() as $unionedType) { |
| 194 | + if (!$unionedType instanceof ConstantArrayType) { |
| 195 | + return \false; |
| 196 | + } |
| 197 | + } |
| 198 | + return \true; |
| 199 | + } |
| 200 | + return $returnedExprType instanceof ConstantArrayType; |
| 201 | + } |
| 202 | +} |
0 commit comments