Skip to content

Commit

Permalink
Support for self in return typehint overriden by static in phpDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 8, 2016
1 parent 2afd095 commit 93e868d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public function getReturnType(): Type
$returnType->isNullable() || $this->phpDocReturnType->isNullable()
);
}
if ($returnType->accepts($this->phpDocReturnType)) {
return $this->phpDocReturnType;
}
}

$this->returnType = $returnType;
Expand Down
15 changes: 12 additions & 3 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ public function accepts(Type $type): bool
return true;
}

if ($type instanceof StaticType) {
return $this->checkSubclassAcceptability($type->getBaseClass());
}

if ($type->getClass() === null) {
return false;
}

if ($this->getClass() === $type->getClass()) {
return $this->checkSubclassAcceptability($type->getClass());
}

private function checkSubclassAcceptability(string $thatClass): bool
{
if ($this->getClass() === $thatClass) {
return true;
}

if (!$this->exists($this->getClass()) || !$this->exists($type->getClass())) {
if (!$this->exists($this->getClass()) || !$this->exists($thatClass)) {
return false;
}

$thisReflection = new \ReflectionClass($this->getClass());
$thatReflection = new \ReflectionClass($type->getClass());
$thatReflection = new \ReflectionClass($thatClass);

if ($thisReflection->isInterface() && $thatReflection->isInterface()) {
return $thatReflection->implementsInterface($this->getClass());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function getClass()
return null;
}

public function getBaseClass(): string
{
return $this->baseClass;
}

public function isNullable(): bool
{
return $this->nullable;
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,24 @@ public function dataTypeFromMethodPhpDocs(): array
'MethodPhpDocsNamespace\FooParent',
'parent::doLorem()',
],
[
ObjectType::class,
false,
'MethodPhpDocsNamespace\Foo',
'$this->doLorem()',
],
[
ObjectType::class,
false,
'MethodPhpDocsNamespace\FooParent',
'parent::doIpsum()',
],
[
ObjectType::class,
false,
'MethodPhpDocsNamespace\Foo',
'$this->doIpsum()',
],
];
}

Expand Down
18 changes: 0 additions & 18 deletions tests/PHPStan/Analyser/data/methodPhpDocs-defined.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,3 @@ public function doFluentNullable()
}

}

class Baz extends Bar
{

}

class FooParent
{

/**
* @return static
*/
public function doLorem()
{

}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/methodPhpDocs-defined2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace MethodPhpDocsNamespace;

use SomeNamespace\Amet as Dolor;

class FooParent
{

/**
* @return static
*/
public function doLorem()
{

}

/**
* @return static
*/
public function doIpsum(): self
{

}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/methodPhpDocs-defined3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MethodPhpDocsNamespace;

use SomeNamespace\Amet as Dolor;

class Baz extends Bar
{

}

1 comment on commit 93e868d

@pepakriz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.