Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 0 additions & 50 deletions src/AssertNotWpErrorTypeSpecifyingExtension.php

This file was deleted.

25 changes: 17 additions & 8 deletions src/AssertWpErrorTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
<?php

/**
* Set specified type of WP_UnitTestCase_Base::assertWPError().
* Set specified type of WP_UnitTestCase_Base::assertWPError and
* WP_UnitTestCase_Base::assertNotWPError.
*/

declare(strict_types=1);

namespace SzepeViktor\PHPStan\WordPress;

use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;

class AssertWpErrorTypeSpecifyingExtension implements \PHPStan\Type\MethodTypeSpecifyingExtension, \PHPStan\Analyser\TypeSpecifierAwareExtension
{
private const ASSERT = 'assertWPError';
private const ASSERT_NOT = 'assertNotWPError';

private TypeSpecifier $typeSpecifier;

public function getClass(): string
Expand All @@ -27,21 +33,24 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return strtolower($methodReflection->getName()) === 'assertwperror'
return in_array($methodReflection->getName(), [self::ASSERT, self::ASSERT_NOT], true)
&& isset($node->args[0])
&& $context->null();
}

// 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
);
}

Expand Down