Guard register_routes against an empty REST namespace#169
Merged
Conversation
php-stubs/wordpress-stubs tightened the register_rest_route stub between v6.9.1 and v6.9.4 to require non-falsy-string for the first argument. CI resolves dependencies with `--highest`, so it picks up v6.9.4 and PHPStan reports an argument.type error against $this->namespace at every register_rest_route call. $this->namespace is inherited from WP_REST_Controller as an untyped property, so PHPStan cannot narrow it to non-falsy-string from a literal default in the child class. Bail out of register_routes early when the namespace is falsy so the remaining calls see the narrowed type. The check uses ! $this->namespace rather than a strict empty-string comparison because the parent property is untyped and could be set to any falsy value upstream.
johnhooks
approved these changes
May 29, 2026
Collaborator
johnhooks
left a comment
There was a problem hiding this comment.
I find this weird, but I understand the issue.
estevao90
approved these changes
May 29, 2026
Collaborator
Author
|
@johnhooks Yeah, it is weird. But without adding it to our phpstan baseline there isn't a way around it really 🫠 The case never happens, but phpstan thinks it can. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
php-stubs/wordpress-stubstightened theregister_rest_routestub between v6.9.1 and v6.9.4 to requirenon-falsy-stringfor the first argument. CI resolves dependencies withdependency-versions: highest, so it picks up v6.9.4 and PHPStan reports anargument.typeerror against$this->namespaceat everyregister_rest_routecall across the V1 controllers.$this->namespaceis inherited fromWP_REST_Controlleras an untyped property, so PHPStan cannot narrow it tonon-falsy-stringfrom a literal default in the child class. This adds an early bail-out at the top ofregister_routes()in each affected controller so PHPStan sees the narrowed type for the remaining calls.The check uses
! $this->namespacerather than a strict empty-string comparison because the parent property is untyped and could be set to any falsy value upstream.