Skip to content

Commit

Permalink
ClassHierarchy: fix missing parts of vendor hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Dec 30, 2024
1 parent fe15bef commit dad08d5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
51 changes: 19 additions & 32 deletions src/Collector/ClassDefinitionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use LogicException;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Enum_;
Expand All @@ -14,6 +13,8 @@
use PhpParser\Node\Stmt\TraitUseAdaptation\Precedence;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use ShipMonk\PHPStan\DeadCode\Enum\ClassLikeKind;
use ShipMonk\PHPStan\DeadCode\Enum\Visibility;
use function array_fill_keys;
Expand All @@ -33,6 +34,13 @@
class ClassDefinitionCollector implements Collector
{

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function getNodeType(): string
{
return ClassLike::class;
Expand Down Expand Up @@ -61,6 +69,7 @@ public function processNode(

$kind = $this->getKind($node);
$typeName = $node->namespacedName->toString();
$reflection = $this->reflectionProvider->getClass($typeName);

$methods = [];

Expand All @@ -87,54 +96,32 @@ public function processNode(
'name' => $typeName,
'methods' => $methods,
'constants' => $constants,
'parents' => $this->getParents($node),
'parents' => $this->getParents($reflection),
'traits' => $this->getTraits($node),
'interfaces' => $this->getInterfaces($node),
'interfaces' => $this->getInterfaces($reflection),
];
}

/**
* @return array<string, null>
*/
private function getParents(ClassLike $node): array
private function getParents(ClassReflection $reflection): array
{
if ($node instanceof Class_) {
if ($node->extends === null) {
return [];
}
$parents = [];

return [$node->extends->toString() => null];
foreach ($reflection->getParentClassesNames() as $parent) {
$parents[$parent] = null;
}

if ($node instanceof Interface_) {
return array_fill_keys(
array_map(
static fn(Name $name) => $name->toString(),
$node->extends,
),
null,
);
}

return [];
return $parents;
}

/**
* @return array<string, null>
*/
private function getInterfaces(ClassLike $node): array
private function getInterfaces(ClassReflection $reflection): array
{
if ($node instanceof Class_ || $node instanceof Enum_) {
return array_fill_keys(
array_map(
static fn(Name $name) => $name->toString(),
$node->implements,
),
null,
);
}

return [];
return array_fill_keys(array_map(static fn (ClassReflection $reflection) => $reflection->getName(), $reflection->getInterfaces()), null);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Rule/DeadCodeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function getCollectors(): array
$reflectionProvider,
$this->getMemberUsageProviders(),
),
new ClassDefinitionCollector(),
new ClassDefinitionCollector(self::createReflectionProvider()),
new MethodCallCollector($this->createUsageOriginDetector(), $this->trackMixedAccess),
new ConstantFetchCollector($this->createUsageOriginDetector(), $reflectionProvider, $this->trackMixedAccess),
];
Expand Down
18 changes: 18 additions & 0 deletions tests/Rule/data/providers/reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Reflection;

use PHPUnit\Framework\TestCase;
use ShipMonk\PHPStan\DeadCode\Rule\RuleTestCase;

interface MyParent
{
Expand Down Expand Up @@ -99,3 +101,19 @@ function testMemberOnlyInDescendant(string $fqn) {
echo $classReflection->getConstant('NOT_IN_PARENT');
}
}

class UserOfVendorClass extends RuleTestCase
{
const DUMMY = 1;
}

/**
* @param class-string<TestCase> $testCase
*/
function testVendorClassDescendant(string $testCase) {
$classReflection = new \ReflectionClass($testCase);

if ($classReflection->hasConstant('DUMMY')) {
echo $classReflection->getConstant('DUMMY');
}
}

0 comments on commit dad08d5

Please sign in to comment.