Skip to content

Commit c3da6a9

Browse files
committedJan 25, 2023
Modernized InClassMethodNode rules
1 parent e864b24 commit c3da6a9

11 files changed

+14
-78
lines changed
 

‎src/Rules/Classes/UnusedConstructorParametersRule.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Internal\SprintfHelper;
1010
use PHPStan\Node\InClassMethodNode;
11-
use PHPStan\Reflection\MethodReflection;
1211
use PHPStan\Rules\Rule;
1312
use PHPStan\Rules\UnusedFunctionParametersCheck;
1413
use PHPStan\ShouldNotHappenException;
@@ -37,15 +36,7 @@ public function getNodeType(): string
3736

3837
public function processNode(Node $node, Scope $scope): array
3938
{
40-
if (!$scope->isInClass()) {
41-
throw new ShouldNotHappenException();
42-
}
43-
44-
$method = $scope->getFunction();
45-
if (!$method instanceof MethodReflection) {
46-
return [];
47-
}
48-
39+
$method = $node->getMethodReflection();
4940
$originalNode = $node->getOriginalNode();
5041
if (strtolower($method->getName()) !== '__construct' || $originalNode->stmts === null) {
5142
return [];
@@ -57,9 +48,9 @@ public function processNode(Node $node, Scope $scope): array
5748

5849
$message = sprintf(
5950
'Constructor of class %s has an unused parameter $%%s.',
60-
SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName()),
51+
SprintfHelper::escapeFormatString($node->getClassReflection()->getDisplayName()),
6152
);
62-
if ($scope->getClassReflection()->isAnonymous()) {
53+
if ($node->getClassReflection()->isAnonymous()) {
6354
$message = 'Constructor of an anonymous class has an unused parameter $%s.';
6455
}
6556

‎src/Rules/Generics/MethodSignatureVarianceRule.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Internal\SprintfHelper;
88
use PHPStan\Node\InClassMethodNode;
9-
use PHPStan\Reflection\MethodReflection;
109
use PHPStan\Reflection\ParametersAcceptorSelector;
1110
use PHPStan\Rules\Rule;
1211
use function sprintf;
@@ -28,10 +27,7 @@ public function getNodeType(): string
2827

2928
public function processNode(Node $node, Scope $scope): array
3029
{
31-
$method = $scope->getFunction();
32-
if (!$method instanceof MethodReflection) {
33-
return [];
34-
}
30+
$method = $node->getMethodReflection();
3531

3632
return $this->varianceCheck->checkParametersAcceptor(
3733
ParametersAcceptorSelector::selectSingle($method->getVariants()),

‎src/Rules/Methods/ConsistentConstructorRule.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
use PHPStan\Reflection\ClassReflection;
99
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
1010
use PHPStan\Reflection\MethodPrototypeReflection;
11-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
1211
use PHPStan\Reflection\Php\PhpMethodReflection;
1312
use PHPStan\Rules\Rule;
14-
use PHPStan\ShouldNotHappenException;
1513
use function strtolower;
1614

1715
/** @implements Rule<InClassMethodNode> */
@@ -31,12 +29,7 @@ public function getNodeType(): string
3129

3230
public function processNode(Node $node, Scope $scope): array
3331
{
34-
$method = $scope->getFunction();
35-
36-
if (! $method instanceof PhpMethodFromParserNodeReflection) {
37-
throw new ShouldNotHappenException();
38-
}
39-
32+
$method = $node->getMethodReflection();
4033
if (strtolower($method->getName()) !== '__construct') {
4134
return [];
4235
}

‎src/Rules/Methods/ExistingClassesInTypehintsRule.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Internal\SprintfHelper;
88
use PHPStan\Node\InClassMethodNode;
9-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
109
use PHPStan\Rules\FunctionDefinitionCheck;
1110
use PHPStan\Rules\Rule;
12-
use PHPStan\ShouldNotHappenException;
1311
use function sprintf;
1412

1513
/**
@@ -29,15 +27,8 @@ public function getNodeType(): string
2927

3028
public function processNode(Node $node, Scope $scope): array
3129
{
32-
$methodReflection = $scope->getFunction();
33-
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
34-
throw new ShouldNotHappenException();
35-
}
36-
if (!$scope->isInClass()) {
37-
throw new ShouldNotHappenException();
38-
}
39-
40-
$className = SprintfHelper::escapeFormatString($scope->getClassReflection()->getDisplayName());
30+
$methodReflection = $node->getMethodReflection();
31+
$className = SprintfHelper::escapeFormatString($node->getClassReflection()->getDisplayName());
4132
$methodName = SprintfHelper::escapeFormatString($methodReflection->getName());
4233

4334
return $this->check->checkClassMethod(

‎src/Rules/Methods/FinalPrivateMethodRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Php\PhpVersion;
9-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
109
use PHPStan\Rules\Rule;
1110
use PHPStan\Rules\RuleErrorBuilder;
1211
use function sprintf;
@@ -28,11 +27,7 @@ public function getNodeType(): string
2827

2928
public function processNode(Node $node, Scope $scope): array
3029
{
31-
$method = $scope->getFunction();
32-
if (!$method instanceof PhpMethodFromParserNodeReflection) {
33-
return [];
34-
}
35-
30+
$method = $node->getMethodReflection();
3631
if (!$this->phpVersion->producesWarningForFinalPrivateMethods()) {
3732
return [];
3833
}

‎src/Rules/Methods/MethodSignatureRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
1111
use PHPStan\Reflection\ParametersAcceptorSelector;
1212
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
13-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
1413
use PHPStan\Rules\Rule;
1514
use PHPStan\Rules\RuleErrorBuilder;
1615
use PHPStan\TrinaryLogic;
@@ -46,11 +45,7 @@ public function getNodeType(): string
4645

4746
public function processNode(Node $node, Scope $scope): array
4847
{
49-
$method = $scope->getFunction();
50-
if (!$method instanceof PhpMethodFromParserNodeReflection) {
51-
return [];
52-
}
53-
48+
$method = $node->getMethodReflection();
5449
$methodName = $method->getName();
5550
if ($methodName === '__construct') {
5651
return [];

‎src/Rules/Methods/MissingMethodParameterTypehintRule.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ public function getNodeType(): string
3434

3535
public function processNode(Node $node, Scope $scope): array
3636
{
37-
$methodReflection = $scope->getFunction();
38-
if (!$methodReflection instanceof MethodReflection) {
39-
return [];
40-
}
41-
37+
$methodReflection = $node->getMethodReflection();
4238
$messages = [];
4339

4440
foreach (ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters() as $parameterReflection) {

‎src/Rules/Methods/MissingMethodReturnTypehintRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
8-
use PHPStan\Reflection\MethodReflection;
98
use PHPStan\Reflection\ParametersAcceptorSelector;
109
use PHPStan\Rules\MissingTypehintCheck;
1110
use PHPStan\Rules\Rule;
@@ -32,11 +31,7 @@ public function getNodeType(): string
3231

3332
public function processNode(Node $node, Scope $scope): array
3433
{
35-
$methodReflection = $scope->getFunction();
36-
if (!$methodReflection instanceof MethodReflection) {
37-
return [];
38-
}
39-
34+
$methodReflection = $node->getMethodReflection();
4035
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
4136

4237
if ($returnType instanceof MixedType && !$returnType->isExplicitMixed()) {

‎src/Rules/Methods/OverridingMethodRule.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
1010
use PHPStan\Reflection\MethodPrototypeReflection;
1111
use PHPStan\Reflection\ParametersAcceptorSelector;
12-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
1312
use PHPStan\Rules\Rule;
1413
use PHPStan\Rules\RuleError;
1514
use PHPStan\Rules\RuleErrorBuilder;
16-
use PHPStan\ShouldNotHappenException;
1715
use PHPStan\Type\VerbosityLevel;
1816
use function array_merge;
1917
use function count;
@@ -42,11 +40,7 @@ public function getNodeType(): string
4240

4341
public function processNode(Node $node, Scope $scope): array
4442
{
45-
$method = $scope->getFunction();
46-
if (!$method instanceof PhpMethodFromParserNodeReflection) {
47-
throw new ShouldNotHappenException();
48-
}
49-
43+
$method = $node->getMethodReflection();
5044
$prototype = $method->getPrototype();
5145
if ($prototype->getDeclaringClass()->getName() === $method->getDeclaringClass()->getName()) {
5246
if (strtolower($method->getName()) === '__construct') {

‎src/Rules/PhpDoc/MethodAssertRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\ShouldNotHappenException;
109
use function count;
1110

1211
/**
@@ -26,11 +25,7 @@ public function getNodeType(): string
2625

2726
public function processNode(Node $node, Scope $scope): array
2827
{
29-
$method = $scope->getFunction();
30-
if ($method === null) {
31-
throw new ShouldNotHappenException();
32-
}
33-
28+
$method = $node->getMethodReflection();
3429
$variants = $method->getVariants();
3530
if (count($variants) !== 1) {
3631
return [];

‎src/Rules/PhpDoc/MethodConditionalReturnTypeRule.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Rules\Rule;
9-
use PHPStan\ShouldNotHappenException;
109
use function count;
1110

1211
/**
@@ -26,11 +25,7 @@ public function getNodeType(): string
2625

2726
public function processNode(Node $node, Scope $scope): array
2827
{
29-
$method = $scope->getFunction();
30-
if ($method === null) {
31-
throw new ShouldNotHappenException();
32-
}
33-
28+
$method = $node->getMethodReflection();
3429
$variants = $method->getVariants();
3530
if (count($variants) !== 1) {
3631
return [];

0 commit comments

Comments
 (0)
Please sign in to comment.