Skip to content

Commit

Permalink
Support @mixin above traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 3, 2024
1 parent 0d0de94 commit f5e2e32
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Reflection/Mixin/MixinMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ private function findMethod(ClassReflection $classReflection, string $methodName
return new MixinMethodReflection($method, $static);
}

foreach ($classReflection->getTraits() as $traitClass) {
$methodWithDeclaringClass = $this->findMethod($traitClass, $methodName);
if ($methodWithDeclaringClass === null) {
continue;
}

return $methodWithDeclaringClass;
}

$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
$method = $this->findMethod($parentClass, $methodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ private function findProperty(ClassReflection $classReflection, string $property
return $property;
}

foreach ($classReflection->getTraits() as $traitClass) {
$methodWithDeclaringClass = $this->findProperty($traitClass, $propertyName);
if ($methodWithDeclaringClass === null) {
continue;
}

return $methodWithDeclaringClass;
}

$parentClass = $classReflection->getParentClass();
while ($parentClass !== null) {
$property = $this->findProperty($parentClass, $propertyName);
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/conditional-complex-templates.php');

yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-7511.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Properties/data/trait-mixin.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/trait-mixin.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-4708.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-7156.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-6364.php');
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3345,4 +3345,13 @@ public function testNoNamedArguments(): void
]);
}

public function testTraitMixin(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/trait-mixin.php'], []);
}

}
44 changes: 44 additions & 0 deletions tests/PHPStan/Rules/Methods/data/trait-mixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace TraitMixinMethods;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
interface Foo
{

/** @return T */
public function get();

}

/**
* @mixin Foo<static>
*/
trait FooTrait
{

}

class Usages
{

use FooTrait;

}

class ChildUsages extends Usages
{

}

function (Usages $u): void {
assertType(Usages::class, $u->get());
};

function (ChildUsages $u): void {
assertType(ChildUsages::class, $u->get());
};
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,12 @@ public function testBug9694(): void
$this->analyse([__DIR__ . '/data/bug-9694.php'], []);
}

public function testTraitMixin(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/trait-mixin.php'], []);
}

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

namespace TraitMixinProperties;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
class Foo
{

/** @var T */
public $a;

}

/**
* @mixin Foo<static>
*/
trait FooTrait
{

}

#[\AllowDynamicProperties]
class Usages
{

use FooTrait;

}

class ChildUsages extends Usages
{

}

function (Usages $u): void {
assertType(Usages::class, $u->a);
};

function (ChildUsages $u): void {
assertType(ChildUsages::class, $u->a);
};

0 comments on commit f5e2e32

Please sign in to comment.