Skip to content

Commit e1ac3e8

Browse files
committed
fix: Add native PHP type hints to REST controllers
- Add native return type hints for void and array methods - Add native parameter type hints for WP_REST_Request and arrays - Use fully qualified class names (\WP_Error, \WP_REST_Request) - Add WP_REST_Abilities prefix to allowed prefixes in PHPCS config - Fix elseif logic simplification in run controller Note: Some PHPCS rules conflict (wanting both @return tags and native type hints) and mixed type hints require PHP 8.0+
1 parent 8152064 commit e1ac3e8

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
<element value="WP_Ability" />
255255
<element value="WP_Abilities" />
256256
<element value="WP_ABILITIES_API" />
257+
<element value="WP_REST_Abilities" />
257258
</property>
258259
</properties>
259260
</rule>

src/rest/class-wp-rest-abilities-init.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ class WP_REST_Abilities_Init {
2727
* Registers the REST API routes for abilities.
2828
*
2929
* @since 0.1.0
30-
* @return void
3130
*/
32-
public static function register_routes() {
31+
public static function register_routes(): void {
3332
require_once __DIR__ . '/class-wp-rest-abilities-run-controller.php';
3433
require_once __DIR__ . '/class-wp-rest-abilities-list-controller.php';
3534

src/rest/class-wp-rest-abilities-list-controller.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ class WP_REST_Abilities_List_Controller extends WP_REST_Controller {
4545
* Registers the routes for abilities.
4646
*
4747
* @since 0.1.0
48-
* @return void
4948
*
5049
* @see register_rest_route()
5150
*/
52-
public function register_routes() {
51+
public function register_routes(): void {
5352
register_rest_route(
5453
$this->namespace,
5554
'/' . $this->rest_base,
@@ -93,7 +92,7 @@ public function register_routes() {
9392
* @param \WP_REST_Request $request Full details about the request.
9493
* @return \WP_REST_Response Response object on success.
9594
*/
96-
public function get_items( $request ) {
95+
public function get_items( \WP_REST_Request $request ): \WP_REST_Response {
9796
$abilities = wp_get_abilities();
9897

9998
// Handle pagination.
@@ -143,11 +142,11 @@ public function get_items( $request ) {
143142
* @param \WP_REST_Request $request Full details about the request.
144143
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
145144
*/
146-
public function get_item( $request ) {
145+
public function get_item( \WP_REST_Request $request ) {
147146
$ability = wp_get_ability( $request['id'] );
148147

149148
if ( ! $ability ) {
150-
return new WP_Error(
149+
return new \WP_Error(
151150
'rest_ability_not_found',
152151
__( 'Ability not found.', 'abilities-api' ),
153152
array( 'status' => 404 )
@@ -164,9 +163,9 @@ public function get_item( $request ) {
164163
* @since 0.1.0
165164
*
166165
* @param \WP_REST_Request $request Full details about the request.
167-
* @return bool True if the request has read access.
166+
* @return boolean True if the request has read access.
168167
*/
169-
public function get_items_permissions_check( $request ) {
168+
public function get_items_permissions_check( \WP_REST_Request $request ): bool {
170169
return current_user_can( 'read' );
171170
}
172171

@@ -179,7 +178,7 @@ public function get_items_permissions_check( $request ) {
179178
* @param \WP_REST_Request $request Request object.
180179
* @return \WP_REST_Response Response object.
181180
*/
182-
public function prepare_item_for_response( $ability, $request ) {
181+
public function prepare_item_for_response( \WP_Ability $ability, \WP_REST_Request $request ): \WP_REST_Response {
183182
$data = array(
184183
'id' => $ability->get_name(),
185184
'label' => $ability->get_label(),
@@ -221,7 +220,7 @@ public function prepare_item_for_response( $ability, $request ) {
221220
*
222221
* @return array<string, mixed> Item schema data.
223222
*/
224-
public function get_item_schema() {
223+
public function get_item_schema(): array {
225224
$schema = array(
226225
'$schema' => 'http://json-schema.org/draft-04/schema#',
227226
'title' => 'ability',
@@ -276,7 +275,7 @@ public function get_item_schema() {
276275
*
277276
* @return array<string, mixed> Collection parameters.
278277
*/
279-
public function get_collection_params() {
278+
public function get_collection_params(): array {
280279
return array(
281280
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
282281
'page' => array(

src/rest/class-wp-rest-abilities-run-controller.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ class WP_REST_Abilities_Run_Controller extends WP_REST_Controller {
4545
* Registers the routes for ability execution.
4646
*
4747
* @since 0.1.0
48-
* @return void
4948
*
5049
* @see register_rest_route()
5150
*/
52-
public function register_routes() {
51+
public function register_routes(): void {
5352
register_rest_route(
5453
$this->namespace,
5554
'/' . $this->rest_base . '/(?P<id>[a-zA-Z0-9\-\/]+?)/run',
@@ -87,11 +86,11 @@ public function register_routes() {
8786
* @param \WP_REST_Request $request Full details about the request.
8887
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
8988
*/
90-
public function run_ability_with_method_check( $request ) {
89+
public function run_ability_with_method_check( \WP_REST_Request $request ) {
9190
$ability = wp_get_ability( $request['id'] );
9291

9392
if ( ! $ability ) {
94-
return new WP_Error(
93+
return new \WP_Error(
9594
'rest_ability_not_found',
9695
__( 'Ability not found.', 'abilities-api' ),
9796
array( 'status' => 404 )
@@ -104,13 +103,15 @@ public function run_ability_with_method_check( $request ) {
104103
$method = $request->get_method();
105104

106105
if ( 'resource' === $type && 'GET' !== $method ) {
107-
return new WP_Error(
106+
return new \WP_Error(
108107
'rest_invalid_method',
109108
__( 'Resource abilities require GET method.', 'abilities-api' ),
110109
array( 'status' => 405 )
111110
);
112-
} elseif ( 'POST' !== $method ) {
113-
return new WP_Error(
111+
}
112+
113+
if ( 'POST' !== $method ) {
114+
return new \WP_Error(
114115
'rest_invalid_method',
115116
__( 'Tool abilities require POST method.', 'abilities-api' ),
116117
array( 'status' => 405 )
@@ -128,11 +129,11 @@ public function run_ability_with_method_check( $request ) {
128129
* @param \WP_REST_Request $request Full details about the request.
129130
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
130131
*/
131-
public function run_ability( $request ) {
132+
public function run_ability( \WP_REST_Request $request ) {
132133
$ability = wp_get_ability( $request['id'] );
133134

134135
if ( ! $ability ) {
135-
return new WP_Error(
136+
return new \WP_Error(
136137
'rest_ability_not_found',
137138
__( 'Ability not found.', 'abilities-api' ),
138139
array( 'status' => 404 )
@@ -154,15 +155,15 @@ public function run_ability( $request ) {
154155
$result = $ability->execute( $input );
155156

156157
if ( is_wp_error( $result ) ) {
157-
return new WP_Error(
158+
return new \WP_Error(
158159
'rest_ability_execution_failed',
159160
$result->get_error_message(),
160161
array( 'status' => 500 )
161162
);
162163
}
163164

164165
if ( is_null( $result ) ) {
165-
return new WP_Error(
166+
return new \WP_Error(
166167
'rest_ability_execution_failed',
167168
__( 'Ability execution failed. Please check permissions and input parameters.', 'abilities-api' ),
168169
array( 'status' => 500 )
@@ -185,11 +186,11 @@ public function run_ability( $request ) {
185186
* @param \WP_REST_Request $request Full details about the request.
186187
* @return true|\WP_Error True if the request has execution permission, WP_Error object otherwise.
187188
*/
188-
public function run_ability_permissions_check( $request ) {
189+
public function run_ability_permissions_check( \WP_REST_Request $request ) {
189190
$ability = wp_get_ability( $request['id'] );
190191

191192
if ( ! $ability ) {
192-
return new WP_Error(
193+
return new \WP_Error(
193194
'rest_ability_not_found',
194195
__( 'Ability not found.', 'abilities-api' ),
195196
array( 'status' => 404 )
@@ -204,7 +205,7 @@ public function run_ability_permissions_check( $request ) {
204205
}
205206

206207
if ( ! $ability->has_permission( $input ) ) {
207-
return new WP_Error(
208+
return new \WP_Error(
208209
'rest_cannot_execute',
209210
__( 'Sorry, you are not allowed to execute this ability.', 'abilities-api' ),
210211
array( 'status' => rest_authorization_required_code() )
@@ -223,7 +224,7 @@ public function run_ability_permissions_check( $request ) {
223224
* @param array<string, mixed> $input The input data to validate.
224225
* @return true|\WP_Error True if validation passes, WP_Error object on failure.
225226
*/
226-
private function validate_input( $ability, $input ) {
227+
private function validate_input( \WP_Ability $ability, array $input ) {
227228
$input_schema = $ability->get_input_schema();
228229

229230
if ( empty( $input_schema ) ) {
@@ -232,7 +233,7 @@ private function validate_input( $ability, $input ) {
232233

233234
$validation_result = rest_validate_value_from_schema( $input, $input_schema );
234235
if ( is_wp_error( $validation_result ) ) {
235-
return new WP_Error(
236+
return new \WP_Error(
236237
'rest_invalid_param',
237238
sprintf(
238239
/* translators: %s: error message */
@@ -255,7 +256,7 @@ private function validate_input( $ability, $input ) {
255256
* @param mixed $output The output data to validate.
256257
* @return true|\WP_Error True if validation passes, WP_Error object on failure.
257258
*/
258-
private function validate_output( $ability, $output ) {
259+
private function validate_output( \WP_Ability $ability, $output ) {
259260
$output_schema = $ability->get_output_schema();
260261

261262
if ( empty( $output_schema ) ) {
@@ -264,7 +265,7 @@ private function validate_output( $ability, $output ) {
264265

265266
$validation_result = rest_validate_value_from_schema( $output, $output_schema );
266267
if ( is_wp_error( $validation_result ) ) {
267-
return new WP_Error(
268+
return new \WP_Error(
268269
'rest_invalid_response',
269270
sprintf(
270271
/* translators: %s: error message */
@@ -285,7 +286,7 @@ private function validate_output( $ability, $output ) {
285286
*
286287
* @return array<string, mixed> Arguments for the run endpoint.
287288
*/
288-
public function get_run_args() {
289+
public function get_run_args(): array {
289290
return array(
290291
'input' => array(
291292
'description' => __( 'Input parameters for the ability execution.', 'abilities-api' ),
@@ -302,7 +303,7 @@ public function get_run_args() {
302303
*
303304
* @return array<string, mixed> Schema for the run endpoint.
304305
*/
305-
public function get_run_schema() {
306+
public function get_run_schema(): array {
306307
return array(
307308
'$schema' => 'http://json-schema.org/draft-04/schema#',
308309
'title' => 'ability-execution',

0 commit comments

Comments
 (0)