Skip to content

Commit 63da051

Browse files
phil-nelsonfelixfbecker
authored andcommitted
fix(DefinitionResolver): fix methods with self return type (#550)
1 parent 9eea26d commit 63da051

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/DefinitionResolver.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ public function resolveExpressionNodeToType($expr)
729729
foreach ($classDef->getAncestorDefinitions($this->index, true) as $fqn => $def) {
730730
$def = $this->index->getDefinition($fqn . $add);
731731
if ($def !== null) {
732-
if ($def->type instanceof Types\This) {
732+
if ($def->type instanceof Types\This || $def->type instanceof Types\Self_) {
733733
return new Types\Object_(new Fqsen('\\' . $classFqn));
734734
}
735735
return $def->type;
@@ -1090,6 +1090,12 @@ public function getTypeFromNode($node)
10901090
if ($node->returnType instanceof PhpParser\Token) {
10911091
// Resolve a string like "bool" to a type object
10921092
return $this->typeResolver->resolve($node->returnType->getText($node->getFileContents()));
1093+
} elseif ($node->returnType->getResolvedName() === 'self') {
1094+
$classNode = $node->getFirstAncestor(Node\Statement\ClassDeclaration::class);
1095+
if ($classNode) {
1096+
$classFqn = (string)$classNode->getNamespacedName();
1097+
return new Types\Object_(new Fqsen('\\' . $classFqn));
1098+
}
10931099
}
10941100
return new Types\Object_(new Fqsen('\\' . (string)$node->returnType->getResolvedName()));
10951101
}

tests/Validation/cases/WithReturnTypehints.php.expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
},
7272
"containerName": "Fixtures\\Prophecy\\WithReturnTypehints"
7373
},
74-
"type__tostring": "\\self",
74+
"type__tostring": "\\Fixtures\\Prophecy\\WithReturnTypehints",
7575
"type": {},
7676
"declarationLine": "public function getSelf(): self {",
7777
"documentation": null,

tests/Validation/cases/staticMethodReturnType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public static function staticFoo(): FooClass {
55
return new FooClass();
66
}
77

8+
public static function staticSelf(): self { }
9+
810
public function bar() { }
911
}

tests/Validation/cases/staticMethodReturnType.php.expected.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@
5050
"parameters": []
5151
}
5252
},
53+
"FooClass::staticSelf()": {
54+
"fqn": "FooClass::staticSelf()",
55+
"extends": [],
56+
"isMember": true,
57+
"roamed": false,
58+
"isStatic": true,
59+
"canBeInstantiated": false,
60+
"symbolInformation": {
61+
"name": "staticSelf",
62+
"kind": 6,
63+
"location": {
64+
"uri": "./staticMethodReturnType.php"
65+
},
66+
"containerName": "FooClass"
67+
},
68+
"type__tostring": "\\FooClass",
69+
"type": {},
70+
"declarationLine": "public static function staticSelf(): self { }",
71+
"documentation": null,
72+
"signatureInformation": {
73+
"label": "()",
74+
"documentation": null,
75+
"parameters": []
76+
}
77+
},
5378
"FooClass->bar()": {
5479
"fqn": "FooClass->bar()",
5580
"extends": [],

0 commit comments

Comments
 (0)