Repository navigation
Expand file tree
/
Copy pathConstantConditionInTraitHelper.php
More file actions
117 lines (104 loc) · 2.85 KB
/
Copy pathConstantConditionInTraitHelper.php
File metadata and controls
117 lines (104 loc) · 2.85 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\CollectedDataEmitter;
use PHPStan\Analyser\NodeCallbackInvoker;
use PHPStan\Analyser\RuleErrorTransformer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\FixableNodeRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\ShouldNotHappenException;
use function sprintf;
#[AutowiredService]
final class ConstantConditionInTraitHelper
{
public function __construct(
private ExprPrinter $exprPrinter,
private RuleErrorTransformer $ruleErrorTransformer,
)
{
}
private function exprString(Expr $expr): string
{
return sprintf('%s:%d', $this->exprPrinter->printExpr($expr), $expr->getStartLine());
}
/**
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitNoError(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr $expr,
): void
{
$this->emitNoErrorForKey($ruleName, $scope, $this->exprString($expr));
}
/**
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitError(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr $expr,
bool $value,
RuleError $ruleError,
): void
{
$this->emitErrorForKey($ruleName, $scope, $expr, $this->exprString($expr), $value, $ruleError);
}
/**
* Like emitNoError(), but for callers that cannot key their check by a single Expr
* (e.g. one Rule node covering several distinct checks at the same location).
*
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitNoErrorForKey(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
string $key,
): void
{
if (!$scope->isInTrait()) {
return;
}
$scope->emitCollectedData(ConstantConditionInTraitCollector::class, [
$ruleName,
$scope->getTraitReflection()->getName(),
$key,
null,
]);
}
/**
* Like emitError(), but for callers that cannot key their check by a single Expr
* (e.g. one Rule node covering several distinct checks at the same location).
*
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitErrorForKey(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Node $node,
string $key,
bool $value,
RuleError $ruleError,
): void
{
if ($ruleError instanceof FixableNodeRuleError) {
throw new ShouldNotHappenException('Fixable errors are not supported by ConstantConditionInTraitHelper.');
}
if (!$scope->isInTrait()) {
return;
}
$scope->emitCollectedData(ConstantConditionInTraitCollector::class, [
$ruleName,
$scope->getTraitReflection()->getName(),
$key,
$value,
$this->ruleErrorTransformer->transform($ruleError, $scope, [], $node),
]);
}
}