Skip to content

Guard register_routes against an empty REST namespace#169

Merged
d4mation merged 1 commit into
mainfrom
fix/phpstan-register-rest-route-namespace
May 29, 2026
Merged

Guard register_routes against an empty REST namespace#169
d4mation merged 1 commit into
mainfrom
fix/phpstan-register-rest-route-namespace

Conversation

@d4mation
Copy link
Copy Markdown
Collaborator

@d4mation d4mation commented May 29, 2026

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 dependency-versions: highest, so it picks up v6.9.4 and PHPStan reports an argument.type error against $this->namespace at every register_rest_route call across the V1 controllers.

$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. This adds an early bail-out at the top of register_routes() in each affected controller so PHPStan sees the narrowed type for the remaining calls.

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.

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.
Copy link
Copy Markdown
Collaborator

@johnhooks johnhooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this weird, but I understand the issue.

@d4mation
Copy link
Copy Markdown
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.

@d4mation d4mation merged commit 0dfdb32 into main May 29, 2026
23 checks passed
@d4mation d4mation deleted the fix/phpstan-register-rest-route-namespace branch May 29, 2026 17:41
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

Successfully merging this pull request may close these issues.

3 participants