szepeviktor / phpstan-wordpress

WordPress extensions for PHPStan ⛏️
https://packagist.org/packages/szepeviktor/phpstan-wordpress
MIT License
262 stars 26 forks source link

`wp_die()` not exclusively an early terminating function #200

Closed IanDelMar closed 10 months ago

IanDelMar commented 10 months ago

wp_die() is treated as if it terminated script execution early: https://github.com/szepeviktor/phpstan-wordpress/blob/76f783564edc0bd8f4f3a2e44f6c09a1092702d5/extension.neon#L126-L127

But for wp_die('', '', ['exit' => false]) the script execution will not be terminated in any of WP's wp_die handlers.

if (somethingIsTrue()) {
    $foo = true;
} elseif (orSomethingElseIsTrue()) {
    $foo = false;
} else {
    wp_die('', '', ['exit' => false])
}

doFoo($foo); // won't catch possibly undefined variable $foo
szepeviktor commented 10 months ago

WordPress is so weird PHPStan cannot follow. There seems to be no setEarlyTerminating method in PHPStan. https://github.com/search?q=repo%3Aphpstan%2Fphpstan-src%20earlyTerminating&type=code

What is your plan? Never use ['exit' => false] again? 🙃

IanDelMar commented 10 months ago

What is your plan? Never use ['exit' => false] again? 🙃

:laughing:

Don't know. Maybe putting 'wp_die' => ['($args is not non-empty-array ? never : void)'], into the functionMap.php of WordPress stubs?

Or adding an extension checking for the exit key in $args and then either use the never or the void type.

szepeviktor commented 10 months ago

Okay! I am in! Please add that and add a few (2) tests too.

Thank you.

szepeviktor commented 10 months ago

Related https://github.com/php-stubs/wordpress-stubs/issues/36