Skip to content

Commit 1111c9a

Browse files
nick-zhHubbitusPavel Alexeev
authored
Backport/2.x generator property fix (#30)
* #24 For PHP version >= 7.4.0 look for the properties explicit type declaration first (#25) * #24 For PHP version >= 7.4.0 look for the properties explicit type declaration first * Apply suggestions from code review Co-authored-by: Pavel Alexeev <[email protected]> Co-authored-by: Nick <[email protected]> * fix stan (#27) * fix import Co-authored-by: Pavel Alexeev aka Pahan-Hubbitus <[email protected]> Co-authored-by: Pavel Alexeev <[email protected]>
1 parent 0d6e73b commit 1111c9a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Parser/TokenParser.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,23 @@ public function getProperties(string $classPath): array
199199
*/
200200
public function getPropertyClass(ReflectionProperty $property, bool $ignorePrimitive = true)
201201
{
202-
// Get the content of the @var annotation
203-
if (preg_match('/@var\s+([^\s]+)/', (string) $property->getDocComment(), $matches)) {
204-
list(, $type) = $matches;
205-
} else {
206-
return null;
202+
$type = null;
203+
$phpVersion = false === phpversion() ? '7.0.0' : phpversion();
204+
// Get is explicit type decralation if possible
205+
if (version_compare($phpVersion, '7.4.0', '>=') && null !== $property->getType()) {
206+
$reflectionType = $property->getType();
207+
208+
if ($reflectionType instanceof \ReflectionNamedType) {
209+
$type = $reflectionType->getName();
210+
}
211+
}
212+
213+
if (is_null($type)) { // Try get the content of the @var annotation
214+
if (preg_match('/@var\s+([^\s]+)/', (string) $property->getDocComment(), $matches)) {
215+
list(, $type) = $matches;
216+
} else {
217+
return null;
218+
}
207219
}
208220

209221
$types = explode('|', $this->replaceTypeStrings($type));

0 commit comments

Comments
 (0)