/
CallToFunctionStatementWithoutSideEffectsRule.php
136 lines (111 loc) · 3.25 KB
/
CallToFunctionStatementWithoutSideEffectsRule.php
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
128
129
130
131
132
133
134
135
136
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use function in_array;
use function sprintf;
/**
* @implements Rule<Node\Stmt\Expression>
*/
class CallToFunctionStatementWithoutSideEffectsRule implements Rule
{
private const SIDE_EFFECT_FLIP_PARAMETERS = [
// functionName => [name, pos, testName, defaultHasSideEffect]
'file_get_contents' => ['context', 2, 'isNotNull', false],
'print_r' => ['return', 1, 'isTruthy', true],
'var_export' => ['return', 1, 'isTruthy', true],
];
public function __construct(private ReflectionProvider $reflectionProvider)
{
}
public function getNodeType(): string
{
return Node\Stmt\Expression::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->expr instanceof Node\Expr\FuncCall) {
return [];
}
$funcCall = $node->expr;
if (!($funcCall->name instanceof Node\Name)) {
return [];
}
if (!$this->reflectionProvider->hasFunction($funcCall->name, $scope)) {
return [];
}
$function = $this->reflectionProvider->getFunction($funcCall->name, $scope);
$functionName = $function->getName();
$functionHasSideEffects = !$function->hasSideEffects()->no();
if (in_array($functionName, [
'PHPStan\\dumpType',
'PHPStan\\Testing\\assertType',
'PHPStan\\Testing\\assertNativeType',
'PHPStan\\Testing\\assertVariableCertainty',
], true)) {
return [];
}
if (isset(self::SIDE_EFFECT_FLIP_PARAMETERS[$functionName])) {
[
$flipParameterName,
$flipParameterPosision,
$testName,
$defaultHasSideEffect,
] = self::SIDE_EFFECT_FLIP_PARAMETERS[$functionName];
$sideEffectFlipped = false;
$hasNamedParameter = false;
$checker = [
'isNotNull' => static fn (Type $type) => $type->isNull()->no(),
'isTruthy' => static fn (Type $type) => $type->toBoolean()->isTrue()->yes(),
][$testName];
foreach ($funcCall->getRawArgs() as $i => $arg) {
if (!$arg instanceof Arg) {
return [];
}
$isFlipParameter = false;
if ($arg->name !== null) {
$hasNamedParameter = true;
if ($arg->name->name === $flipParameterName) {
$isFlipParameter = true;
}
}
if (!$hasNamedParameter && $i === $flipParameterPosision) {
$isFlipParameter = true;
}
if ($isFlipParameter) {
$sideEffectFlipped = $checker($scope->getType($arg->value));
break;
}
}
if ($sideEffectFlipped xor $defaultHasSideEffect) {
return [];
}
$functionHasSideEffects = false;
}
if (!$functionHasSideEffects || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $function->getThrowType();
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}
$functionResult = $scope->getType($funcCall);
if ($functionResult instanceof NeverType && $functionResult->isExplicit()) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Call to function %s() on a separate line has no effect.',
$function->getName(),
))->build(),
];
}
return [];
}
}