Skip to content

Commit c97f8bf

Browse files
committed
chore: PHPCS/PHPStan remediations
1 parent 2c18631 commit c97f8bf

File tree

4 files changed

+59
-84
lines changed

4 files changed

+59
-84
lines changed

phpcs.xml.dist

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
<!-- Deprecated: @todo remove in PHPCS 4.0 -->
5959
<exclude name="WordPressVIPMinimum.JS" />
6060
</rule>
61-
<rule ref="WordPress-Extra" />
61+
<rule ref="WordPress-Extra">
62+
<!-- Needed to typehint, see: https://github.com/WordPress/WordPress-Coding-Standards/issues/403 -->
63+
<exclude name="Generic.Commenting.DocComment.MissingShort" />
64+
</rule>
6265

6366
<!--
6467
Load PHPCompatibility & PHPCompatibilityWP
@@ -70,7 +73,8 @@
7073
Checks from PCP
7174
@see https://github.com/wordpress/plugin-check
7275
-->
73-
<rule ref="./vendor/wpackagist-plugin/plugin-check/vendor/plugin-check/phpcs-sniffs/PluginCheck/ruleset.xml" />
76+
<rule
77+
ref="./vendor/wpackagist-plugin/plugin-check/vendor/plugin-check/phpcs-sniffs/PluginCheck/ruleset.xml" />
7478
<rule ref="./vendor/wpackagist-plugin/plugin-check/phpcs-rulesets/plugin-check.ruleset.xml" />
7579

7680
<!-- Check for superfluous whitespace - These get suppressed in VIP-GO.-->
@@ -89,13 +93,6 @@
8993
</properties>
9094
</rule>
9195

92-
<!-- Allow inheritDoc comments -->
93-
<rule ref="Squiz.Commenting.FunctionComment">
94-
<properties>
95-
<property name="skipIfInheritdoc" value="true" />
96-
</properties>
97-
</rule>
98-
9996
<!--
10097
Slevomat Coding Standards to keep our code clean and modern.
10198
See: https://github.com/slevomat/coding-standard/tree/master#alphabetical-list-of-sniffs
@@ -164,14 +161,16 @@
164161
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses" />
165162
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon" />
166163

164+
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints" />
167165
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue" />
168166
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition" />
169167
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
170168
<properties>
171169
<property name="enableMixedTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
172170
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
173171
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
174-
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
172+
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
173+
PHP 8.2+ -->
175174
</properties>
176175
</rule>
177176
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
@@ -180,7 +179,8 @@
180179
<property name="enableMixedTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
181180
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
182181
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
183-
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
182+
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
183+
PHP 8.2+ -->
184184
</properties>
185185
</rule>
186186
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
@@ -190,7 +190,8 @@
190190
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
191191
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
192192
<property name="enableNeverTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
193-
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
193+
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
194+
PHP 8.2+ -->
194195
</properties>
195196
</rule>
196197

@@ -206,34 +207,6 @@
206207
<!--
207208
Ignore Rules for Tests
208209
-->
209-
<!-- Do not require doc-blocks for unit tests -->
210-
<rule ref="Squiz.Commenting.FunctionComment.Missing">
211-
<exclude-pattern>tests/*</exclude-pattern>
212-
</rule>
213-
<rule ref="Squiz.Commenting.FileComment.Missing">
214-
<exclude-pattern>tests/*</exclude-pattern>
215-
</rule>
216-
<rule ref="Squiz.Commenting.ClassComment.Missing">
217-
<exclude-pattern>tests/*</exclude-pattern>
218-
</rule>
219-
<rule ref="Squiz.Commenting.ClassComment.SpacingAfter">
220-
<exclude-pattern>tests/*</exclude-pattern>
221-
</rule>
222-
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
223-
<exclude-pattern>tests/*</exclude-pattern>
224-
</rule>
225-
<rule ref="Generic.Commenting.DocComment.Empty">
226-
<exclude-pattern>tests/*</exclude-pattern>
227-
</rule>
228-
<rule ref="Generic.Commenting.DocComment.MissingShort">
229-
<exclude-pattern>tests/*</exclude-pattern>
230-
</rule>
231-
<rule ref="Squiz.Commenting.VariableComment.Missing">
232-
<exclude-pattern>tests/*</exclude-pattern>
233-
</rule>
234-
<rule ref="Squiz.Commenting.FunctionCommentThrowTag.Missing">
235-
<exclude-pattern>tests/*</exclude-pattern>
236-
</rule>
237210
<rule ref="PluginCheck.CodeAnalysis.Offloading.OffloadedContent">
238211
<exclude-pattern>/tests/*</exclude-pattern>
239212
</rule>
@@ -251,6 +224,7 @@
251224
<properties>
252225
<property name="prefixes" type="array">
253226
<!-- @todo: Should we use a namespace or prefix ? -->
227+
<element value="abilities_api" />
254228
<element value="WP_Ability" />
255229
<element value="WP_Abilities" />
256230
<element value="WP_ABILITIES_API" />

src/abilities-api.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php declare( strict_types = 1 );
2-
1+
<?php
32
/**
43
* Abilities API
54
*
@@ -10,6 +9,8 @@
109
* @since 0.1.0
1110
*/
1211

12+
declare( strict_types = 1 );
13+
1314
/**
1415
* Registers a new ability using Abilities API.
1516
*
@@ -19,13 +20,13 @@
1920
*
2021
* @since 0.1.0
2122
*
22-
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
23-
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
24-
* contain lowercase alphanumeric characters, dashes and the forward slash.
25-
* @param array $properties Optional. An associative array of properties for the ability. This should
26-
* include `label`, `description`, `input_schema`, `output_schema`,
27-
* `execute_callback`, `permission_callback`, and `meta`.
28-
* @return ?WP_Ability An instance of registered ability on success, null on failure.
23+
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance.
24+
* The name must be a string containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
25+
* contain lowercase alphanumeric characters, dashes and the forward slash.
26+
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
27+
* include `label`, `description`, `input_schema`, `output_schema`,
28+
* `execute_callback`, `permission_callback`, and `meta`.
29+
* @return ?\WP_Ability An instance of registered ability on success, null on failure.
2930
*/
3031
function wp_register_ability( $name, array $properties = array() ): ?WP_Ability {
3132
if ( ! did_action( 'abilities_api_init' ) ) {
@@ -35,7 +36,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
3536
/* translators: 1: abilities_api_init, 2: string value of the ability name. */
3637
esc_html__( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ),
3738
'<code>abilities_api_init</code>',
38-
'<code>' . esc_attr( $name ) . '</code>'
39+
'<code>' . esc_html( $name ) . '</code>'
3940
),
4041
'0.1.0'
4142
);
@@ -53,7 +54,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
5354
* @since 0.1.0
5455
*
5556
* @param string $name The name of the registered ability, with its namespace.
56-
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
57+
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
5758
*/
5859
function wp_unregister_ability( string $name ): ?WP_Ability {
5960
return WP_Abilities_Registry::get_instance()->unregister( $name );
@@ -67,7 +68,7 @@ function wp_unregister_ability( string $name ): ?WP_Ability {
6768
* @since 0.1.0
6869
*
6970
* @param string $name The name of the registered ability, with its namespace.
70-
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
71+
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
7172
*/
7273
function wp_get_ability( string $name ): ?WP_Ability {
7374
return WP_Abilities_Registry::get_instance()->get_registered( $name );
@@ -80,7 +81,7 @@ function wp_get_ability( string $name ): ?WP_Ability {
8081
*
8182
* @since 0.1.0
8283
*
83-
* @return WP_Ability[] The array of registered abilities.
84+
* @return \WP_Ability[] The array of registered abilities.
8485
*/
8586
function wp_get_abilities(): array {
8687
return WP_Abilities_Registry::get_instance()->get_all_registered();

src/class-wp-abilities-registry.php

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php declare( strict_types = 1 );
2-
1+
<?php
32
/**
43
* Abilities API
54
*
@@ -10,6 +9,8 @@
109
* @since 0.1.0
1110
*/
1211

12+
declare( strict_types = 1 );
13+
1314
/**
1415
* Manages the registration and lookup of abilities.
1516
*
@@ -21,15 +22,15 @@ final class WP_Abilities_Registry {
2122
* Holds the registered abilities.
2223
*
2324
* @since 0.1.0
24-
* @var WP_Ability[]
25+
* @var \WP_Ability[]
2526
*/
2627
private $registered_abilities = array();
2728

2829
/**
2930
* Container for the main instance of the class.
3031
*
3132
* @since 0.1.0
32-
* @var ?WP_Abilities_Registry
33+
* @var ?\WP_Abilities_Registry
3334
*/
3435
private static $instance = null;
3536

@@ -42,13 +43,13 @@ final class WP_Abilities_Registry {
4243
*
4344
* @since 0.1.0
4445
*
45-
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
46-
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
47-
* contain lowercase alphanumeric characters, dashes and the forward slash.
48-
* @param array $properties Optional. An associative array of properties for the ability. This should
49-
* include `label`, `description`, `input_schema`, `output_schema`,
50-
* `execute_callback`, `permission_callback`, and `meta`.
51-
* @return ?WP_Ability The registered ability instance on success, null on failure.
46+
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
47+
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
48+
* contain lowercase alphanumeric characters, dashes and the forward slash.
49+
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
50+
* include `label`, `description`, `input_schema`, `output_schema`,
51+
* `execute_callback`, `permission_callback`, and `meta`.
52+
* @return ?\WP_Ability The registered ability instance on success, null on failure.
5253
*/
5354
public function register( $name, array $properties = array() ): ?WP_Ability {
5455
$ability = null;
@@ -159,6 +160,7 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
159160
'meta' => $properties['meta'] ?? array(),
160161
)
161162
);
163+
162164
$this->registered_abilities[ $name ] = $ability;
163165
return $ability;
164166
}
@@ -173,9 +175,9 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
173175
* @since 0.1.0
174176
*
175177
* @param string $name The name of the registered ability, with its namespace.
176-
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
178+
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
177179
*/
178-
public function unregister( $name ): ?WP_Ability {
180+
public function unregister( string $name ): ?WP_Ability {
179181
if ( ! $this->is_registered( $name ) ) {
180182
_doing_it_wrong(
181183
__METHOD__,
@@ -201,7 +203,7 @@ public function unregister( $name ): ?WP_Ability {
201203
*
202204
* @since 0.1.0
203205
*
204-
* @return WP_Ability[] The array of registered abilities.
206+
* @return \WP_Ability[] The array of registered abilities.
205207
*/
206208
public function get_all_registered(): array {
207209
return $this->registered_abilities;
@@ -215,7 +217,7 @@ public function get_all_registered(): array {
215217
* @param string $name The name of the registered ability, with its namespace.
216218
* @return bool True if the ability is registered, false otherwise.
217219
*/
218-
public function is_registered( $name ): bool {
220+
public function is_registered( string $name ): bool {
219221
return isset( $this->registered_abilities[ $name ] );
220222
}
221223

@@ -229,9 +231,9 @@ public function is_registered( $name ): bool {
229231
* @since 0.1.0
230232
*
231233
* @param string $name The name of the registered ability, with its namespace.
232-
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
234+
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
233235
*/
234-
public function get_registered( $name ): ?WP_Ability {
236+
public function get_registered( string $name ): ?WP_Ability {
235237
if ( ! $this->is_registered( $name ) ) {
236238
_doing_it_wrong(
237239
__METHOD__,
@@ -251,10 +253,10 @@ public function get_registered( $name ): ?WP_Ability {
251253
*
252254
* @since 0.1.0
253255
*
254-
* @return WP_Abilities_Registry The main registry instance.
256+
* @return \WP_Abilities_Registry The main registry instance.
255257
*/
256-
public static function get_instance(): WP_Abilities_Registry {
257-
/* @var WP_Abilities_Registry $wp_abilities */
258+
public static function get_instance(): self {
259+
/** @var \WP_Abilities_Registry $wp_abilities */
258260
global $wp_abilities;
259261

260262
if ( empty( $wp_abilities ) ) {
@@ -267,7 +269,7 @@ public static function get_instance(): WP_Abilities_Registry {
267269
*
268270
* @since 0.1.0
269271
*
270-
* @param WP_Abilities_Registry $instance Abilities registry object.
272+
* @param \WP_Abilities_Registry $instance Abilities registry object.
271273
*/
272274
do_action( 'abilities_api_init', $wp_abilities );
273275
}
@@ -279,15 +281,12 @@ public static function get_instance(): WP_Abilities_Registry {
279281
* Wakeup magic method.
280282
*
281283
* @since 0.1.0
284+
* @throws \UnexpectedValueException If any of the registered abilities is not an instance of WP_Ability.
282285
*/
283286
public function __wakeup(): void {
284-
if ( empty( $this->registered_abilities ) ) {
285-
return;
286-
}
287-
288287
foreach ( $this->registered_abilities as $ability ) {
289288
if ( ! $ability instanceof WP_Ability ) {
290-
throw new UnexpectedValueException();
289+
throw new \UnexpectedValueException();
291290
}
292291
}
293292
}

src/class-wp-ability.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php declare( strict_types = 1 );
2-
1+
<?php
32
/**
43
* Abilities API
54
*
@@ -10,6 +9,8 @@
109
* @since 0.1.0
1110
*/
1211

12+
declare( strict_types = 1 );
13+
1314
/**
1415
* Encapsulates the properties and methods related to a specific ability in the registry.
1516
*
@@ -235,7 +236,7 @@ public function has_permission( array $input = array() ): bool {
235236
* @since 0.1.0
236237
*
237238
* @param array $input The input data for the ability.
238-
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
239+
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
239240
*/
240241
protected function do_execute( array $input ) {
241242
if ( ! is_callable( $this->execute_callback ) ) {
@@ -293,7 +294,7 @@ protected function validate_output( $output ): bool {
293294
* @since 0.1.0
294295
*
295296
* @param array $input Optional. The input data for the ability.
296-
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
297+
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
297298
*/
298299
public function execute( array $input = array() ) {
299300
if ( ! $this->has_permission( $input ) ) {
@@ -326,6 +327,6 @@ public function execute( array $input = array() ) {
326327
* @since 0.1.0
327328
*/
328329
public function __wakeup(): void {
329-
throw new \LogicException( __CLASS__ . ' should never be unserialized.' );
330+
throw new \LogicException( self::class . ' should never be unserialized.' );
330331
}
331332
}

0 commit comments

Comments
 (0)