-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathFunctionPurityCheck.php
148 lines (133 loc) · 4.2 KB
/
FunctionPurityCheck.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
137
138
139
140
141
142
143
144
145
146
147
148
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Pure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PHPStan\Analyser\ImpurePoint;
use PHPStan\Analyser\ThrowPoint;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Rules\Functions\CallToFunctionStatementWithoutSideEffectsRule;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Type;
use function array_filter;
use function count;
use function in_array;
use function lcfirst;
use function sprintf;
final class FunctionPurityCheck
{
/**
* @param 'Function'|'Method' $identifier
* @param ExtendedParameterReflection[] $parameters
* @param ImpurePoint[] $impurePoints
* @param ThrowPoint[] $throwPoints
* @param Stmt[] $statements
* @return list<IdentifierRuleError>
*/
public function check(
string $functionDescription,
string $identifier,
FunctionReflection|ExtendedMethodReflection $functionReflection,
array $parameters,
Type $returnType,
array $impurePoints,
array $throwPoints,
array $statements,
bool $isConstructor,
): array
{
$errors = [];
$isPure = $functionReflection->isPure();
if ($isPure->yes()) {
foreach ($parameters as $parameter) {
if (!$parameter->passedByReference()->createsNewVariable()) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf(
'%s is marked as pure but parameter $%s is passed by reference.',
$functionDescription,
$parameter->getName(),
))->identifier(sprintf('pure%s.parameterByRef', $identifier))->build();
}
if ($returnType->isVoid()->yes() && !$isConstructor) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s is marked as pure but returns void.',
$functionDescription,
))->identifier(sprintf('pure%s.void', $identifier))->build();
}
foreach ($impurePoints as $impurePoint) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s %s in pure %s.',
$impurePoint->isCertain() ? 'Impure' : 'Possibly impure',
$impurePoint->getDescription(),
lcfirst($functionDescription),
))
->line($impurePoint->getNode()->getStartLine())
->identifier(sprintf(
'%s.%s',
$impurePoint->isCertain() ? 'impure' : 'possiblyImpure',
$impurePoint->getIdentifier(),
))
->build();
}
} elseif ($isPure->no()) {
if (
count($throwPoints) === 0
&& count($impurePoints) === 0
&& count($functionReflection->getAsserts()->getAll()) === 0
&& (
!$functionReflection instanceof ExtendedMethodReflection
|| $functionReflection->isFinal()->yes()
|| $functionReflection->getDeclaringClass()->isFinal()
)
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s is marked as impure but does not have any side effects.',
$functionDescription,
))->identifier(sprintf('impure%s.pure', $identifier))->build();
}
} elseif ($returnType->isVoid()->yes()) {
if (
count($throwPoints) === 0
&& count($impurePoints) === 0
&& !$isConstructor
&& (!$functionReflection instanceof ExtendedMethodReflection || $functionReflection->isPrivate())
&& count($functionReflection->getAsserts()->getAll()) === 0
) {
$hasByRef = false;
foreach ($parameters as $parameter) {
if (!$parameter->passedByReference()->createsNewVariable()) {
continue;
}
$hasByRef = true;
break;
}
$statements = array_filter($statements, static function (Stmt $stmt): bool {
if ($stmt instanceof Stmt\Nop) {
return false;
}
if (!$stmt instanceof Stmt\Expression) {
return true;
}
if (!$stmt->expr instanceof FuncCall) {
return true;
}
if (!$stmt->expr->name instanceof Name) {
return true;
}
return !in_array($stmt->expr->name->toString(), CallToFunctionStatementWithoutSideEffectsRule::PHPSTAN_TESTING_FUNCTIONS, true);
});
if (!$hasByRef && count($statements) > 0) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s returns void but does not have any side effects.',
$functionDescription,
))->identifier('void.pure')->build();
}
}
}
return $errors;
}
}