szepeviktor/phpstan-wordpress

`wp_die()` doesn't end script anymore

Closed this issue ยท 24 comments

Hello,

Sorry to bring bad news. Unless I'm missing something, it seems that something went wrong with #201.

/**
 * @phpstan-return never
 */
function foo() {
	wp_die();
}

This ends with the following message:

Function foo() should always throw an exception or terminate script execution but doesn't do that.

Versions used:

php-stubs/wordpress-stubs v6.3.2
szepeviktor/phpstan-wordpress v1.3.1

@IanDelMar Please hotfix this ๐Ÿ”ฅ

Seems like @return never is does not mean termination.

@IanDelMar Wait! wp_die got no @return never in #110!

Looks like a test is needed for assertType('*NEVER*', wp_die('')); too (no second parameter).

Was at work. Will have a look at it now.

I added tests for wp_die() and wp_die(''). Both tests pass, because the extension returns "never" if the number of arguments is less than 3:

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

Okay.
What could make the above code snippet fail?

@Screenfeed What is you phpstan-wordpress and wp-stubs version?

@szepeviktor Sorry I should have mentioned that in the OP.

php-stubs/wordpress-stubs v6.3.2
szepeviktor/phpstan-wordpress v1.3.1

Does PHPStan know about the dynamic return type of a function when that function is called by another function?

Does PHPStan know about the dynamic return type of a function when that function is called by another function?

Yes.

\PHPStan\dumpType(wp_die()); says NEVER.

@IanDelMar Adding back wp_die to earlyTerminatingFunctionCalls resolves the problem.
#203 (comment)

@ondrejmirtes Could you help us?
Is there a programmatic way to mark a function early terminating?
wp_die sometimes exits, other times not.

Look at this:

/**
 * @phpstan-return never
 */
function foo() {
	\PHPStan\dumpType(wp_die());
	wp_die();
}
\PHPStan\dumpType(foo());
:51    Dumped type: *NEVER*
:52    Function foo() should always throw an exception or terminate script execution but doesn't do that.
:54    Dumped type: never

๐Ÿ˜ฒ

There is a NonAcceptingNeverType, is that the correct type to return in the extension?

Yess please!

I was wondering why it's '*NEVER*' not just 'never', but was not aware that there is another never type (NonAcceptingNeverType) and thought PHPStan will know that '*NEVER*' is 'never'.

WordPress will *NEVER* die()

Yeah, use new NeverType(true) or new NonAcceptingNeverType(). The NeverType needs to be explicit: https://github.com/phpstan/phpstan-src/blob/485922f5c2734af5af15230250340e6c1cc76451/src/Analyser/NodeScopeResolver.php#L1846-L1848

Thank you.

@Screenfeed This release is for you. https://github.com/szepeviktor/phpstan-wordpress/releases/tag/v1.3.2

Yes! It's ok for us ๐Ÿ‘ Thank you so much ๐Ÿ˜‰

@szepeviktor Perfect. Thanks a lot to everyone! โค๏ธ