Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AssertWpErrorTypeSpecifying extensions #127

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Expand Up @@ -6,6 +6,7 @@

<exclude-pattern>tests/data/*</exclude-pattern>
<exclude-pattern>tests/functions.php</exclude-pattern>
<exclude-pattern>tests/WP_UnitTestCase_Base.php</exclude-pattern>

<rule ref="PSR12NeutronRuleset">
<exclude name="Generic.Files.LineLength"/>
Expand All @@ -15,6 +16,7 @@

<!-- PHPCS does not understand yield -->
<rule ref="NeutronStandard.Functions.TypeHint.UnusedReturnType">
<exclude-pattern>tests/AssertMethodTypeSpecifyingExtensionTest.php</exclude-pattern>
<exclude-pattern>tests/DynamicReturnTypeExtensionTest.php</exclude-pattern>
</rule>
</ruleset>
10 changes: 3 additions & 7 deletions src/AssertNotWpErrorTypeSpecifyingExtension.php
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/AssertWpErrorTypeSpecifyingExtension.php
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/WP_UnitTestCase_Base.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class WP_UnitTestCase_Base extends TestCase
{
/**
* Asserts that the given value is an instance of WP_Error.
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
* @return void
*/
public function assertWPError( $actual, $message = '' ) {
}

/**
* Asserts that the given value is not an instance of WP_Error.
*
* @param mixed $actual The value to check.
* @param string $message Optional. Message to display when the assertion fails.
* @return void
*/
public function assertNotWPError( $actual, $message = '' ) {
}
}
5 changes: 3 additions & 2 deletions tests/data/assert_not_wp_error.php
Expand Up @@ -4,15 +4,16 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use WP_UnitTestCase_Base;
use function PHPStan\Testing\assertType;

class Foo
class AssertNotWpError
{

public function inheritedAssertMethodsNarrowType(): void
{
/** @var \WP_Error|int $no */
$no = $no;
$no = $_GET['no'];

$customAsserter = new class () extends WP_UnitTestCase_Base {};
$customAsserter->assertNotWPError($no);
Expand Down
5 changes: 3 additions & 2 deletions tests/data/assert_wp_error.php
Expand Up @@ -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);
Expand Down