Skip to content

Commit 6d08225

Browse files
mad-brillerondrejmirtes
authored andcommitted
Simplify access to class reflections in ClassConstantsNode and ClassMethodsNode rules.
1 parent 32ca340 commit 6d08225

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,8 @@ private function processStmtNode(
706706

707707
$this->processStmtNodes($stmt, $stmt->stmts, $classScope, $classStatementsGatherer, $context);
708708
$nodeCallback(new ClassPropertiesNode($stmt, $this->readWritePropertiesExtensionProvider, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls(), $classStatementsGatherer->getReturnStatementsNodes(), $classReflection), $classScope);
709-
$nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls()), $classScope);
710-
$nodeCallback(new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches()), $classScope);
709+
$nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls(), $classReflection), $classScope);
710+
$nodeCallback(new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches(), $classReflection), $classScope);
711711
$classReflection->evictPrivateSymbols();
712712
$this->calledMethodResults = [];
713713
} elseif ($stmt instanceof Node\Stmt\Property) {

src/Node/ClassConstantsNode.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Stmt\ClassLike;
77
use PhpParser\NodeAbstract;
88
use PHPStan\Node\Constant\ClassConstantFetch;
9+
use PHPStan\Reflection\ClassReflection;
910

1011
/** @api */
1112
class ClassConstantsNode extends NodeAbstract implements VirtualNode
@@ -15,7 +16,7 @@ class ClassConstantsNode extends NodeAbstract implements VirtualNode
1516
* @param ClassConst[] $constants
1617
* @param ClassConstantFetch[] $fetches
1718
*/
18-
public function __construct(private ClassLike $class, private array $constants, private array $fetches)
19+
public function __construct(private ClassLike $class, private array $constants, private array $fetches, private ClassReflection $classReflection)
1920
{
2021
parent::__construct($class->getAttributes());
2122
}
@@ -54,4 +55,9 @@ public function getSubNodeNames(): array
5455
return [];
5556
}
5657

58+
public function getClassReflection(): ClassReflection
59+
{
60+
return $this->classReflection;
61+
}
62+
5763
}

src/Node/ClassMethodsNode.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Stmt\ClassLike;
66
use PhpParser\NodeAbstract;
77
use PHPStan\Node\Method\MethodCall;
8+
use PHPStan\Reflection\ClassReflection;
89

910
/** @api */
1011
class ClassMethodsNode extends NodeAbstract implements VirtualNode
@@ -14,7 +15,7 @@ class ClassMethodsNode extends NodeAbstract implements VirtualNode
1415
* @param ClassMethod[] $methods
1516
* @param array<int, MethodCall> $methodCalls
1617
*/
17-
public function __construct(private ClassLike $class, private array $methods, private array $methodCalls)
18+
public function __construct(private ClassLike $class, private array $methods, private array $methodCalls, private ClassReflection $classReflection)
1819
{
1920
parent::__construct($class->getAttributes());
2021
}
@@ -53,4 +54,9 @@ public function getSubNodeNames(): array
5354
return [];
5455
}
5556

57+
public function getClassReflection(): ClassReflection
58+
{
59+
return $this->classReflection;
60+
}
61+
5662
}

src/Rules/DeadCode/UnusedPrivateConstantRule.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Rules\Constants\AlwaysUsedClassConstantsExtensionProvider;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Rules\RuleErrorBuilder;
11-
use PHPStan\ShouldNotHappenException;
1211
use function sprintf;
1312

1413
/**
@@ -31,11 +30,8 @@ public function processNode(Node $node, Scope $scope): array
3130
if (!$node->getClass() instanceof Node\Stmt\Class_ && !$node->getClass() instanceof Node\Stmt\Enum_) {
3231
return [];
3332
}
34-
if (!$scope->isInClass()) {
35-
throw new ShouldNotHappenException();
36-
}
3733

38-
$classReflection = $scope->getClassReflection();
34+
$classReflection = $node->getClassReflection();
3935

4036
$constants = [];
4137
foreach ($node->getConstants() as $constant) {

src/Rules/DeadCode/UnusedPrivateMethodRule.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Reflection\MethodReflection;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12-
use PHPStan\ShouldNotHappenException;
1312
use PHPStan\Type\Constant\ConstantStringType;
1413
use function array_map;
1514
use function count;
@@ -32,10 +31,7 @@ public function processNode(Node $node, Scope $scope): array
3231
if (!$node->getClass() instanceof Node\Stmt\Class_ && !$node->getClass() instanceof Node\Stmt\Enum_) {
3332
return [];
3433
}
35-
if (!$scope->isInClass()) {
36-
throw new ShouldNotHappenException();
37-
}
38-
$classReflection = $scope->getClassReflection();
34+
$classReflection = $node->getClassReflection();
3935
$constructor = null;
4036
if ($classReflection->hasConstructor()) {
4137
$constructor = $classReflection->getConstructor();

0 commit comments

Comments
 (0)