Skip to content

Commit

Permalink
fix(DefinitionResolver): fix methods with self return type (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-nelson authored and felixfbecker committed Dec 23, 2017
1 parent 9eea26d commit 63da051
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/DefinitionResolver.php
Expand Up @@ -729,7 +729,7 @@ public function resolveExpressionNodeToType($expr)
foreach ($classDef->getAncestorDefinitions($this->index, true) as $fqn => $def) {
$def = $this->index->getDefinition($fqn . $add);
if ($def !== null) {
if ($def->type instanceof Types\This) {
if ($def->type instanceof Types\This || $def->type instanceof Types\Self_) {
return new Types\Object_(new Fqsen('\\' . $classFqn));
}
return $def->type;
Expand Down Expand Up @@ -1090,6 +1090,12 @@ public function getTypeFromNode($node)
if ($node->returnType instanceof PhpParser\Token) {
// Resolve a string like "bool" to a type object
return $this->typeResolver->resolve($node->returnType->getText($node->getFileContents()));
} elseif ($node->returnType->getResolvedName() === 'self') {
$classNode = $node->getFirstAncestor(Node\Statement\ClassDeclaration::class);
if ($classNode) {
$classFqn = (string)$classNode->getNamespacedName();
return new Types\Object_(new Fqsen('\\' . $classFqn));
}
}
return new Types\Object_(new Fqsen('\\' . (string)$node->returnType->getResolvedName()));
}
Expand Down
Expand Up @@ -71,7 +71,7 @@
},
"containerName": "Fixtures\\Prophecy\\WithReturnTypehints"
},
"type__tostring": "\\self",
"type__tostring": "\\Fixtures\\Prophecy\\WithReturnTypehints",
"type": {},
"declarationLine": "public function getSelf(): self {",
"documentation": null,
Expand Down
2 changes: 2 additions & 0 deletions tests/Validation/cases/staticMethodReturnType.php
Expand Up @@ -5,5 +5,7 @@ public static function staticFoo(): FooClass {
return new FooClass();
}

public static function staticSelf(): self { }

public function bar() { }
}
25 changes: 25 additions & 0 deletions tests/Validation/cases/staticMethodReturnType.php.expected.json
Expand Up @@ -50,6 +50,31 @@
"parameters": []
}
},
"FooClass::staticSelf()": {
"fqn": "FooClass::staticSelf()",
"extends": [],
"isMember": true,
"roamed": false,
"isStatic": true,
"canBeInstantiated": false,
"symbolInformation": {
"name": "staticSelf",
"kind": 6,
"location": {
"uri": "./staticMethodReturnType.php"
},
"containerName": "FooClass"
},
"type__tostring": "\\FooClass",
"type": {},
"declarationLine": "public static function staticSelf(): self { }",
"documentation": null,
"signatureInformation": {
"label": "()",
"documentation": null,
"parameters": []
}
},
"FooClass->bar()": {
"fqn": "FooClass->bar()",
"extends": [],
Expand Down

0 comments on commit 63da051

Please sign in to comment.