Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassReflection: make getTraits recursive #557

Merged
merged 7 commits into from Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Reflection/ClassReflection.php
Expand Up @@ -672,11 +672,24 @@ public function getInterfaces(): array
/**
* @return \PHPStan\Reflection\ClassReflection[]
*/
public function getTraits(): array
public function getTraits(bool $recursive = false): array
{
return array_map(function (\ReflectionClass $trait): ClassReflection {
$traits = array_map(function (\ReflectionClass $trait): ClassReflection {
return $this->reflectionProvider->getClass($trait->getName());
}, $this->getNativeReflection()->getTraits());
}, $this->collectTraits($this->getNativeReflection()));
iamrgroot marked this conversation as resolved.
Show resolved Hide resolved

if ($recursive) {
$parentClass = $this->getNativeReflection()->getParentClass();

if ($parentClass !== false) {
return array_merge(
$traits,
$this->reflectionProvider->getClass($parentClass->getName())->getTraits(true)
);
}
}

return $traits;
}

/**
Expand Down
92 changes: 92 additions & 0 deletions tests/PHPStan/Reflection/ClassReflectionTest.php
Expand Up @@ -220,4 +220,96 @@ public function testDeprecatedConstantFromAnotherFile(): void
$this->assertTrue($constant->isDeprecated()->yes());
}

/**
* @dataProvider dataNestedRecursiveTraits
* @param class-string $className
* @param class-string[] $expected
* @param bool $recursive
*/
public function testGetTraits(string $className, array $expected, bool $recursive = false): void
iamrgroot marked this conversation as resolved.
Show resolved Hide resolved
{
$reflectionProvider = $this->createBroker();

$this->assertSame(
array_map(
static fn(ClassReflection $classReflection) => $classReflection->getNativeReflection()->getName(),
iamrgroot marked this conversation as resolved.
Show resolved Hide resolved
$reflectionProvider->getClass($className)->getTraits($recursive)
),
$expected
);
}

public function dataNestedRecursiveTraits(): array
{
return [
[
\NestedTraits\NoTrait::class,
[],
],
[
\NestedTraits\NoTrait::class,
[],
true,
],
[
\NestedTraits\Foo::class,
[
\NestedTraits\FooTrait::class,
],
],
[
\NestedTraits\Foo::class,
[
\NestedTraits\FooTrait::class,
],
true,
],
[
\NestedTraits\Bar::class,
[
\NestedTraits\BarTrait::class,
\NestedTraits\FooTrait::class,
],
],
[
\NestedTraits\Bar::class,
[
\NestedTraits\BarTrait::class,
\NestedTraits\FooTrait::class,
],
true,
],
[
\NestedTraits\Baz::class,
[
\NestedTraits\BazTrait::class,
\NestedTraits\BarTrait::class,
\NestedTraits\FooTrait::class,
],
],
[
\NestedTraits\Baz::class,
[
\NestedTraits\BazTrait::class,
\NestedTraits\BarTrait::class,
\NestedTraits\FooTrait::class,
],
true,
],
[
\NestedTraits\BazChild::class,
[],
],
[
\NestedTraits\BazChild::class,
[
\NestedTraits\BazTrait::class,
\NestedTraits\BarTrait::class,
\NestedTraits\FooTrait::class,
],
true,
],
];
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Reflection/data/NestedTraits.php
@@ -0,0 +1,40 @@
<?php

namespace NestedTraits;

trait FooTrait
{
}

trait BarTrait
{
use FooTrait;
}

trait BazTrait
{
use BarTrait;
}

class NoTrait
{
}

class Foo
{
use FooTrait;
}

class Bar
{
use BarTrait;
}

class Baz
{
use BazTrait;
}

class BazChild extends Baz
{
}