Skip to content

Commit

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

* Fix wp_die() extension

* Bump min PHPStan

---------

Co-authored-by: Viktor Szépe <viktor@szepe.net>
  • Loading branch information
IanDelMar and szepeviktor authored Oct 16, 2023
1 parent 78db560 commit b8516ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"require": {
"php": "^7.2 || ^8.0",
"php-stubs/wordpress-stubs": "^4.7 || ^5.0 || ^6.0",
"phpstan/phpstan": "^1.10.0",
"phpstan/phpstan": "^1.10.30",
"symfony/polyfill-php73": "^1.12.0"
},
"require-dev": {
Expand Down
8 changes: 4 additions & 4 deletions src/WpDieDynamicFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NonAcceptingNeverType;

class WpDieDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{
Expand All @@ -30,7 +30,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

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

$argType = $scope->getType($args[2]->value);
Expand All @@ -42,12 +42,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

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

// Note WP's wp_die handlers do lazy comparison
return $argType->getOffsetValueType(new ConstantStringType('exit'))->toBoolean()->isTrue()->yes()
? new NeverType()
? new NonAcceptingNeverType()
: new VoidType();
}
}
6 changes: 4 additions & 2 deletions tests/data/wp_die.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
/** @var array $array */
$array = null;

assertType('*NEVER*', wp_die('', ''));
assertType('*NEVER*', wp_die('', '', ['exit' => true]));
assertType('never', wp_die());
assertType('never', wp_die(''));
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 b8516ed

Please sign in to comment.