Skip to content

Commit

Permalink
Add dynamic return type extension for wp_die() (#201)
Browse files Browse the repository at this point in the history
* Add dynamic return type extension for wp_die()

* Update tests for wp_die()

Co-authored-by: Viktor Szépe <viktor@szepe.net>

---------

Co-authored-by: Viktor Szépe <viktor@szepe.net>
  • Loading branch information
IanDelMar and szepeviktor committed Oct 14, 2023
1 parent 76f7835 commit 4ed6cf2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
5 changes: 4 additions & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ services:
class: SzepeViktor\PHPStan\WordPress\WpParseUrlFunctionDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: SzepeViktor\PHPStan\WordPress\WpDieDynamicFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: SzepeViktor\PHPStan\WordPress\HookDocsVisitor
tags:
Expand Down Expand Up @@ -124,7 +128,6 @@ parameters:
- SAVEQUERIES
- SCRIPT_DEBUG
earlyTerminatingFunctionCalls:
- wp_die
- wp_send_json
- wp_send_json_success
- wp_send_json_error
Expand Down
53 changes: 53 additions & 0 deletions src/WpDieDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Set return type of wp_die().
*/

declare(strict_types=1);

namespace SzepeViktor\PHPStan\WordPress;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use PHPStan\Type\NeverType;

class WpDieDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'wp_die';
}

// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();

// Called without $args parameter
if (count($args) < 3) {
return new NeverType();
}

$argType = $scope->getType($args[2]->value);

// Return void for non constant arrays.
if (! $argType->isConstantArray()->yes()) {
return new VoidType();
}

// Return never if the key 'exit' is not set.
if (! $argType->hasOffsetValueType(new ConstantStringType('exit'))->yes()) {
return new NeverType();
}

// Note WP's wp_die handlers do lazy comparison
return $argType->getOffsetValueType(new ConstantStringType('exit'))->toBoolean()->isTrue()->yes()
? new NeverType()
: new VoidType();
}
}
1 change: 1 addition & 0 deletions tests/DynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/term_exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_error_parameter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_parse_url.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_die.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme_get.php');
}

Expand Down
15 changes: 15 additions & 0 deletions tests/data/wp_die.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace SzepeViktor\PHPStan\WordPress\Tests;

use function PHPStan\Testing\assertType;

/** @var array $array */
$array = null;

assertType('*NEVER*', wp_die('', ''));
assertType('*NEVER*', wp_die('', '', ['exit' => true]));
assertType('void', wp_die('', '', ['exit' => false]));
assertType('void', wp_die('', '', $array));

0 comments on commit 4ed6cf2

Please sign in to comment.