Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 6, 2024
1 parent ec8e11c commit 8d326eb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function getMethod(ClassReflection $classReflection, string $methodName):

private function findMethod(ClassReflection $classReflection, string $methodName): ?MethodReflection
{
if (!$classReflection->isInterface()) {
return null;
}

$extendsTags = $classReflection->getRequireExtendsTags();
foreach ($extendsTags as $extendsTag) {
$type = $extendsTag->getType();
Expand All @@ -55,14 +59,12 @@ private function findMethod(ClassReflection $classReflection, string $methodName
return $method;
}

$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
$method = $this->findMethod($parentClass, $methodName);
$interfaces = $classReflection->getInterfaces();
foreach ($interfaces as $interface) {
$method = $this->findMethod($interface, $methodName);
if ($method !== null) {
return $method;
}

$parentClass = $parentClass->getParentClass();
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa

private function findProperty(ClassReflection $classReflection, string $propertyName): ?PropertyReflection
{
// XXX require-implements cannot grant access to properties?
if (!$classReflection->isInterface()) {
return null;
}

$requireExtendsTags = $classReflection->getRequireExtendsTags();
foreach ($requireExtendsTags as $requireExtendsTag) {
$type = $requireExtendsTag->getType();
Expand All @@ -55,14 +58,12 @@ private function findProperty(ClassReflection $classReflection, string $property
return $property;
}

$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
$property = $this->findProperty($parentClass, $propertyName);
$interfaces = $classReflection->getInterfaces();
foreach ($interfaces as $interface) {
$property = $this->findProperty($interface, $propertyName);
if ($property !== null) {
return $property;
}

$parentClass = $parentClass->getParentClass();
}

return null;
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10302-interface-extends.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ function test(SampleInterface $test): void

assertType('int', $test->doFoo());
}

function testExtendedInterface(AnotherInterface $test): void
{
assertType('int', $test->x);
assertType('string', $test->y);
assertType('array', $test->z);

assertType('int', $test->doFoo());
}

interface AnotherInterface extends SampleInterface
{
}

class SomeSubClass extends SomeClass {}

class ValidClass extends SomeClass implements SampleInterface {}

class ValidSubClass extends SomeSubClass implements SampleInterface {}

class InvalidClass implements SampleInterface {}
33 changes: 29 additions & 4 deletions tests/PHPStan/Analyser/data/bug-10302-trait-extends.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
*/
trait myTrait
{
function test(): void
{
assertType('int', $this->x);
assertType('string', $this->y);
assertType('*ERROR*', $this->z);
}
}

class SomeClass {
use myTrait;
/**
* @phpstan-require-extends SomeClass
*/
trait anotherTrait
{
}

public int $x;
protected string $y;
class SomeClass {
public int $x = 1;
protected string $y = 'foo';
private array $z = [];
}

Expand All @@ -25,3 +36,17 @@ function test(SomeClass $test): void
assertType('string', $test->y);
assertType('array', $test->z);
}

class SomeSubClass extends SomeClass {}

class ValidClass extends SomeClass {
use myTrait;
}

class ValidSubClass extends SomeSubClass {
use myTrait;
}

class InvalidClass {
use anotherTrait;
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10302-trait-implements.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
trait myTrait
{
protected function doIt() {
$this->foo();
$this->doesNotExist();
}
}

interface SomeInterface
Expand Down Expand Up @@ -37,3 +41,14 @@ function test(SomeClass $test): void

assertType('string', $test->foo());
}

class ValidImplements implements SomeInterface {
public function foo(): string
{
return "hallo";
}
}

class InvalidClass {
use myTrait;
}

0 comments on commit 8d326eb

Please sign in to comment.