Skip to content

Commit d072ced

Browse files
committed
Extract ParentMethodHelper
1 parent d78a5d8 commit d072ced

File tree

5 files changed

+78
-58
lines changed

5 files changed

+78
-58
lines changed

src/PhpDoc/StubValidator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
use PHPStan\Rules\Methods\MissingMethodReturnTypehintRule;
7676
use PHPStan\Rules\Methods\MissingMethodSelfOutTypeRule;
7777
use PHPStan\Rules\Methods\OverridingMethodRule;
78+
use PHPStan\Rules\Methods\ParentMethodHelper;
7879
use PHPStan\Rules\MissingTypehintCheck;
7980
use PHPStan\Rules\PhpDoc\AssertRuleHelper;
8081
use PHPStan\Rules\PhpDoc\ConditionalReturnTypeRuleHelper;
@@ -207,6 +208,7 @@ private function getRuleRegistry(Container $container): RuleRegistry
207208
$relativePathHelper = $container->getService('simpleRelativePathHelper');
208209
$assertRuleHelper = $container->getByType(AssertRuleHelper::class);
209210
$conditionalReturnTypeRuleHelper = $container->getByType(ConditionalReturnTypeRuleHelper::class);
211+
$parentMethodHelper = $container->getByType(ParentMethodHelper::class);
210212

211213
$rules = [
212214
// level 0
@@ -219,7 +221,7 @@ private function getRuleRegistry(Container $container): RuleRegistry
219221
new ExistingClassesInPropertiesRule($reflectionProvider, $classNameCheck, $unresolvableTypeHelper, $phpVersion, true, false, $discoveringSymbolsTip),
220222
new OverridingMethodRule(
221223
$phpVersion,
222-
new MethodSignatureRule($phpClassReflectionExtension, true, true),
224+
new MethodSignatureRule($parentMethodHelper, true, true),
223225
true,
224226
new MethodParameterComparisonHelper($phpVersion),
225227
new MethodVisibilityComparisonHelper(),

src/Rules/Methods/MethodSignatureRule.php

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Reflection\ClassReflection;
9-
use PHPStan\Reflection\ExtendedMethodReflection;
109
use PHPStan\Reflection\ExtendedParameterReflection;
1110
use PHPStan\Reflection\ExtendedParametersAcceptor;
12-
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
1311
use PHPStan\Rules\IdentifierRuleError;
1412
use PHPStan\Rules\Rule;
1513
use PHPStan\Rules\RuleError;
@@ -40,7 +38,7 @@ final class MethodSignatureRule implements Rule
4038
{
4139

4240
public function __construct(
43-
private PhpClassReflectionExtension $phpClassReflectionExtension,
41+
private ParentMethodHelper $parentMethodHelper,
4442
private bool $reportMaybes,
4543
private bool $reportStatic,
4644
)
@@ -67,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6765
}
6866
$errors = [];
6967
$declaringClass = $method->getDeclaringClass();
70-
foreach ($this->collectParentMethods($methodName, $method->getDeclaringClass()) as [$parentMethod, $parentMethodDeclaringClass]) {
68+
foreach ($this->parentMethodHelper->collectParentMethods($methodName, $method->getDeclaringClass()) as [$parentMethod, $parentMethodDeclaringClass]) {
7169
$parentVariants = $parentMethod->getVariants();
7270
if (count($parentVariants) !== 1) {
7371
continue;
@@ -141,57 +139,6 @@ public function processNode(Node $node, Scope $scope): array
141139
return $errors;
142140
}
143141

144-
/**
145-
* @return list<array{ExtendedMethodReflection, ClassReflection}>
146-
*/
147-
private function collectParentMethods(string $methodName, ClassReflection $class): array
148-
{
149-
$parentMethods = [];
150-
151-
$parentClass = $class->getParentClass();
152-
if ($parentClass !== null && $parentClass->hasNativeMethod($methodName)) {
153-
$parentMethod = $parentClass->getNativeMethod($methodName);
154-
if (!$parentMethod->isPrivate()) {
155-
$parentMethods[] = [$parentMethod, $parentMethod->getDeclaringClass()];
156-
}
157-
}
158-
159-
foreach ($class->getInterfaces() as $interface) {
160-
if (!$interface->hasNativeMethod($methodName)) {
161-
continue;
162-
}
163-
164-
$method = $interface->getNativeMethod($methodName);
165-
$parentMethods[] = [$method, $method->getDeclaringClass()];
166-
}
167-
168-
foreach ($class->getTraits(true) as $trait) {
169-
$nativeTraitReflection = $trait->getNativeReflection();
170-
if (!$nativeTraitReflection->hasMethod($methodName)) {
171-
continue;
172-
}
173-
174-
$methodReflection = $nativeTraitReflection->getMethod($methodName);
175-
$isAbstract = $methodReflection->isAbstract();
176-
if (!$isAbstract) {
177-
continue;
178-
}
179-
180-
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
181-
$parentMethods[] = [
182-
$this->phpClassReflectionExtension->createUserlandMethodReflection(
183-
$trait,
184-
$class,
185-
$methodReflection,
186-
$declaringTrait->getName(),
187-
),
188-
$declaringTrait,
189-
];
190-
}
191-
192-
return $parentMethods;
193-
}
194-
195142
/**
196143
* @return array{TrinaryLogic, Type, Type}
197144
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Methods;
4+
5+
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\ExtendedMethodReflection;
8+
use PHPStan\Reflection\Php\PhpClassReflectionExtension;
9+
10+
#[AutowiredService]
11+
final class ParentMethodHelper
12+
{
13+
14+
public function __construct(
15+
private PhpClassReflectionExtension $phpClassReflectionExtension,
16+
)
17+
{
18+
}
19+
20+
/**
21+
* @return list<array{ExtendedMethodReflection, ClassReflection}>
22+
*/
23+
public function collectParentMethods(string $methodName, ClassReflection $class): array
24+
{
25+
$parentMethods = [];
26+
27+
$parentClass = $class->getParentClass();
28+
if ($parentClass !== null && $parentClass->hasNativeMethod($methodName)) {
29+
$parentMethod = $parentClass->getNativeMethod($methodName);
30+
if (!$parentMethod->isPrivate()) {
31+
$parentMethods[] = [$parentMethod, $parentMethod->getDeclaringClass()];
32+
}
33+
}
34+
35+
foreach ($class->getInterfaces() as $interface) {
36+
if (!$interface->hasNativeMethod($methodName)) {
37+
continue;
38+
}
39+
40+
$method = $interface->getNativeMethod($methodName);
41+
$parentMethods[] = [$method, $method->getDeclaringClass()];
42+
}
43+
44+
foreach ($class->getTraits(true) as $trait) {
45+
$nativeTraitReflection = $trait->getNativeReflection();
46+
if (!$nativeTraitReflection->hasMethod($methodName)) {
47+
continue;
48+
}
49+
50+
$methodReflection = $nativeTraitReflection->getMethod($methodName);
51+
$isAbstract = $methodReflection->isAbstract();
52+
if (!$isAbstract) {
53+
continue;
54+
}
55+
56+
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
57+
$parentMethods[] = [
58+
$this->phpClassReflectionExtension->createUserlandMethodReflection(
59+
$trait,
60+
$class,
61+
$methodReflection,
62+
$declaringTrait->getName(),
63+
),
64+
$declaringTrait,
65+
];
66+
}
67+
68+
return $parentMethods;
69+
}
70+
71+
}

tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function getRule(): Rule
2727

2828
return new OverridingMethodRule(
2929
$phpVersion,
30-
new MethodSignatureRule($phpClassReflectionExtension, $this->reportMaybes, $this->reportStatic),
30+
new MethodSignatureRule(new ParentMethodHelper($phpClassReflectionExtension), $this->reportMaybes, $this->reportStatic),
3131
true,
3232
new MethodParameterComparisonHelper($phpVersion),
3333
new MethodVisibilityComparisonHelper(),

tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function getRule(): Rule
3030

3131
return new OverridingMethodRule(
3232
$phpVersion,
33-
new MethodSignatureRule($phpClassReflectionExtension, true, true),
33+
new MethodSignatureRule(new ParentMethodHelper($phpClassReflectionExtension), true, true),
3434
false,
3535
new MethodParameterComparisonHelper($phpVersion),
3636
new MethodVisibilityComparisonHelper(),

0 commit comments

Comments
 (0)