-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathPromoteParameterRule.php
More file actions
127 lines (108 loc) · 2.95 KB
/
PromoteParameterRule.php
File metadata and controls
127 lines (108 loc) · 2.95 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Playground;
use PhpParser\Node;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\MissingServiceException;
use PHPStan\Rules\FixableNodeRuleError;
use PHPStan\Rules\LazyRegistry;
use PHPStan\Rules\LineRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;
use function get_class;
use function sprintf;
/**
* @template TNodeType of Node
* @implements Rule<TNodeType>
*/
final class PromoteParameterRule implements Rule
{
/** @var Rule<TNodeType>|false|null */
private Rule|false|null $originalRule = null;
/**
* @param Rule<TNodeType> $rule
* @param class-string<TNodeType> $nodeType
*/
public function __construct(
private Rule $rule,
private Container $container,
private string $nodeType,
private bool $parameterValue,
private string $parameterName,
)
{
}
public function getNodeType(): string
{
return $this->nodeType;
}
/**
* @return Rule<TNodeType>|null
*/
private function getOriginalRule(): ?Rule
{
if ($this->originalRule === false) {
return null;
}
if ($this->originalRule !== null) {
return $this->originalRule;
}
$originalRule = null;
try {
/** @var Rule<TNodeType> $originalRule */
$originalRule = $this->container->getByType(get_class($this->rule));
$taggedRules = $this->container->getServicesByTag(LazyRegistry::RULE_TAG);
$found = false;
foreach ($taggedRules as $rule) {
if ($originalRule !== $rule) {
continue;
}
$found = true;
break;
}
if (!$found) {
$originalRule = null;
}
} catch (MissingServiceException) {
// pass
}
if ($originalRule === null) {
$this->originalRule = false;
return null;
}
return $this->originalRule = $originalRule;
}
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
{
if ($this->parameterValue) {
return [];
}
if ($this->nodeType !== $this->rule->getNodeType()) {
return [];
}
$originalRule = $this->getOriginalRule();
if ($originalRule !== null) {
$originalRuleErrors = $originalRule->processNode($node, $scope);
if (count($originalRuleErrors) > 0) {
return [];
}
}
$errors = [];
foreach ($this->rule->processNode($node, $scope) as $error) {
$builder = RuleErrorBuilder::message($error->getMessage())
->identifier('phpstanPlayground.configParameter')
->tip(sprintf('This error would be reported if the <fg=cyan>%s: true</> parameter was enabled in your <fg=cyan>%%configurationFile%%</>.', $this->parameterName));
if ($error instanceof LineRuleError) {
$builder->line($error->getLine());
}
if ($error instanceof FixableNodeRuleError) {
$builder->fixNode($error->getOriginalNode(), $error->getNewNodeCallable());
}
$errors[] = $builder->build();
}
return $errors;
}
}