Skip to content

Commit

Permalink
Fix overriding trait properties in PHPDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 18, 2020
1 parent 37c29d5 commit 55a3444
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function createProperty(
throw new \PHPStan\ShouldNotHappenException();
}

if ($hierarchyDistances[$annotationProperty->getDeclaringClass()->getName()] < $hierarchyDistances[$propertyReflection->getDeclaringClass()->getName()]) {
if ($hierarchyDistances[$annotationProperty->getDeclaringClass()->getName()] <= $hierarchyDistances[$propertyReflection->getDeclaringClass()->getName()]) {
return $annotationProperty;
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9834,6 +9834,11 @@ public function dataArrayShapeKeysStrings(): array
return $this->gatherAssertTypes(__DIR__ . '/data/array-shapes-keys-strings.php');
}

public function dataBug1216(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-1216.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -9871,6 +9876,7 @@ public function dataArrayShapeKeysStrings(): array
* @dataProvider dataIsNumeric
* @dataProvider dataBug3142
* @dataProvider dataArrayShapeKeysStrings
* @dataProvider dataBug1216
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/data/bug-1216.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Bug1216;

use function PHPStan\Analyser\assertType;

abstract class Foo
{
/**
* @var int
*/
protected $foo;
}

trait Bar
{
/**
* @var int
*/
protected $bar;

protected $untypedBar;
}

/**
* @property string $foo
* @property string $bar
* @property string $untypedBar
*/
class Baz extends Foo
{

public function __construct()
{
assertType('string', $this->foo);
assertType('string', $this->bar);
assertType('string', $this->untypedBar);
}

}

function (Baz $baz): void {
assertType('string', $baz->foo);
assertType('string', $baz->bar);
assertType('string', $baz->untypedBar);
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function dataProperties(): array
],
'conflictingAnnotationProperty' => [
'class' => \AnnotationsProperties\Bar::class,
'type' => \AnnotationsProperties\Bar::class,
'type' => \AnnotationsProperties\Foo::class,
'writable' => true,
'readable' => true,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,18 @@ public function testTypesAssignedToProperties(): void
]);
}

public function testBug1216(): void
{
$this->analyse([__DIR__ . '/data/bug-1216.php'], [
[
'Property Bug1216PropertyTest\Baz::$untypedBar (string) does not accept int.',
35,
],
[
'Property Bug1216PropertyTest\Dummy::$foo (Exception) does not accept stdClass.',
59,
],
]);
}

}
64 changes: 64 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-1216.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Bug1216PropertyTest;

abstract class Foo
{
/**
* @var int
*/
protected $foo;
}

trait Bar
{
/**
* @var int
*/
protected $bar;

protected $untypedBar;
}

/**
* @property string $foo
* @property string $bar
* @property string $untypedBar
*/
class Baz extends Foo
{

public function __construct()
{
$this->foo = 'foo'; // OK
$this->bar = 'bar'; // OK
$this->untypedBar = 123; // error
}

}

trait DecoratorTrait
{

/** @var \stdClass */
public $foo;

}

/**
* @property \Exception $foo
*/
class Dummy
{

use DecoratorTrait;

}

function (Dummy $dummy): void {
$dummy->foo = new \stdClass();
};

function (Dummy $dummy): void {
$dummy->foo = new \Exception();
};

0 comments on commit 55a3444

Please sign in to comment.