|
5 | 5 | namespace Rector\Doctrine\TypedCollections\Rector\ClassMethod;
|
6 | 6 |
|
7 | 7 | use PhpParser\Node;
|
| 8 | +use PhpParser\Node\NullableType; |
8 | 9 | use PhpParser\Node\Stmt\ClassMethod;
|
| 10 | +use PhpParser\Node\UnionType; |
9 | 11 | use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
| 12 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
10 | 13 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
11 | 14 | use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
12 | 15 | use Rector\Doctrine\Enum\DoctrineClass;
|
@@ -74,26 +77,97 @@ public function getNodeTypes(): array
|
74 | 77 | */
|
75 | 78 | public function refactor(Node $node): ?ClassMethod
|
76 | 79 | {
|
77 |
| - $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
78 |
| - $returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue(); |
| 80 | + $hasChanged = false; |
| 81 | + if ($this->refactorReturnDocblockTag($node)) { |
| 82 | + $hasChanged = true; |
| 83 | + } |
79 | 84 |
|
80 |
| - if (! $returnTagValueNode instanceof ReturnTagValueNode) { |
| 85 | + if ($this->refactorNativeReturn($node)) { |
| 86 | + $hasChanged = true; |
| 87 | + } |
| 88 | + |
| 89 | + if (! $hasChanged) { |
81 | 90 | return null;
|
82 | 91 | }
|
83 | 92 |
|
84 |
| - if ($node->returnType !== null && $this->isName($node->returnType, DoctrineClass::COLLECTION)) { |
85 |
| - $hasNativeCollectionType = true; |
86 |
| - } else { |
87 |
| - $hasNativeCollectionType = false; |
| 93 | + return $node; |
| 94 | + } |
| 95 | + |
| 96 | + private function refactorReturnDocblockTag(ClassMethod $classMethod): bool |
| 97 | + { |
| 98 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod); |
| 99 | + if (! $phpDocInfo instanceof PhpDocInfo) { |
| 100 | + return false; |
88 | 101 | }
|
89 | 102 |
|
| 103 | + $returnTagValueNode = $phpDocInfo->getReturnTagValue(); |
| 104 | + if (! $returnTagValueNode instanceof ReturnTagValueNode) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + $hasNativeCollectionType = $this->hasNativeCollectionType($classMethod); |
| 109 | + |
90 | 110 | $hasChanged = $this->unionCollectionTagValueNodeNarrower->narrow($returnTagValueNode, $hasNativeCollectionType);
|
91 | 111 | if ($hasChanged === false) {
|
92 |
| - return null; |
| 112 | + return false; |
93 | 113 | }
|
94 | 114 |
|
95 |
| - $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 115 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); |
96 | 116 |
|
97 |
| - return $node; |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + private function hasNativeCollectionType(ClassMethod $classMethod): bool |
| 121 | + { |
| 122 | + if (! $classMethod->returnType instanceof Node) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + return $this->isName($classMethod->returnType, DoctrineClass::COLLECTION); |
| 127 | + } |
| 128 | + |
| 129 | + private function refactorNativeReturn(ClassMethod $classMethod): bool |
| 130 | + { |
| 131 | + if (! $classMethod->returnType instanceof Node) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if ($classMethod->returnType instanceof NullableType && $this->isName( |
| 136 | + $classMethod->returnType->type, |
| 137 | + DoctrineClass::COLLECTION |
| 138 | + )) { |
| 139 | + // unwrap nullable type |
| 140 | + $classMethod->returnType = $classMethod->returnType->type; |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + if (! $classMethod->returnType instanceof UnionType) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + $unionType = $classMethod->returnType; |
| 149 | + if (! $this->hasNativeReturnCollectionType($unionType)) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // remove null from union type |
| 154 | + foreach ($unionType->types as $key => $unionedType) { |
| 155 | + if ($this->isName($unionedType, 'null')) { |
| 156 | + unset($unionType->types[$key]); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + private function hasNativeReturnCollectionType(UnionType $unionType): bool |
| 164 | + { |
| 165 | + foreach ($unionType->types as $unionedType) { |
| 166 | + if ($this->isName($unionedType, DoctrineClass::COLLECTION)) { |
| 167 | + return true; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return false; |
98 | 172 | }
|
99 | 173 | }
|
0 commit comments