Skip to content

Commit a172f5b

Browse files
committed
Require reflection-docblock 5.5 or greater so that tag type is consistent
1 parent 44e0006 commit a172f5b

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": "^8.1",
2020
"illuminate/contracts": "^10.0|^11.0",
2121
"phpdocumentor/reflection": "^6.0",
22+
"phpdocumentor/reflection-docblock": "^5.5",
2223
"spatie/laravel-package-tools": "^1.9.0",
2324
"spatie/php-structure-discoverer": "^2.0"
2425
},

src/Support/Annotations/CollectionAnnotationReader.php

+27-30
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Iterator;
66
use IteratorAggregate;
7-
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
7+
use phpDocumentor\Reflection\DocBlock\Tags\Extends_;
8+
use phpDocumentor\Reflection\DocBlock\Tags\Template;
89
use phpDocumentor\Reflection\DocBlockFactory;
910
use phpDocumentor\Reflection\TypeResolver;
11+
use phpDocumentor\Reflection\Types\Collection;
1012
use phpDocumentor\Reflection\Types\Context;
1113
use ReflectionClass;
1214
use Spatie\LaravelData\Data;
@@ -96,44 +98,39 @@ protected function getCollectionReturnType(ReflectionClass $class): ?array
9698
$docBlock = $docBlockFactory->create($docComment, $this->context);
9799

98100
$templateTypes = [];
99-
$keyType = null;
100-
$valueType = null;
101101

102102
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+
104108
continue;
105109
}
106110

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;
112115
}
113116

114-
continue;
115-
}
117+
$keyType = $this->resolve((string) $type->getKeyType());
118+
$valueType = $this->resolve((string) $type->getValueType());
116119

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';
136122
}
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+
];
137134
}
138135
}
139136

0 commit comments

Comments
 (0)