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

Reflection: support members only on descendants #134

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/Collector/ProvidedUsagesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ReflectionProvider;
use ShipMonk\PHPStan\DeadCode\Graph\ClassConstantRef;
use ShipMonk\PHPStan\DeadCode\Graph\ClassMemberUsage;
use ShipMonk\PHPStan\DeadCode\Graph\ClassMethodRef;
use ShipMonk\PHPStan\DeadCode\Provider\MemberUsageProvider;
use function get_class;
use function sprintf;
Expand Down Expand Up @@ -88,18 +86,8 @@ private function validateUsage(
$node->getStartLine(),
);

if ($memberRefClass !== null) {
if (!$this->reflectionProvider->hasClass($memberRefClass)) {
throw new LogicException("Class '$memberRefClass' does not exist. $context");
}

if ($memberRef instanceof ClassMethodRef && !$this->reflectionProvider->getClass($memberRefClass)->hasMethod($memberRef->getMemberName())) {
throw new LogicException("Method '{$memberRef->getMemberName()}' does not exist in class '$memberRefClass'. $context");
}

if ($memberRef instanceof ClassConstantRef && !$this->reflectionProvider->getClass($memberRefClass)->hasConstant($memberRef->getMemberName())) {
throw new LogicException("Constant '{$memberRef->getMemberName()}' does not exist in class '$memberRefClass'. $context");
}
if (($memberRefClass !== null) && !$this->reflectionProvider->hasClass($memberRefClass)) {
throw new LogicException("Class '$memberRefClass' does not exist. $context");
}

if (
Expand Down
12 changes: 2 additions & 10 deletions src/Provider/ReflectionUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ private function extractConstantsUsedByReflection(
$firstArg = $args[array_key_first($args)]; // @phpstan-ignore offsetAccess.notFound

foreach ($scope->getType($firstArg->value)->getConstantStrings() as $constantString) {
if (!$genericReflection->hasConstant($constantString->getValue())) {
continue;
}

$usedConstants[] = $this->createConstantUsage($genericReflection->getName(), $constantString->getValue());
}
}
Expand Down Expand Up @@ -140,10 +136,6 @@ private function extractMethodsUsedByReflection(
$firstArg = $args[array_key_first($args)]; // @phpstan-ignore offsetAccess.notFound

foreach ($scope->getType($firstArg->value)->getConstantStrings() as $constantString) {
if (!$genericReflection->hasMethod($constantString->getValue())) {
continue;
}

$usedMethods[] = $this->createMethodUsage($genericReflection->getName(), $constantString->getValue());
}
}
Expand Down Expand Up @@ -189,7 +181,7 @@ private function createConstantUsage(string $className, string $constantName): C
new ClassConstantRef(
$className,
$constantName,
false,
true,
),
);
}
Expand All @@ -201,7 +193,7 @@ private function createMethodUsage(string $className, string $methodName): Class
new ClassMethodRef(
$className,
$methodName,
false,
true,
),
);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Rule/data/providers/reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class Holder5
public function __construct() {}
}

abstract class HolderParent {}
class Holder6 extends HolderParent
{
const NOT_IN_PARENT = 1;
}

enum EnumHolder1 {
const CONST1 = 1;
public function used() {}
Expand Down Expand Up @@ -73,3 +79,14 @@ public function used() {}
$enumReflection1 = new \ReflectionClass(EnumHolder1::class);
$enumReflection1->getConstants();
$enumReflection1->getMethod('used');

/**
* @param class-string<HolderParent> $fqn
*/
function testMemberOnlyInDescendant(string $fqn) {
$classReflection = new \ReflectionClass($fqn);

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