-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ForbiddenFuncCallRule.php
143 lines (122 loc) · 4.12 KB
/
ForbiddenFuncCallRule.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
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use SimpleXMLElement;
use Symplify\PHPStanRules\Formatter\RequiredWithMessageFormatter;
use Symplify\PHPStanRules\Matcher\ArrayStringAndFnMatcher;
use Symplify\PHPStanRules\ValueObject\Configuration\RequiredWithMessage;
use Symplify\RuleDocGenerator\Contract\ConfigurableRuleInterface;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenFuncCallRule\ForbiddenFuncCallRuleTest
*/
final class ForbiddenFuncCallRule implements Rule, DocumentedRuleInterface, ConfigurableRuleInterface
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Function "%s()" cannot be used/left in the code';
/**
* @param string[]|array<string|int, string> $forbiddenFunctions
*/
public function __construct(
private readonly array $forbiddenFunctions,
private readonly ArrayStringAndFnMatcher $arrayStringAndFnMatcher,
private readonly RequiredWithMessageFormatter $requiredWithMessageFormatter,
) {
}
/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return FuncCall::class;
}
/**
* @param FuncCall $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (! $node->name instanceof Name) {
return [];
}
$funcName = $node->name->toString();
$requiredWithMessages = $this->requiredWithMessageFormatter->normalizeConfig($this->forbiddenFunctions);
foreach ($requiredWithMessages as $requiredWithMessage) {
if (! $this->arrayStringAndFnMatcher->isMatch($funcName, [$requiredWithMessage->getRequired()])) {
continue;
}
// special cases
if ($this->shouldAllowSpecialCase($node, $scope, $funcName)) {
continue;
}
$errorMessage = $this->createErrorMessage($requiredWithMessage, $funcName);
return [$errorMessage];
}
return [];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
echo eval('...');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo '...';
CODE_SAMPLE
,
[
'forbiddenFunctions' => ['eval'],
]
),
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
dump($value);
echo $value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo $value;
CODE_SAMPLE
,
[
'forbiddenFunctions' => [
'dump' => 'seems you missed some debugging function',
],
]
),
]);
}
private function shouldAllowSpecialCase(FuncCall $funcCall, Scope $scope, string $functionName): bool
{
if ($functionName !== 'property_exists') {
return false;
}
$arg = $funcCall->getArgs()[0];
$firstArgType = $scope->getType($arg->value);
// non nullable
$firstArgType = TypeCombinator::removeNull($firstArgType);
$simpleXmlElementObjectType = new ObjectType(SimpleXMLElement::class);
return $simpleXmlElementObjectType->isSuperTypeOf($firstArgType)
->yes();
}
private function createErrorMessage(RequiredWithMessage $requiredWithMessage, string $funcName): string
{
if ($requiredWithMessage->getMessage() === null) {
return sprintf(self::ERROR_MESSAGE, $funcName);
}
return sprintf(self::ERROR_MESSAGE . ': ' . $requiredWithMessage->getMessage(), $funcName);
}
}