From bf741683658691aed5eec103a9d9e31c92de4774 Mon Sep 17 00:00:00 2001 From: Marian <42134098+IanDelMar@users.noreply.github.com> Date: Fri, 11 Jul 2025 01:06:11 +0200 Subject: [PATCH] Merge Assert(Not)WpErrorTypeSpecifyingExtensions --- extension.neon | 4 -- ...ssertNotWpErrorTypeSpecifyingExtension.php | 50 ------------------- src/AssertWpErrorTypeSpecifyingExtension.php | 25 +++++++--- 3 files changed, 17 insertions(+), 62 deletions(-) delete mode 100644 src/AssertNotWpErrorTypeSpecifyingExtension.php diff --git a/extension.neon b/extension.neon index ddb9185..7c0a5b0 100644 --- a/extension.neon +++ b/extension.neon @@ -25,10 +25,6 @@ services: class: SzepeViktor\PHPStan\WordPress\AssertWpErrorTypeSpecifyingExtension tags: - phpstan.typeSpecifier.methodTypeSpecifyingExtension - - - class: SzepeViktor\PHPStan\WordPress\AssertNotWpErrorTypeSpecifyingExtension - tags: - - phpstan.typeSpecifier.methodTypeSpecifyingExtension rules: - SzepeViktor\PHPStan\WordPress\HookCallbackRule - SzepeViktor\PHPStan\WordPress\HookDocsRule diff --git a/src/AssertNotWpErrorTypeSpecifyingExtension.php b/src/AssertNotWpErrorTypeSpecifyingExtension.php deleted file mode 100644 index 9a5153e..0000000 --- a/src/AssertNotWpErrorTypeSpecifyingExtension.php +++ /dev/null @@ -1,50 +0,0 @@ -getName()) === 'assertnotwperror' - && isset($node->args[0]) - && $context->null(); - } - - // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter - public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes - { - $expr = $node->getArgs()[0]->value; - $typeBefore = $scope->getType($expr); - $type = TypeCombinator::remove($typeBefore, new ObjectType(\WP_Error::class)); - - return $this->typeSpecifier->create($expr, $type, TypeSpecifierContext::createTruthy(), $scope); - } - - public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void - { - $this->typeSpecifier = $typeSpecifier; - } -} diff --git a/src/AssertWpErrorTypeSpecifyingExtension.php b/src/AssertWpErrorTypeSpecifyingExtension.php index 7b35a55..0383ea9 100644 --- a/src/AssertWpErrorTypeSpecifyingExtension.php +++ b/src/AssertWpErrorTypeSpecifyingExtension.php @@ -1,23 +1,29 @@ getName()) === 'assertwperror' + return in_array($methodReflection->getName(), [self::ASSERT, self::ASSERT_NOT], true) && isset($node->args[0]) && $context->null(); } @@ -35,13 +41,16 @@ public function isMethodSupported(MethodReflection $methodReflection, MethodCall // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes { - $args = $node->getArgs(); + $expression = new Instanceof_($node->getArgs()[0]->value, new Name('WP_Error')); + + if ($methodReflection->getName() === self::ASSERT_NOT) { + $expression = new BooleanNot($expression); + } - return $this->typeSpecifier->create( - $args[0]->value, - new ObjectType(\WP_Error::class), + return $this->typeSpecifier->specifyTypesInCondition( + $scope, + $expression, TypeSpecifierContext::createTruthy(), - $scope ); }