Skip to content

Commit 9e50df8

Browse files
authored
Merge pull request #17 from liip/look-in-parent-class-for-imports
Look for imports also in parent classes and traits
2 parents 87c9be0 + 1df9168 commit 9e50df8

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
"psr/log": "^1"
2020
},
2121
"require-dev": {
22-
"phpstan/phpstan-shim": "^0.11.0",
23-
"phpstan/phpstan-phpunit": "^0.11",
24-
"phpunit/phpunit": "^7.5",
22+
"friendsofphp/php-cs-fixer": "v2.14.1",
2523
"jms/serializer": "^1.13 || ^2",
26-
"friendsofphp/php-cs-fixer": "v2.14.1"
24+
"phpstan/phpstan": "^0.12.71",
25+
"phpstan/phpstan-phpunit": "^0.12",
26+
"phpunit/phpunit": "^7.5"
2727
},
2828
"suggest": {
2929
"jms/serializer": "^1.13 || ^2"
@@ -41,8 +41,8 @@
4141
"scripts": {
4242
"fix-cs": "vendor/bin/php-cs-fixer fix -v",
4343
"cs-fixer": "vendor/bin/php-cs-fixer fix --dry-run --diff --diff-format udiff -v",
44-
"phpstan": "vendor/bin/phpstan analyse --no-progress --level 5 src/",
45-
"phpstan-tests": "vendor/bin/phpstan analyse --no-progress --level 1 -c phpstan.tests.neon tests/",
44+
"phpstan": "vendor/bin/phpstan analyse",
45+
"phpstan-tests": "vendor/bin/phpstan analyse -c phpstan.tests.neon",
4646
"phpstan-all": [
4747
"@phpstan",
4848
"@phpstan-tests"

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
parameters:
2+
level: 5
3+
paths:
4+
- src/
25
ignoreErrors:
36
# Allow JMS parser version 1
47
- '#has unknown class JMS\\Serializer\\TypeParser as its type#'

phpstan.tests.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# empty file to not use config for src that has ignore statements
1+
parameters:
2+
level: 1
3+
paths:
4+
- tests/

src/TypeParser/PhpTypeParser.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ public function parseAnnotationType(string $rawType, \ReflectionClass $reflClass
7171
*/
7272
public function parseReflectionType(\ReflectionType $reflType): PropertyType
7373
{
74-
return $this->createType($reflType->getName(), $reflType->allowsNull());
74+
if ($reflType instanceof \ReflectionNamedType) {
75+
return $this->createType($reflType->getName(), $reflType->allowsNull());
76+
}
77+
78+
throw new InvalidTypeException(sprintf('No type information found, got %s but expected %s', \ReflectionType::class, \ReflectionNamedType::class));
7579
}
7680

7781
private function createType(string $rawType, bool $nullable, \ReflectionClass $reflClass = null): PropertyType
@@ -118,9 +122,20 @@ private function resolveClass(string $className, \ReflectionClass $reflClass = n
118122
if (null !== $reflClass) {
119123
// resolve use statements of the class with the type information
120124
$lowerClassName = strtolower($className);
121-
$imports = $this->useStatementsParser->parseClass($reflClass);
122-
if (isset($imports[$lowerClassName])) {
123-
return $imports[$lowerClassName];
125+
126+
$reflCurrentClass = $reflClass;
127+
do {
128+
$imports = $this->useStatementsParser->parseClass($reflCurrentClass);
129+
if (isset($imports[$lowerClassName])) {
130+
return $imports[$lowerClassName];
131+
}
132+
} while (false !== ($reflCurrentClass = $reflCurrentClass->getParentClass()));
133+
134+
foreach ($reflClass->getTraits() as $reflTrait) {
135+
$imports = $this->useStatementsParser->parseClass($reflTrait);
136+
if (isset($imports[$lowerClassName])) {
137+
return $imports[$lowerClassName];
138+
}
124139
}
125140

126141
// the referenced class is expected to be in the same namespace

tests/ModelParser/JMSParserTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ public function testVirtualPropertyTypeExtendingPrimitive(): void
953953
*/
954954
public function foo(): ?string
955955
{
956+
return 'foo';
956957
}
957958
};
958959

@@ -977,6 +978,7 @@ public function testVirtualPropertyTypeExtendingDateTime(): void
977978
*/
978979
public function foo(): ?\DateTime
979980
{
981+
return new \DateTime();
980982
}
981983
};
982984

tests/TypeParser/PhpTypeParserTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,17 @@ public function provideReflectionTypes(): iterable
192192
$c = new class() {
193193
private function method1(): string
194194
{
195+
return '1';
195196
}
196197

197198
private function method2(): ?int
198199
{
200+
return 1;
199201
}
200202

201203
private function method3(): array
202204
{
205+
return [1];
203206
}
204207
};
205208
$reflClass = new \ReflectionClass(\get_class($c));

0 commit comments

Comments
 (0)