From 9544f8a6b52dbd6068800bbfc5bdaf67fe04a8c2 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 13 May 2024 09:51:03 +0200 Subject: [PATCH] Do not complain about `impure(Function|Method).pure` if it has `@phpstan-assert` tags --- src/Rules/Pure/FunctionPurityCheck.php | 6 +++++- tests/PHPStan/Rules/Pure/data/pure-method.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Rules/Pure/FunctionPurityCheck.php b/src/Rules/Pure/FunctionPurityCheck.php index 9bd7046986..4e294dc0fe 100644 --- a/src/Rules/Pure/FunctionPurityCheck.php +++ b/src/Rules/Pure/FunctionPurityCheck.php @@ -89,7 +89,11 @@ public function check( ->build(); } } elseif ($isPure->no()) { - if (count($impurePoints) === 0) { + if ( + count($throwPoints) === 0 + && count($impurePoints) === 0 + && count($functionReflection->getAsserts()->getAll()) === 0 + ) { $errors[] = RuleErrorBuilder::message(sprintf( '%s is marked as impure but does not have any side effects.', $functionDescription, diff --git a/tests/PHPStan/Rules/Pure/data/pure-method.php b/tests/PHPStan/Rules/Pure/data/pure-method.php index 2b91f7ea69..bcaa939b76 100644 --- a/tests/PHPStan/Rules/Pure/data/pure-method.php +++ b/tests/PHPStan/Rules/Pure/data/pure-method.php @@ -360,3 +360,18 @@ private function doBaz(): void } } + +class AssertingImpureVoidMethod +{ + + /** + * @param mixed $value + * @phpstan-assert array $value + * @phpstan-impure + */ + public function assertSth($value): void + { + + } + +}