Skip to content

Commit 031b7b0

Browse files
committed
Fix #309
1 parent 763a0b6 commit 031b7b0

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/collector/backend/parser/UnitCollectingVisitor.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,27 @@ private function processMethodReturnType(MethodObject $method, $returnType) {
272272
}
273273

274274
if ($returnType instanceof \PhpParser\Node\NullableType) {
275-
$returnTypeObject = $method->setReturnType($returnType->type);
275+
if ((string)$returnType->type === 'self') {
276+
$returnTypeObject = $method->setReturnType($this->unit->getName());
277+
} else {
278+
$returnTypeObject = $method->setReturnType($returnType->type);
279+
}
276280
$returnTypeObject->setNullable(true);
277281
return;
278282
}
279-
throw new ParseErrorException("Unexpected return type definition", ParseErrorException::UnexpectedExpr);
283+
284+
if ($returnType instanceof \PhpParser\Node\Name) {
285+
$returnTypeObject = $method->setReturnType(
286+
$this->unit->getName()
287+
);
288+
$returnTypeObject->setNullable(false);
289+
return;
290+
}
291+
292+
throw new ParseErrorException(
293+
sprintf("Unexpected return type definition %s",
294+
get_class($returnType)
295+
),ParseErrorException::UnexpectedExpr);
280296
}
281297

282298
private function processInlineComments(MethodObject $method, array $stmts) {

tests/data/issue309/src/foo.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
namespace some\test;
3+
4+
class foo {
5+
6+
public static function createFromFoo(): self {
7+
return new self;
8+
}
9+
10+
public static function createFromBar(): ?self {
11+
return new self;
12+
}
13+
14+
}

tests/data/issue309/test.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpdox xmlns="http://xml.phpdox.net/config" silent="false">
3+
4+
<project name="phpDox-issue300" source="${basedir}/src" workdir="${basedir}/xml">
5+
6+
<collector publiconly="false" backend="parser" />
7+
8+
<generator output="${basedir}/docs">
9+
<build engine="html" enabled="true" output="html" />
10+
<build engine="xml" enabled="true" output="xml" />
11+
</generator>
12+
13+
</project>
14+
15+
</phpdox>

0 commit comments

Comments
 (0)