|
4 | 4 |
|
5 | 5 | use Iterator;
|
6 | 6 | use IteratorAggregate;
|
7 |
| -use phpDocumentor\Reflection\DocBlock\Tags\Generic; |
| 7 | +use phpDocumentor\Reflection\DocBlock\Tags\Extends_; |
| 8 | +use phpDocumentor\Reflection\DocBlock\Tags\Template; |
8 | 9 | use phpDocumentor\Reflection\DocBlockFactory;
|
9 | 10 | use phpDocumentor\Reflection\TypeResolver;
|
| 11 | +use phpDocumentor\Reflection\Types\Collection; |
10 | 12 | use phpDocumentor\Reflection\Types\Context;
|
11 | 13 | use ReflectionClass;
|
12 | 14 | use Spatie\LaravelData\Data;
|
@@ -96,44 +98,39 @@ protected function getCollectionReturnType(ReflectionClass $class): ?array
|
96 | 98 | $docBlock = $docBlockFactory->create($docComment, $this->context);
|
97 | 99 |
|
98 | 100 | $templateTypes = [];
|
99 |
| - $keyType = null; |
100 |
| - $valueType = null; |
101 | 101 |
|
102 | 102 | foreach ($docBlock->getTags() as $tag) {
|
103 |
| - if (! $tag instanceof Generic) { |
| 103 | + if ($tag instanceof Template) { |
| 104 | + $templateName = $this->resolve($tag->getTemplateName()); |
| 105 | + $bound = $this->resolve((string) $tag->getBound()); |
| 106 | + $templateTypes[$templateName] = $bound; |
| 107 | + |
104 | 108 | continue;
|
105 | 109 | }
|
106 | 110 |
|
107 |
| - if ($tag->getName() === 'template') { |
108 |
| - $description = $tag->getDescription(); |
109 |
| - |
110 |
| - if (preg_match('/^(\w+)\s+of\s+([^\s]+)/', $description, $matches)) { |
111 |
| - $templateTypes[$matches[1]] = $this->resolve($matches[2]); |
| 111 | + if ($tag instanceof Extends_) { |
| 112 | + $type = $tag->getType(); |
| 113 | + if (! $type instanceof Collection) { |
| 114 | + continue; |
112 | 115 | }
|
113 | 116 |
|
114 |
| - continue; |
115 |
| - } |
| 117 | + $keyType = $this->resolve((string) $type->getKeyType()); |
| 118 | + $valueType = $this->resolve((string) $type->getValueType()); |
116 | 119 |
|
117 |
| - if ($tag->getName() === 'extends') { |
118 |
| - $description = $tag->getDescription(); |
119 |
| - |
120 |
| - if (preg_match('/<\s*([^,\s]+)?\s*(?:,\s*([^>\s]+))?\s*>/', $description, $matches)) { |
121 |
| - if (count($matches) === 3) { |
122 |
| - $keyType = $templateTypes[$matches[1]] ?? $this->resolve($matches[1]); |
123 |
| - $valueType = $templateTypes[$matches[2]] ?? $this->resolve($matches[2]); |
124 |
| - } else { |
125 |
| - $keyType = null; |
126 |
| - $valueType = $templateTypes[$matches[1]] ?? $this->resolve($matches[1]); |
127 |
| - } |
128 |
| - |
129 |
| - $keyType = $keyType ? explode('|', $keyType)[0] : null; |
130 |
| - $valueType = explode('|', $valueType)[0]; |
131 |
| - |
132 |
| - return [ |
133 |
| - 'keyType' => $keyType, |
134 |
| - 'valueType' => $valueType, |
135 |
| - ]; |
| 120 | + if ($keyType === 'string|int') { |
| 121 | + $keyType = 'array-key'; |
136 | 122 | }
|
| 123 | + |
| 124 | + $keyType = $templateTypes[$keyType] ?? $keyType; |
| 125 | + $valueType = $templateTypes[$valueType] ?? $valueType; |
| 126 | + |
| 127 | + $keyType = $keyType ? explode('|', $keyType)[0] : null; |
| 128 | + $valueType = explode('|', $valueType)[0]; |
| 129 | + |
| 130 | + return [ |
| 131 | + 'keyType' => $keyType, |
| 132 | + 'valueType' => $valueType, |
| 133 | + ]; |
137 | 134 | }
|
138 | 135 | }
|
139 | 136 |
|
|
0 commit comments