Skip to content

Commit

Permalink
Backport/2.x generator property fix (#30)
Browse files Browse the repository at this point in the history
* #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]>
  • Loading branch information
3 people authored Dec 8, 2021
1 parent 0d6e73b commit 1111c9a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Parser/TokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,23 @@ public function getProperties(string $classPath): array
*/
public function getPropertyClass(ReflectionProperty $property, bool $ignorePrimitive = true)
{
// Get the content of the @var annotation
if (preg_match('/@var\s+([^\s]+)/', (string) $property->getDocComment(), $matches)) {
list(, $type) = $matches;
} else {
return null;
$type = null;
$phpVersion = false === phpversion() ? '7.0.0' : phpversion();
// Get is explicit type decralation if possible
if (version_compare($phpVersion, '7.4.0', '>=') && null !== $property->getType()) {
$reflectionType = $property->getType();

if ($reflectionType instanceof \ReflectionNamedType) {
$type = $reflectionType->getName();
}
}

if (is_null($type)) { // Try get the content of the @var annotation
if (preg_match('/@var\s+([^\s]+)/', (string) $property->getDocComment(), $matches)) {
list(, $type) = $matches;
} else {
return null;
}
}

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

0 comments on commit 1111c9a

Please sign in to comment.