Repository navigation
Expand file tree
/
Copy pathConstantConditionInTraitHelper.php
More file actions
85 lines (74 loc) · 2.02 KB
/
Copy pathConstantConditionInTraitHelper.php
File metadata and controls
85 lines (74 loc) · 2.02 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
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|Name $expr): string
{
return sprintf('%s:%d', $expr instanceof Name ? $expr->toString() : $this->exprPrinter->printExpr($expr), $expr->getStartLine());
}
/**
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitNoError(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr|Name $expr,
): void
{
if (!$scope->isInTrait()) {
return;
}
$scope->emitCollectedData(ConstantConditionInTraitCollector::class, [
$ruleName,
$scope->getTraitReflection()->getName(),
$this->exprString($expr),
null,
]);
}
/**
* @param class-string<Rule<covariant Node>> $ruleName
*/
public function emitError(
string $ruleName,
Scope&NodeCallbackInvoker&CollectedDataEmitter $scope,
Expr|Name $expr,
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(),
$this->exprString($expr),
$value,
$this->ruleErrorTransformer->transform($ruleError, $scope, [], $expr),
]);
}
}