Repository navigation
Expand file tree
/
Copy pathPipeOperatorRule.php
More file actions
68 lines (58 loc) · 1.53 KB
/
Copy pathPipeOperatorRule.php
File metadata and controls
68 lines (58 loc) · 1.53 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function sprintf;
/**
* @implements Rule<Node\Expr\BinaryOp\Pipe>
*/
#[RegisteredRule(level: 0)]
final class PipeOperatorRule implements Rule
{
public function __construct(private RuleLevelHelper $ruleLevelHelper)
{
}
public function getNodeType(): string
{
return Node\Expr\BinaryOp\Pipe::class;
}
public function processNode(Node $node, Scope $scope): array
{
$rightType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->right,
'',
static fn (Type $type) => $type->isCallable()->yes(),
)->getType();
if ($rightType instanceof ErrorType) {
return [];
}
if (!$rightType->isCallable()->yes()) {
return [];
}
$acceptors = $rightType->getCallableParametersAcceptors($scope);
foreach ($acceptors as $acceptor) {
foreach ($acceptor->getParameters() as $parameter) {
if ($parameter->passedByReference()->no()) {
break;
}
return [
RuleErrorBuilder::message(sprintf(
'Parameter #1%s of callable on the right side of pipe operator is passed by reference.',
$parameter->getName() !== '' ? ' $' . $parameter->getName() : '',
))
->line($node->right->getStartLine())
->identifier('pipe.byRef')
->build(),
];
}
}
return [];
}
}