Skip to content

Commit af38fed

Browse files
committed
fix: add PHPStan type assertions to REST controllers
Add local @var non-falsy-string assertions in register_routes() methods so PHPStan can verify register_rest_route namespace parameter type.
1 parent 74961af commit af38fed

7 files changed

Lines changed: 24 additions & 10 deletions

src/Rest/DuplicateController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public function __construct( ?ModelRestPolicy $policy = null ) {
3030
* Register the REST route for post duplication.
3131
*/
3232
public function register_routes(): void {
33+
/** @var non-falsy-string $namespace */
34+
$namespace = $this->namespace;
3335
register_rest_route(
34-
$this->namespace,
36+
$namespace,
3537
'/' . $this->rest_base . '/(?P<post_id>\d+)',
3638
[
3739
'methods' => WP_REST_Server::CREATABLE,

src/Rest/ExportController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public function __construct( ?ModelRestPolicy $policy = null, ?SaltusSingleExpor
3232
* Register the REST route for post export.
3333
*/
3434
public function register_routes(): void {
35+
/** @var non-falsy-string $namespace */
36+
$namespace = $this->namespace;
3537
\register_rest_route(
36-
$this->namespace,
38+
$namespace,
3739
'/' . $this->rest_base . '/(?P<post_id>\d+)',
3840
[
3941
'methods' => WP_REST_Server::READABLE,

src/Rest/HealthController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public function __construct( string $version, ?AuditLogger $audit_logger = null
2929
* Register the health route.
3030
*/
3131
public function register_routes(): void {
32+
/** @var non-falsy-string $namespace */
33+
$namespace = $this->namespace;
3234
register_rest_route(
33-
$this->namespace,
35+
$namespace,
3436
'/' . $this->rest_base,
3537
[
3638
'methods' => WP_REST_Server::READABLE,

src/Rest/MetaController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public function register_routes(): void {
4141
return;
4242
}
4343

44+
/** @var non-falsy-string $namespace */
45+
$namespace = $this->namespace;
4446
register_rest_route(
45-
$this->namespace,
47+
$namespace,
4648
'/' . $this->rest_base,
4749
[
4850
'methods' => WP_REST_Server::READABLE,
@@ -52,7 +54,7 @@ public function register_routes(): void {
5254
);
5355

5456
register_rest_route(
55-
$this->namespace,
57+
$namespace,
5658
'/' . $this->rest_base . '/(?P<post_type>[a-z0-9_-]+)',
5759
[
5860
'methods' => WP_REST_Server::READABLE,
@@ -69,7 +71,7 @@ public function register_routes(): void {
6971
);
7072

7173
register_rest_route(
72-
$this->namespace,
74+
$namespace,
7375
'/' . $this->rest_base . '/(?P<post_type>[a-z0-9_-]+)/(?P<post_id>\d+)',
7476
[
7577
'methods' => WP_REST_Server::EDITABLE,

src/Rest/ModelsController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function __construct( Modeler $modeler, ?ModelRestPolicy $policy = null )
3535
* Register the REST routes for listing and reading models.
3636
*/
3737
public function register_routes(): void {
38+
/** @var non-falsy-string $namespace */
39+
$namespace = $this->namespace;
3840
register_rest_route(
39-
$this->namespace,
41+
$namespace,
4042
'/' . $this->rest_base,
4143
[
4244
'methods' => WP_REST_Server::READABLE,
@@ -46,7 +48,7 @@ public function register_routes(): void {
4648
);
4749

4850
register_rest_route(
49-
$this->namespace,
51+
$namespace,
5052
'/' . $this->rest_base . '/(?P<post_type>[a-z0-9_-]+)',
5153
[
5254
'methods' => WP_REST_Server::READABLE,

src/Rest/ReorderController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public function __construct( ?ModelRestPolicy $policy = null, ?ReorderPostsServi
3333
* Register the REST route for reordering posts.
3434
*/
3535
public function register_routes(): void {
36+
/** @var non-falsy-string $namespace */
37+
$namespace = $this->namespace;
3638
register_rest_route(
37-
$this->namespace,
39+
$namespace,
3840
'/' . $this->rest_base,
3941
[
4042
'methods' => WP_REST_Server::CREATABLE,

src/Rest/SettingsController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public function __construct( ?ModelRestPolicy $policy = null, ?SettingsManager $
3333
* Register the REST routes for reading and updating settings.
3434
*/
3535
public function register_routes(): void {
36+
/** @var non-falsy-string $namespace */
37+
$namespace = $this->namespace;
3638
register_rest_route(
37-
$this->namespace,
39+
$namespace,
3840
'/' . $this->rest_base . '/(?P<post_type>[a-z0-9_-]+)',
3941
[
4042
[

0 commit comments

Comments
 (0)