From abaeae5a9e9aaf0098e87e59f4baeff1f562803e Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 19 Apr 2022 14:28:52 +0200 Subject: [PATCH 01/10] Handle FuncCall nodes that represent first class callables --- composer.json | 1 + src/Rules/UseSafeFunctionsRule.php | 30 +++++++++++-------- ...afeFunctionsDynamicReturnTypeExtension.php | 19 +++++------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 14771e8..e1b293d 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "thecodingmachine/safe": "^1.0 || ^2.0" }, "require-dev": { + "nikic/php-parser": "^4", "phpunit/phpunit": "^9.6", "php-coveralls/php-coveralls": "^2.1", "squizlabs/php_codesniffer": "^3.4" diff --git a/src/Rules/UseSafeFunctionsRule.php b/src/Rules/UseSafeFunctionsRule.php index d649184..8ba50ac 100644 --- a/src/Rules/UseSafeFunctionsRule.php +++ b/src/Rules/UseSafeFunctionsRule.php @@ -19,6 +19,11 @@ */ class UseSafeFunctionsRule implements Rule { + /** + * @see JSON_THROW_ON_ERROR + */ + const JSON_THROW_ON_ERROR = 4194304; + public function getNodeType(): string { return Node\Expr\FuncCall::class; @@ -33,18 +38,18 @@ public function processNode(Node $node, Scope $scope): array $unsafeFunctions = FunctionListLoader::getFunctionList(); if (isset($unsafeFunctions[$functionName])) { - if ( - $functionName === "json_decode" - && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[3] ?? null) - ) { - return []; - } + if (! $node->isFirstClassCallable()) { + if ($functionName === "json_decode" + && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[3] ?? null) + ) { + return []; + } - if ( - $functionName === "json_encode" - && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[1] ?? null) - ) { - return []; + if ($functionName === "json_encode" + && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[1] ?? null) + ) { + return []; + } } return [new SafeFunctionRuleError($node->name, $node->getStartLine())]; @@ -77,8 +82,7 @@ private function argValueIncludeJSONTHROWONERROR(?Arg $arg): bool } return in_array(true, array_map(function ($element) { - // JSON_THROW_ON_ERROR == 4194304 - return ($element & 4194304) == 4194304; + return ($element & self::JSON_THROW_ON_ERROR) == self::JSON_THROW_ON_ERROR; }, array_filter($options, function ($element) { return is_int($element); })), true); diff --git a/src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php b/src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php index 02711e1..fe1ae33 100644 --- a/src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php +++ b/src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php @@ -56,22 +56,17 @@ private function getPreliminarilyResolvedTypeFromFunctionCall( Scope $scope ): Type { $argumentPosition = $this->functions[$functionReflection->getName()]; - $defaultReturnType = ParametersAcceptorSelector::selectFromArgs( - $scope, - $functionCall->getArgs(), - $functionReflection->getVariants() - ) + + $args = $functionCall->getArgs(); + $variants = $functionReflection->getVariants(); + $defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $args, $variants) ->getReturnType(); - - if (count($functionCall->args) <= $argumentPosition) { - return $defaultReturnType; - } - $subjectArgument = $functionCall->args[$argumentPosition]; - if (!$subjectArgument instanceof Arg) { + if (count($args) <= $argumentPosition) { return $defaultReturnType; } - + + $subjectArgument = $args[$argumentPosition]; $subjectArgumentType = $scope->getType($subjectArgument->value); $mixedType = new MixedType(); if ($subjectArgumentType->isSuperTypeOf($mixedType)->yes()) { From ce42c0824f40d237b606c1737143ece736189be7 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 26 Nov 2024 09:15:00 +0100 Subject: [PATCH 02/10] Raise nikic/php-parser minimum version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e1b293d..f460c47 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "thecodingmachine/safe": "^1.0 || ^2.0" }, "require-dev": { - "nikic/php-parser": "^4", + "nikic/php-parser": "^4.19.4", "phpunit/phpunit": "^9.6", "php-coveralls/php-coveralls": "^2.1", "squizlabs/php_codesniffer": "^3.4" From fde3604fe5daa0551a8151fbd6a199910bcc6084 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 24 Feb 2025 15:54:34 +0100 Subject: [PATCH 03/10] fix merge, add test case --- src/Rules/UseSafeFunctionsRule.php | 16 +++++++++++----- tests/Rules/data/safe_json_decode.php | 4 ++++ tests/Rules/data/safe_json_encode.php | 5 +++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Rules/UseSafeFunctionsRule.php b/src/Rules/UseSafeFunctionsRule.php index 826f667..d024e6d 100644 --- a/src/Rules/UseSafeFunctionsRule.php +++ b/src/Rules/UseSafeFunctionsRule.php @@ -94,10 +94,16 @@ private function argValueIncludeJSONTHROWONERROR(?Arg $arg): bool return true; } - return in_array(true, array_map(function ($element) { - return ($element & self::JSON_THROW_ON_ERROR) == self::JSON_THROW_ON_ERROR; - }, array_filter($options, function ($element) { - return is_int($element); - })), true); + $intOptions = array_filter($options, function (mixed $option): bool { + return is_int($option); + }); + + foreach ($intOptions as $option) { + if (($option & self::JSON_THROW_ON_ERROR) === self::JSON_THROW_ON_ERROR) { + return true; + } + } + + return false; } } diff --git a/tests/Rules/data/safe_json_decode.php b/tests/Rules/data/safe_json_decode.php index 22b450a..69c3980 100644 --- a/tests/Rules/data/safe_json_decode.php +++ b/tests/Rules/data/safe_json_decode.php @@ -12,3 +12,7 @@ // Test named arguments instead of positional json_decode("{}", flags: JSON_THROW_ON_ERROR); +json_decode("{}", flags: JSON_THROW_ON_ERROR); + +// Test first class callable +json_decode(...); diff --git a/tests/Rules/data/safe_json_encode.php b/tests/Rules/data/safe_json_encode.php index 06bb4ec..ad4f152 100644 --- a/tests/Rules/data/safe_json_encode.php +++ b/tests/Rules/data/safe_json_encode.php @@ -1,7 +1,12 @@ Date: Mon, 24 Feb 2025 17:49:01 +0100 Subject: [PATCH 04/10] add failing test --- tests/Rules/UseSafeFunctionsRuleTest.php | 10 ++++++++++ tests/Rules/data/first_class_callable.php | 4 ++++ tests/Rules/data/safe_json_decode.php | 3 --- tests/Rules/data/safe_json_encode.php | 3 --- 4 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 tests/Rules/data/first_class_callable.php diff --git a/tests/Rules/UseSafeFunctionsRuleTest.php b/tests/Rules/UseSafeFunctionsRuleTest.php index 014bd6d..70b2800 100644 --- a/tests/Rules/UseSafeFunctionsRuleTest.php +++ b/tests/Rules/UseSafeFunctionsRuleTest.php @@ -44,4 +44,14 @@ public function testJSONEncodeNoCatchSafe(): void { $this->analyse([__DIR__ . '/data/safe_json_encode.php'], []); } + + public function testFirstClassCallable(): void + { + $this->analyse([__DIR__ . '/data/first_class_callable.php'], [ + [ + "Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", + 3, + ], + ]); + } } diff --git a/tests/Rules/data/first_class_callable.php b/tests/Rules/data/first_class_callable.php new file mode 100644 index 0000000..d20edfa --- /dev/null +++ b/tests/Rules/data/first_class_callable.php @@ -0,0 +1,4 @@ + Date: Mon, 24 Feb 2025 17:50:46 +0100 Subject: [PATCH 05/10] clean up --- tests/Rules/data/safe_json_decode.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Rules/data/safe_json_decode.php b/tests/Rules/data/safe_json_decode.php index 6eaa8f0..22b450a 100644 --- a/tests/Rules/data/safe_json_decode.php +++ b/tests/Rules/data/safe_json_decode.php @@ -12,4 +12,3 @@ // Test named arguments instead of positional json_decode("{}", flags: JSON_THROW_ON_ERROR); -json_decode("{}", flags: JSON_THROW_ON_ERROR); From 3b355df4778e7f9c416bbc03fbcc825fd6fd2da6 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 8 Apr 2025 14:27:58 +0200 Subject: [PATCH 06/10] Introduce new UseSafeCallablesRule --- phpstan-safe-rule.neon | 4 +++ src/Rules/UseSafeCallablesRule.php | 44 +++++++++++++++++++++++ src/Rules/UseSafeFunctionsRule.php | 46 ++++++++++++------------ tests/Rules/UseSafeCallablesRuleTest.php | 27 ++++++++++++++ tests/Rules/UseSafeFunctionsRuleTest.php | 10 ------ 5 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 src/Rules/UseSafeCallablesRule.php create mode 100644 tests/Rules/UseSafeCallablesRuleTest.php diff --git a/phpstan-safe-rule.neon b/phpstan-safe-rule.neon index 5573fe9..d922803 100644 --- a/phpstan-safe-rule.neon +++ b/phpstan-safe-rule.neon @@ -1,4 +1,8 @@ services: + - + class: TheCodingMachine\Safe\PHPStan\Rules\UseSafeCallablesRule + tags: + - phpstan.rules.rule - class: TheCodingMachine\Safe\PHPStan\Rules\UseSafeFunctionsRule tags: diff --git a/src/Rules/UseSafeCallablesRule.php b/src/Rules/UseSafeCallablesRule.php new file mode 100644 index 0000000..58e26c0 --- /dev/null +++ b/src/Rules/UseSafeCallablesRule.php @@ -0,0 +1,44 @@ + + */ +class UseSafeCallablesRule implements Rule +{ + /** + * @see JSON_THROW_ON_ERROR + */ + const JSON_THROW_ON_ERROR = 4194304; + + public function getNodeType(): string + { + return FunctionCallableNode::class; + } + + public function processNode(Node $node, Scope $scope): array + { + $name = $node->getName(); + if (!$name instanceof Node\Name) { + return []; + } + $functionName = $name->toString(); + $unsafeFunctions = FunctionListLoader::getFunctionList(); + + if (isset($unsafeFunctions[$functionName])) { + return [new SafeFunctionRuleError($name, $node->getStartLine())]; + } + + return []; + } +} diff --git a/src/Rules/UseSafeFunctionsRule.php b/src/Rules/UseSafeFunctionsRule.php index d024e6d..d48d4ff 100644 --- a/src/Rules/UseSafeFunctionsRule.php +++ b/src/Rules/UseSafeFunctionsRule.php @@ -1,6 +1,5 @@ name instanceof Node\Name) { + $name = $node->name; + if (!$name instanceof Node\Name) { return []; } - $functionName = $node->name->toString(); + $functionName = $name->toString(); $unsafeFunctions = FunctionListLoader::getFunctionList(); if (isset($unsafeFunctions[$functionName])) { - if (! $node->isFirstClassCallable()) { - if ($functionName === "json_decode" || $functionName === "json_encode") { - foreach ($node->args as $arg) { - if ($arg instanceof Node\Arg && - $arg->name instanceof Node\Identifier && - $arg->name->toLowerString() === "flags" - ) { - if ($this->argValueIncludeJSONTHROWONERROR($arg)) { - return []; - } + if ($functionName === "json_decode" || $functionName === "json_encode") { + foreach ($node->args as $arg) { + if ($arg instanceof Node\Arg && + $arg->name instanceof Node\Identifier && + $arg->name->toLowerString() === "flags" + ) { + if ($this->argValueIncludeJSONTHROWONERROR($arg)) { + return []; } } } + } - if ($functionName === "json_decode" - && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[3] ?? null) - ) { - return []; - } + if ($functionName === "json_decode" + && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[3] ?? null) + ) { + return []; + } - if ($functionName === "json_encode" - && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[1] ?? null) - ) { - return []; - } + if ($functionName === "json_encode" + && $this->argValueIncludeJSONTHROWONERROR($node->getArgs()[1] ?? null) + ) { + return []; } - return [new SafeFunctionRuleError($node->name, $node->getStartLine())]; + return [new SafeFunctionRuleError($name, $node->getStartLine())]; } return []; diff --git a/tests/Rules/UseSafeCallablesRuleTest.php b/tests/Rules/UseSafeCallablesRuleTest.php new file mode 100644 index 0000000..8028bf0 --- /dev/null +++ b/tests/Rules/UseSafeCallablesRuleTest.php @@ -0,0 +1,27 @@ + + */ +class UseSafeCallablesRuleTest extends RuleTestCase +{ + protected function getRule(): Rule + { + return new UseSafeCallablesRule(); + } + + public function testFirstClassCallable(): void + { + $this->analyse([__DIR__ . '/data/first_class_callable.php'], [ + [ + "Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", + 3, + ], + ]); + } +} diff --git a/tests/Rules/UseSafeFunctionsRuleTest.php b/tests/Rules/UseSafeFunctionsRuleTest.php index 70b2800..014bd6d 100644 --- a/tests/Rules/UseSafeFunctionsRuleTest.php +++ b/tests/Rules/UseSafeFunctionsRuleTest.php @@ -44,14 +44,4 @@ public function testJSONEncodeNoCatchSafe(): void { $this->analyse([__DIR__ . '/data/safe_json_encode.php'], []); } - - public function testFirstClassCallable(): void - { - $this->analyse([__DIR__ . '/data/first_class_callable.php'], [ - [ - "Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", - 3, - ], - ]); - } } From 58a32e6565492733dc2fe3c35151977cdd894c17 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 8 Apr 2025 14:30:26 +0200 Subject: [PATCH 07/10] clean up variable names --- src/Rules/UseSafeCallablesRule.php | 8 ++++---- src/Rules/UseSafeFunctionsRule.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Rules/UseSafeCallablesRule.php b/src/Rules/UseSafeCallablesRule.php index 58e26c0..a6388fe 100644 --- a/src/Rules/UseSafeCallablesRule.php +++ b/src/Rules/UseSafeCallablesRule.php @@ -28,15 +28,15 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - $name = $node->getName(); - if (!$name instanceof Node\Name) { + $nodeName = $node->getName(); + if (!$nodeName instanceof Node\Name) { return []; } - $functionName = $name->toString(); + $functionName = $nodeName->toString(); $unsafeFunctions = FunctionListLoader::getFunctionList(); if (isset($unsafeFunctions[$functionName])) { - return [new SafeFunctionRuleError($name, $node->getStartLine())]; + return [new SafeFunctionRuleError($nodeName, $node->getStartLine())]; } return []; diff --git a/src/Rules/UseSafeFunctionsRule.php b/src/Rules/UseSafeFunctionsRule.php index d48d4ff..0d77b12 100644 --- a/src/Rules/UseSafeFunctionsRule.php +++ b/src/Rules/UseSafeFunctionsRule.php @@ -30,11 +30,11 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { - $name = $node->name; - if (!$name instanceof Node\Name) { + $nodeName = $node->name; + if (!$nodeName instanceof Node\Name) { return []; } - $functionName = $name->toString(); + $functionName = $nodeName->toString(); $unsafeFunctions = FunctionListLoader::getFunctionList(); if (isset($unsafeFunctions[$functionName])) { @@ -63,7 +63,7 @@ public function processNode(Node $node, Scope $scope): array return []; } - return [new SafeFunctionRuleError($name, $node->getStartLine())]; + return [new SafeFunctionRuleError($nodeName, $node->getStartLine())]; } return []; From 42290af3c9851d024a2f2cf7377e3fe0830a42e8 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 8 Apr 2025 15:02:48 +0200 Subject: [PATCH 08/10] clean up tests, improve coverage --- tests/Rules/UseSafeCallables/expr.php | 4 ++++ tests/Rules/UseSafeCallables/native_safe.php | 3 +++ tests/Rules/UseSafeCallables/unsafe.php | 3 +++ tests/Rules/UseSafeCallables/use_safe.php | 5 ++++ tests/Rules/UseSafeCallablesRuleTest.php | 19 +++++++++++++-- .../{data => UseSafeClassesRule}/datetime.php | 0 tests/Rules/UseSafeClassesRuleTest.php | 2 +- tests/Rules/UseSafeFunctionsRule/expr.php | 4 ++++ .../native_safe.php} | 15 ++++++++++++ tests/Rules/UseSafeFunctionsRule/unsafe.php | 3 +++ .../use_safe.php} | 6 ++++- tests/Rules/UseSafeFunctionsRuleTest.php | 23 ++++++++----------- tests/Rules/data/first_class_callable.php | 4 ---- tests/Rules/data/fopen.php | 5 ---- tests/Rules/data/safe_fopen.php | 6 ----- tests/Rules/data/safe_json_encode.php | 9 -------- tests/Rules/data/undirect_call.php | 5 ---- 17 files changed, 69 insertions(+), 47 deletions(-) create mode 100644 tests/Rules/UseSafeCallables/expr.php create mode 100644 tests/Rules/UseSafeCallables/native_safe.php create mode 100644 tests/Rules/UseSafeCallables/unsafe.php create mode 100644 tests/Rules/UseSafeCallables/use_safe.php rename tests/Rules/{data => UseSafeClassesRule}/datetime.php (100%) create mode 100644 tests/Rules/UseSafeFunctionsRule/expr.php rename tests/Rules/{data/safe_json_decode.php => UseSafeFunctionsRule/native_safe.php} (54%) create mode 100644 tests/Rules/UseSafeFunctionsRule/unsafe.php rename tests/Rules/{data/safe_pregreplace.php => UseSafeFunctionsRule/use_safe.php} (51%) delete mode 100644 tests/Rules/data/first_class_callable.php delete mode 100644 tests/Rules/data/fopen.php delete mode 100644 tests/Rules/data/safe_fopen.php delete mode 100644 tests/Rules/data/safe_json_encode.php delete mode 100644 tests/Rules/data/undirect_call.php diff --git a/tests/Rules/UseSafeCallables/expr.php b/tests/Rules/UseSafeCallables/expr.php new file mode 100644 index 0000000..0646c60 --- /dev/null +++ b/tests/Rules/UseSafeCallables/expr.php @@ -0,0 +1,4 @@ +analyse([__DIR__ . '/data/first_class_callable.php'], [ + $this->analyse([__DIR__ . '/UseSafeCallables/unsafe.php'], [ [ "Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", 3, ], ]); } + + public function testUseSafe(): void + { + $this->analyse([__DIR__ . '/UseSafeCallables/use_safe.php'], []); + } + + public function testNativeSafe(): void + { + $this->analyse([__DIR__ . '/UseSafeCallables/native_safe.php'], []); + } + + public function testExpr(): void + { + $this->analyse([__DIR__ . '/UseSafeCallables/expr.php'], []); + } } diff --git a/tests/Rules/data/datetime.php b/tests/Rules/UseSafeClassesRule/datetime.php similarity index 100% rename from tests/Rules/data/datetime.php rename to tests/Rules/UseSafeClassesRule/datetime.php diff --git a/tests/Rules/UseSafeClassesRuleTest.php b/tests/Rules/UseSafeClassesRuleTest.php index bfa8aaf..d6ca6b1 100644 --- a/tests/Rules/UseSafeClassesRuleTest.php +++ b/tests/Rules/UseSafeClassesRuleTest.php @@ -17,7 +17,7 @@ protected function getRule(): Rule public function testDateTime(): void { - $this->analyse([__DIR__ . '/data/datetime.php'], [ + $this->analyse([__DIR__ . '/UseSafeClassesRule/datetime.php'], [ [ "Class DateTime is unsafe to use. Its methods can return FALSE instead of throwing an exception. Please add 'use Safe\DateTime;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", 3, diff --git a/tests/Rules/UseSafeFunctionsRule/expr.php b/tests/Rules/UseSafeFunctionsRule/expr.php new file mode 100644 index 0000000..41480b6 --- /dev/null +++ b/tests/Rules/UseSafeFunctionsRule/expr.php @@ -0,0 +1,4 @@ +analyse([__DIR__ . '/data/fopen.php'], [ + $this->analyse([__DIR__ . '/UseSafeFunctionsRule/unsafe.php'], [ [ "Function fopen is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\fopen;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", - 4, + 3, ], ]); } - public function testNoCatchSafe(): void + public function testUseSafe(): void { - $this->analyse([__DIR__ . '/data/safe_fopen.php'], []); + $this->analyse([__DIR__ . '/UseSafeFunctionsRule/use_safe.php'], []); } - public function testExprCall(): void + public function testNativeSafe(): void { - $this->analyse([__DIR__ . '/data/undirect_call.php'], []); + $this->analyse([__DIR__ . '/UseSafeFunctionsRule/native_safe.php'], []); } - public function testJSONDecodeNoCatchSafe(): void + public function testExpr(): void { - $this->analyse([__DIR__ . '/data/safe_json_decode.php'], []); - } - - public function testJSONEncodeNoCatchSafe(): void - { - $this->analyse([__DIR__ . '/data/safe_json_encode.php'], []); + $this->analyse([__DIR__ . '/UseSafeFunctionsRule/expr.php'], []); } } diff --git a/tests/Rules/data/first_class_callable.php b/tests/Rules/data/first_class_callable.php deleted file mode 100644 index d20edfa..0000000 --- a/tests/Rules/data/first_class_callable.php +++ /dev/null @@ -1,4 +0,0 @@ - Date: Tue, 8 Apr 2025 15:05:59 +0200 Subject: [PATCH 09/10] fix phpstan and folder name --- phpstan.neon | 4 +++- .../{UseSafeCallables => UseSafeCallablesRule}/expr.php | 0 .../native_safe.php | 0 .../{UseSafeCallables => UseSafeCallablesRule}/unsafe.php | 0 .../use_safe.php | 0 tests/Rules/UseSafeCallablesRuleTest.php | 8 ++++---- 6 files changed, 7 insertions(+), 5 deletions(-) rename tests/Rules/{UseSafeCallables => UseSafeCallablesRule}/expr.php (100%) rename tests/Rules/{UseSafeCallables => UseSafeCallablesRule}/native_safe.php (100%) rename tests/Rules/{UseSafeCallables => UseSafeCallablesRule}/unsafe.php (100%) rename tests/Rules/{UseSafeCallables => UseSafeCallablesRule}/use_safe.php (100%) diff --git a/phpstan.neon b/phpstan.neon index f4ecada..692e10a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,9 @@ parameters: - src - tests excludePaths: - - tests/Rules/data + - tests/Rules/UseSafeCallablesRule + - tests/Rules/UseSafeClassesRule + - tests/Rules/UseSafeFunctionsRule ignoreErrors: - message: '#^Implementing PHPStan\\Rules\\IdentifierRuleError is not covered by backward compatibility promise\. The interface might change in a minor PHPStan version\.$#' diff --git a/tests/Rules/UseSafeCallables/expr.php b/tests/Rules/UseSafeCallablesRule/expr.php similarity index 100% rename from tests/Rules/UseSafeCallables/expr.php rename to tests/Rules/UseSafeCallablesRule/expr.php diff --git a/tests/Rules/UseSafeCallables/native_safe.php b/tests/Rules/UseSafeCallablesRule/native_safe.php similarity index 100% rename from tests/Rules/UseSafeCallables/native_safe.php rename to tests/Rules/UseSafeCallablesRule/native_safe.php diff --git a/tests/Rules/UseSafeCallables/unsafe.php b/tests/Rules/UseSafeCallablesRule/unsafe.php similarity index 100% rename from tests/Rules/UseSafeCallables/unsafe.php rename to tests/Rules/UseSafeCallablesRule/unsafe.php diff --git a/tests/Rules/UseSafeCallables/use_safe.php b/tests/Rules/UseSafeCallablesRule/use_safe.php similarity index 100% rename from tests/Rules/UseSafeCallables/use_safe.php rename to tests/Rules/UseSafeCallablesRule/use_safe.php diff --git a/tests/Rules/UseSafeCallablesRuleTest.php b/tests/Rules/UseSafeCallablesRuleTest.php index 4c73e71..75ba219 100644 --- a/tests/Rules/UseSafeCallablesRuleTest.php +++ b/tests/Rules/UseSafeCallablesRuleTest.php @@ -17,7 +17,7 @@ protected function getRule(): Rule public function testUnsafe(): void { - $this->analyse([__DIR__ . '/UseSafeCallables/unsafe.php'], [ + $this->analyse([__DIR__ . '/UseSafeCallablesRule/unsafe.php'], [ [ "Function json_encode is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use function Safe\\json_encode;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library.", 3, @@ -27,16 +27,16 @@ public function testUnsafe(): void public function testUseSafe(): void { - $this->analyse([__DIR__ . '/UseSafeCallables/use_safe.php'], []); + $this->analyse([__DIR__ . '/UseSafeCallablesRule/use_safe.php'], []); } public function testNativeSafe(): void { - $this->analyse([__DIR__ . '/UseSafeCallables/native_safe.php'], []); + $this->analyse([__DIR__ . '/UseSafeCallablesRule/native_safe.php'], []); } public function testExpr(): void { - $this->analyse([__DIR__ . '/UseSafeCallables/expr.php'], []); + $this->analyse([__DIR__ . '/UseSafeCallablesRule/expr.php'], []); } } From 9d145a412aee2a5ed61206c2d8b0fdc6496a6c32 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 8 Apr 2025 15:16:51 +0200 Subject: [PATCH 10/10] fix missing coverage --- tests/Rules/UseSafeFunctionsRule/unsafe.php | 2 ++ tests/Rules/UseSafeFunctionsRuleTest.php | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/tests/Rules/UseSafeFunctionsRule/unsafe.php b/tests/Rules/UseSafeFunctionsRule/unsafe.php index 9823bca..4c7854d 100644 --- a/tests/Rules/UseSafeFunctionsRule/unsafe.php +++ b/tests/Rules/UseSafeFunctionsRule/unsafe.php @@ -1,3 +1,5 @@