diff --git a/phpcs.xml.dist b/phpcs.xml.dist index b9824c5..5b1d2ee 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -6,6 +6,7 @@ tests/data/* tests/functions.php + tests/WP_UnitTestCase_Base.php @@ -15,6 +16,7 @@ + tests/AssertMethodTypeSpecifyingExtensionTest.php tests/DynamicReturnTypeExtensionTest.php diff --git a/src/AssertNotWpErrorTypeSpecifyingExtension.php b/src/AssertNotWpErrorTypeSpecifyingExtension.php index e512e72..401f684 100644 --- a/src/AssertNotWpErrorTypeSpecifyingExtension.php +++ b/src/AssertNotWpErrorTypeSpecifyingExtension.php @@ -29,23 +29,19 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool { - return strtolower($methodReflection->getName()) === 'assertNotWPError' + return strtolower($methodReflection->getName()) === 'assertnotwperror' && isset($node->args[0]) - && !$context->null(); + && $context->null(); } // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes { - if ($context->null()) { - throw new \PHPStan\ShouldNotHappenException(); - } - $expr = $node->getArgs()[0]->value; $typeBefore = $scope->getType($expr); $type = TypeCombinator::remove($typeBefore, new ObjectType('WP_Error')); - return $this->typeSpecifier->create($expr, $type, $context); + return $this->typeSpecifier->create($expr, $type, TypeSpecifierContext::createTruthy()); } public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void diff --git a/src/AssertWpErrorTypeSpecifyingExtension.php b/src/AssertWpErrorTypeSpecifyingExtension.php index e0608cb..27db6ce 100644 --- a/src/AssertWpErrorTypeSpecifyingExtension.php +++ b/src/AssertWpErrorTypeSpecifyingExtension.php @@ -28,21 +28,17 @@ public function getClass(): string public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool { - return strtolower($methodReflection->getName()) === 'assertWPError' + return strtolower($methodReflection->getName()) === 'assertwperror' && isset($node->args[0]) - && !$context->null(); + && $context->null(); } // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes { - if ($context->null()) { - throw new \PHPStan\ShouldNotHappenException(); - } - $args = $node->getArgs(); - return $this->typeSpecifier->create($args[0]->value, new ObjectType('WP_Error'), $context); + return $this->typeSpecifier->create($args[0]->value, new ObjectType('WP_Error'), TypeSpecifierContext::createTruthy()); } public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void diff --git a/tests/WP_UnitTestCase_Base.php b/tests/WP_UnitTestCase_Base.php new file mode 100644 index 0000000..6079da0 --- /dev/null +++ b/tests/WP_UnitTestCase_Base.php @@ -0,0 +1,28 @@ +assertNotWPError($no); diff --git a/tests/data/assert_wp_error.php b/tests/data/assert_wp_error.php index 93b6462..7785805 100644 --- a/tests/data/assert_wp_error.php +++ b/tests/data/assert_wp_error.php @@ -4,15 +4,16 @@ namespace SzepeViktor\PHPStan\WordPress\Tests; +use WP_UnitTestCase_Base; use function PHPStan\Testing\assertType; -class Foo +class AssertWpError { public function inheritedAssertMethodsNarrowType(): void { /** @var \WP_Error|int $yes */ - $yes = $yes; + $yes = $_GET['yes']; $customAsserter = new class () extends WP_UnitTestCase_Base {}; $customAsserter->assertWPError($yes);