Skip to content

Commit 8c38bbc

Browse files
committed
Refactoring - extract ConsistentConstructorHelper
1 parent f827021 commit 8c38bbc

File tree

8 files changed

+45
-73
lines changed

8 files changed

+45
-73
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Classes;
4+
5+
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
8+
use PHPStan\Reflection\ExtendedMethodReflection;
9+
10+
#[AutowiredService]
11+
final class ConsistentConstructorHelper
12+
{
13+
14+
public function findConsistentConstructor(ClassReflection $classReflection): ?ExtendedMethodReflection
15+
{
16+
if ($classReflection->hasConsistentConstructor()) {
17+
if ($classReflection->hasConstructor()) {
18+
return $classReflection->getConstructor();
19+
}
20+
21+
return new DummyConstructorReflection($classReflection);
22+
}
23+
24+
$parent = $classReflection->getParentClass();
25+
if ($parent === null) {
26+
return null;
27+
}
28+
29+
return $this->findConsistentConstructor($parent);
30+
}
31+
32+
}

src/Rules/Classes/InstantiationRule.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use PHPStan\DependencyInjection\RegisteredRule;
1111
use PHPStan\Internal\SprintfHelper;
1212
use PHPStan\Reflection\ClassReflection;
13-
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
14-
use PHPStan\Reflection\ExtendedMethodReflection;
1513
use PHPStan\Reflection\ParametersAcceptorSelector;
1614
use PHPStan\Reflection\Php\PhpMethodReflection;
1715
use PHPStan\Reflection\ReflectionProvider;
@@ -45,6 +43,7 @@ public function __construct(
4543
private ReflectionProvider $reflectionProvider,
4644
private FunctionCallParametersCheck $check,
4745
private ClassNameCheck $classCheck,
46+
private ConsistentConstructorHelper $consistentConstructorHelper,
4847
#[AutowiredParameter(ref: '%tips.discoveringSymbols%')]
4948
private bool $discoveringSymbolsTip,
5049
)
@@ -96,7 +95,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
9695
&& $constructor instanceof PhpMethodReflection
9796
&& !$constructor->isFinal()->yes()
9897
&& !$constructor->getPrototype()->isAbstract()
99-
&& $this->findConsistentParentConstructor($classReflection) === null
98+
&& $this->consistentConstructorHelper->findConsistentConstructor($classReflection) === null
10099
) {
101100
return [];
102101
}
@@ -312,22 +311,4 @@ private function getClassNames(Node $node, Scope $scope): array
312311
);
313312
}
314313

315-
private function findConsistentParentConstructor(ClassReflection $classReflection): ?ExtendedMethodReflection
316-
{
317-
if ($classReflection->hasConsistentConstructor()) {
318-
if ($classReflection->hasConstructor()) {
319-
return $classReflection->getConstructor();
320-
}
321-
322-
return new DummyConstructorReflection($classReflection);
323-
}
324-
325-
$parent = $classReflection->getParentClass();
326-
if ($parent === null) {
327-
return null;
328-
}
329-
330-
return $this->findConsistentParentConstructor($parent);
331-
}
332-
333314
}

src/Rules/Classes/NewStaticRule.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Php\PhpVersion;
9-
use PHPStan\Reflection\ClassReflection;
10-
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
11-
use PHPStan\Reflection\ExtendedMethodReflection;
129
use PHPStan\Reflection\Php\PhpMethodReflection;
1310
use PHPStan\Rules\Rule;
1411
use PHPStan\Rules\RuleErrorBuilder;
@@ -24,6 +21,7 @@ final class NewStaticRule implements Rule
2421

2522
public function __construct(
2623
private PhpVersion $phpVersion,
24+
private ConsistentConstructorHelper $consistentConstructorHelper,
2725
)
2826
{
2927
}
@@ -58,7 +56,8 @@ public function processNode(Node $node, Scope $scope): array
5856
->tip('See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static')
5957
->build(),
6058
];
61-
if ($classReflection->hasConsistentConstructor()) {
59+
$consistentConstructor = $this->consistentConstructorHelper->findConsistentConstructor($classReflection);
60+
if ($consistentConstructor !== null) {
6261
return [];
6362
}
6463
if (!$classReflection->hasConstructor()) {
@@ -80,14 +79,6 @@ public function processNode(Node $node, Scope $scope): array
8079
return [];
8180
}
8281

83-
$parent = $classReflection->getParentClass();
84-
if ($parent !== null) {
85-
$parentConstructor = $this->findConsistentParentConstructor($parent);
86-
if ($parentConstructor !== null) {
87-
return [];
88-
}
89-
}
90-
9182
if ($constructor instanceof PhpMethodReflection) {
9283
$prototype = $constructor->getPrototype();
9384
if ($prototype->isAbstract()) {
@@ -111,22 +102,4 @@ public function processNode(Node $node, Scope $scope): array
111102
return $messages;
112103
}
113104

114-
private function findConsistentParentConstructor(ClassReflection $classReflection): ?ExtendedMethodReflection
115-
{
116-
if ($classReflection->hasConsistentConstructor()) {
117-
if ($classReflection->hasConstructor()) {
118-
return $classReflection->getConstructor();
119-
}
120-
121-
return new DummyConstructorReflection($classReflection);
122-
}
123-
124-
$parent = $classReflection->getParentClass();
125-
if ($parent === null) {
126-
return null;
127-
}
128-
129-
return $this->findConsistentParentConstructor($parent);
130-
}
131-
132105
}

src/Rules/Methods/ConsistentConstructorRule.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\RegisteredRule;
88
use PHPStan\Node\InClassMethodNode;
9-
use PHPStan\Reflection\ClassReflection;
10-
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
11-
use PHPStan\Reflection\ExtendedMethodReflection;
9+
use PHPStan\Rules\Classes\ConsistentConstructorHelper;
1210
use PHPStan\Rules\Rule;
1311
use function array_merge;
1412
use function strtolower;
@@ -19,6 +17,7 @@ final class ConsistentConstructorRule implements Rule
1917
{
2018

2119
public function __construct(
20+
private ConsistentConstructorHelper $consistentConstructorHelper,
2221
private MethodParameterComparisonHelper $methodParameterComparisonHelper,
2322
private MethodVisibilityComparisonHelper $methodVisibilityComparisonHelper,
2423
)
@@ -42,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4241
return [];
4342
}
4443

45-
$parentConstructor = $this->findConsistentParentConstructor($parent);
44+
$parentConstructor = $this->consistentConstructorHelper->findConsistentConstructor($parent);
4645
if ($parentConstructor === null) {
4746
return [];
4847
}
@@ -53,22 +52,4 @@ public function processNode(Node $node, Scope $scope): array
5352
);
5453
}
5554

56-
private function findConsistentParentConstructor(ClassReflection $classReflection): ?ExtendedMethodReflection
57-
{
58-
if ($classReflection->hasConsistentConstructor()) {
59-
if ($classReflection->hasConstructor()) {
60-
return $classReflection->getConstructor();
61-
}
62-
63-
return new DummyConstructorReflection($classReflection);
64-
}
65-
66-
$parent = $classReflection->getParentClass();
67-
if ($parent === null) {
68-
return null;
69-
}
70-
71-
return $this->findConsistentParentConstructor($parent);
72-
}
73-
7455
}

tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function getRule(): Rule
3333
$reflectionProvider,
3434
self::getContainer(),
3535
),
36+
new ConsistentConstructorHelper(),
3637
true,
3738
);
3839
}

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function getRule(): Rule
3333
$reflectionProvider,
3434
self::getContainer(),
3535
),
36+
new ConsistentConstructorHelper(),
3637
true,
3738
);
3839
}

tests/PHPStan/Rules/Classes/NewStaticRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protected function getRule(): Rule
1717
{
1818
return new NewStaticRule(
1919
new PhpVersion(PHP_VERSION_ID),
20+
new ConsistentConstructorHelper(),
2021
);
2122
}
2223

tests/PHPStan/Rules/Methods/ConsistentConstructorRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Methods;
44

5+
use PHPStan\Rules\Classes\ConsistentConstructorHelper;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
78
use function sprintf;
@@ -13,6 +14,7 @@ class ConsistentConstructorRuleTest extends RuleTestCase
1314
protected function getRule(): Rule
1415
{
1516
return new ConsistentConstructorRule(
17+
new ConsistentConstructorHelper(),
1618
self::getContainer()->getByType(MethodParameterComparisonHelper::class),
1719
self::getContainer()->getByType(MethodVisibilityComparisonHelper::class),
1820
);

0 commit comments

Comments
 (0)