-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic return type extension for
wp_die()
(#201)
* Add dynamic return type extension for wp_die() * Update tests for wp_die() Co-authored-by: Viktor Szépe <[email protected]> --------- Co-authored-by: Viktor Szépe <[email protected]>
- Loading branch information
1 parent
76f7835
commit 4ed6cf2
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |