|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| - |
4 | 3 | namespace TheCodingMachine\Safe\PHPStan\Rules;
|
5 | 4 |
|
6 | 5 | use PhpParser\Node;
|
|
19 | 18 | */
|
20 | 19 | class UseSafeFunctionsRule implements Rule
|
21 | 20 | {
|
| 21 | + /** |
| 22 | + * @see JSON_THROW_ON_ERROR |
| 23 | + */ |
| 24 | + const JSON_THROW_ON_ERROR = 4194304; |
| 25 | + |
22 | 26 | public function getNodeType(): string
|
23 | 27 | {
|
24 | 28 | return Node\Expr\FuncCall::class;
|
25 | 29 | }
|
26 | 30 |
|
27 | 31 | public function processNode(Node $node, Scope $scope): array
|
28 | 32 | {
|
29 |
| - if (!$node->name instanceof Node\Name) { |
| 33 | + $nodeName = $node->name; |
| 34 | + if (!$nodeName instanceof Node\Name) { |
30 | 35 | return [];
|
31 | 36 | }
|
32 |
| - $functionName = $node->name->toString(); |
| 37 | + $functionName = $nodeName->toString(); |
33 | 38 | $unsafeFunctions = FunctionListLoader::getFunctionList();
|
34 | 39 |
|
35 | 40 | if (isset($unsafeFunctions[$functionName])) {
|
@@ -58,7 +63,7 @@ public function processNode(Node $node, Scope $scope): array
|
58 | 63 | return [];
|
59 | 64 | }
|
60 | 65 |
|
61 |
| - return [new SafeFunctionRuleError($node->name, $node->getStartLine())]; |
| 66 | + return [new SafeFunctionRuleError($nodeName, $node->getStartLine())]; |
62 | 67 | }
|
63 | 68 |
|
64 | 69 | return [];
|
@@ -87,11 +92,16 @@ private function argValueIncludeJSONTHROWONERROR(?Arg $arg): bool
|
87 | 92 | return true;
|
88 | 93 | }
|
89 | 94 |
|
90 |
| - return in_array(true, array_map(function ($element) { |
91 |
| - // JSON_THROW_ON_ERROR == 4194304 |
92 |
| - return ($element & 4194304) == 4194304; |
93 |
| - }, array_filter($options, function ($element) { |
94 |
| - return is_int($element); |
95 |
| - })), true); |
| 95 | + $intOptions = array_filter($options, function (mixed $option): bool { |
| 96 | + return is_int($option); |
| 97 | + }); |
| 98 | + |
| 99 | + foreach ($intOptions as $option) { |
| 100 | + if (($option & self::JSON_THROW_ON_ERROR) === self::JSON_THROW_ON_ERROR) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return false; |
96 | 106 | }
|
97 | 107 | }
|
0 commit comments