Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp_die() and PHPStan #96

Closed
jcvignoli opened this issue Jan 6, 2025 · 7 comments
Closed

wp_die() and PHPStan #96

jcvignoli opened this issue Jan 6, 2025 · 7 comments

Comments

@jcvignoli
Copy link
Contributor

I haven't found any phpstan specific instruction in the stubs, so I would like to ask you wether it's ok to add the following comment for wp_die():

 * @phpstan-return ($args is array{exit: false}&array ? void : never)

Without it, PHPStan doesn't understand that wp_die() is an exiting function.

@marekdedic
Copy link
Collaborator

If you keep the non-phpstan-specific return as well (so that other tools keep working), then adding this is absolutely ok.

2 questions:

  1. Why is there the &array? Seems confusing to me
  2. While we are poking wp_die, maybe the default return should be changed to never? I suspect for 99% of uses it's what they want...

Thanks!

@jcvignoli
Copy link
Contributor Author

Good!

  1. I Have no idea actually. Just copied-pasted from another stub, and frankly I'm not an expert about phpdoc definitions. I just tried
    * @phpstan-return ($args is array{exit: false} ? void : never)
    which worked out too.
  2. I fully agree! I didn't want to mess out things, didn't know if was ok to use never (which is never used in your stubs).

I'm gonna make a PR with a @return never. Thanks!

@jcvignoli
Copy link
Contributor Author

Well, actually I commited a @return never but PHPStan check fails:

 1372   Function wp_die() should always throw an exception or terminate  
         script execution but doesn't do that.                            
         return.never      

@marekdedic
Copy link
Collaborator

2. didn't know if was ok to use never (which is never used in your stubs).

I think if it passes CI...

Well, actually I commited a @return never but PHPStan check fails:

I think putting exit(); in the implementation will fix this. Or disable this rule in PHPStan config, it doesn't really add much for stubs anyway...

@jcvignoli
Copy link
Contributor Author

I think putting exit(); in the implementation will fix this.
Putting an extra exit() in the code following wp_die()? Well, it wouldn't make sense, right?

PHPStan rule can indeed be disabled, but it would disable also proper issues at the same time.

I therefore used the @phpstan-return option, which passes CI.

@marekdedic
Copy link
Collaborator

PHPStan rule can indeed be disabled, but it would disable also proper issues at the same time.

Hmm, that makes sense... And putting exit() inside the function doesn't work either?

@jcvignoli
Copy link
Contributor Author

Adding an exit() after a wp_die() would throw errors in other static tools (something like the code could not be executed), I guess.

Closing due to the discussion in PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants