forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsistentConstructorRule.php
More file actions
55 lines (45 loc) · 1.52 KB
/
ConsistentConstructorRule.php
File metadata and controls
55 lines (45 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Methods;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\InClassMethodNode;
use PHPStan\Rules\Classes\ConsistentConstructorHelper;
use PHPStan\Rules\Rule;
use function array_merge;
use function strtolower;
/** @implements Rule<InClassMethodNode> */
#[RegisteredRule(level: 0)]
final class ConsistentConstructorRule implements Rule
{
public function __construct(
private ConsistentConstructorHelper $consistentConstructorHelper,
private MethodParameterComparisonHelper $methodParameterComparisonHelper,
private MethodVisibilityComparisonHelper $methodVisibilityComparisonHelper,
)
{
}
public function getNodeType(): string
{
return InClassMethodNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$method = $node->getMethodReflection();
if (strtolower($method->getName()) !== '__construct') {
return [];
}
$parent = $method->getDeclaringClass()->getParentClass();
if ($parent === null) {
return [];
}
$parentConstructor = $this->consistentConstructorHelper->findConsistentConstructor($parent);
if ($parentConstructor === null) {
return [];
}
return array_merge(
$this->methodParameterComparisonHelper->compare($parentConstructor, $parentConstructor->getDeclaringClass(), $method, true),
$this->methodVisibilityComparisonHelper->compare($parentConstructor, $parentConstructor->getDeclaringClass(), $method, $node->getOriginalNode()->name),
);
}
}