Skip to content

Commit

Permalink
Merge pull request #1362 from staabm/traits
Browse files Browse the repository at this point in the history
Implement `ReflectionClass#getTraitClassNames()`
  • Loading branch information
Ocramius authored Aug 7, 2023
2 parents 350fb40 + f89b16b commit 77c68f6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,12 @@ private function addEnumInterfaces(array $interfaces): array
return $interfaces;
}

/** @return list<trait-string> */
public function getTraitClassNames(): array
{
return $this->traitClassNames;
}

/**
* Get the names of the traits used as an array of strings, if any are
* defined. If this class does not have any defined traits, this will
Expand Down
8 changes: 8 additions & 0 deletions src/Reflection/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ public function isInterface(): bool
return $this->reflectionClass->isInterface();
}

/**
* {@inheritDoc}
*/
public function getTraitClassNames(): array
{
return $this->reflectionClass->getTraitClassNames();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions test/unit/Fixture/ClassUsesUnknownTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Roave\BetterReflectionTest\Fixture;

class ClassUsesUnknownTrait
{
use UnknownTrait;
}
79 changes: 79 additions & 0 deletions test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
use SplFileInfo;
use stdClass;
use Stringable;
use TraitFixtureA;
use TraitFixtureC;
use TypeError;
use UnitEnum;

Expand Down Expand Up @@ -1500,6 +1502,83 @@ public function testGetTraitAliases(): void
], $classInfo->getTraitAliases());
}

public function testGetTraitNamesWithMissingTraitDefinitions(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassUsesUnknownTrait.php',
$this->astLocator,
));

$this->expectException(IdentifierNotFound::class);

$reflector->reflectClass(Fixture\ClassUsesUnknownTrait::class)->getTraitNames();
}

public function testGetTraitsWithMissingTraitDefinitions(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassUsesUnknownTrait.php',
$this->astLocator,
));

$this->expectException(IdentifierNotFound::class);

$reflector->reflectClass(Fixture\ClassUsesUnknownTrait::class)->getTraits();
}

public function testGetTraitAliasesWithMissingTraitDefinitions(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
__DIR__ . '/../Fixture/ClassUsesUnknownTrait.php',
$this->astLocator,
));

self::assertSame(
[],
$reflector->reflectClass(Fixture\ClassUsesUnknownTrait::class)->getTraitAliases(),
);
}

/** @param list<class-string> $expectedTraits */
#[DataProvider('getTraitClassNamesDataProvider')]
public function testGetTraitClassNames(string $sourcePath, string $className, array $expectedTraits): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
$sourcePath,
$this->astLocator,
));

self::assertSame(
$expectedTraits,
$reflector->reflectClass($className)->getTraitClassNames(),
);
}

/** @return list<array{string, class-string, list<class-string>}> */
public static function getTraitClassNamesDataProvider(): array
{
return [
[
__DIR__ . '/../Fixture/ClassUsesUnknownTrait.php',
Fixture\ClassUsesUnknownTrait::class,
['Roave\BetterReflectionTest\Fixture\UnknownTrait'],
],
[
__DIR__ . '/../Fixture/TraitFixture.php',
TraitFixtureA::class,
['TraitFixtureTraitA'],
],
[
__DIR__ . '/../Fixture/TraitFixture.php',
TraitFixtureC::class,
[
'TraitFixtureTraitC',
'TraitFixtureTraitC3',
],
],
];
}

public function testMethodsFromTraits(): void
{
$reflector = new DefaultReflector(new SingleFileSourceLocator(
Expand Down

0 comments on commit 77c68f6

Please sign in to comment.