Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 16 additions & 40 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@
<!-- Deprecated: @todo remove in PHPCS 4.0 -->
<exclude name="WordPressVIPMinimum.JS" />
</rule>
<rule ref="WordPress-Extra" />
<rule ref="WordPress-Extra">
<exclude name="WordPress.WP.I18n.MissingArgDomain" />
<exclude name="WordPress.NamingConventions.PrefixAllGlobals" />
<!-- Needed to typehint, see: https://github.com/WordPress/WordPress-Coding-Standards/issues/403 -->
<exclude name="Generic.Commenting.DocComment.MissingShort" />
</rule>

<!--
Load PHPCompatibility & PHPCompatibilityWP
Expand All @@ -70,7 +75,8 @@
Checks from PCP
@see https://github.com/wordpress/plugin-check
-->
<rule ref="./vendor/wpackagist-plugin/plugin-check/vendor/plugin-check/phpcs-sniffs/PluginCheck/ruleset.xml" />
<rule
ref="./vendor/wpackagist-plugin/plugin-check/vendor/plugin-check/phpcs-sniffs/PluginCheck/ruleset.xml" />
<rule ref="./vendor/wpackagist-plugin/plugin-check/phpcs-rulesets/plugin-check.ruleset.xml" />

<!-- Check for superfluous whitespace - These get suppressed in VIP-GO.-->
Expand All @@ -89,13 +95,6 @@
</properties>
</rule>

<!-- Allow inheritDoc comments -->
<rule ref="Squiz.Commenting.FunctionComment">
<properties>
<property name="skipIfInheritdoc" value="true" />
</properties>
</rule>

<!--
Slevomat Coding Standards to keep our code clean and modern.
See: https://github.com/slevomat/coding-standard/tree/master#alphabetical-list-of-sniffs
Expand Down Expand Up @@ -164,14 +163,16 @@
<rule ref="SlevomatCodingStandard.PHP.UselessParentheses" />
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon" />

<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints" />
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue" />
<rule ref="SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition" />
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<properties>
<property name="enableMixedTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
PHP 8.2+ -->
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
Expand All @@ -180,7 +181,8 @@
<property name="enableMixedTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
PHP 8.2+ -->
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
Expand All @@ -190,7 +192,8 @@
<property name="enableUnionTypeHint" value="false" /><!-- Only available in PHP 8.0+ -->
<property name="enableIntersectionTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
<property name="enableNeverTypeHint" value="false" /><!-- Only available in PHP 8.1+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in PHP 8.2+ -->
<property name="enableStandaloneNullTrueFalseTypeHints" value="false" /><!-- Only available in
PHP 8.2+ -->
</properties>
</rule>

Expand Down Expand Up @@ -222,34 +225,6 @@
<!--
Ignore Rules for Tests
-->
<!-- Do not require doc-blocks for unit tests -->
<rule ref="Squiz.Commenting.FunctionComment.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FileComment.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.ClassComment.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.ClassComment.SpacingAfter">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Generic.Commenting.DocComment.Empty">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Generic.Commenting.DocComment.MissingShort">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.VariableComment.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting.FunctionCommentThrowTag.Missing">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="PluginCheck.CodeAnalysis.Offloading.OffloadedContent">
<exclude-pattern>/tests/*</exclude-pattern>
</rule>
Expand All @@ -267,6 +242,7 @@
<properties>
<property name="prefixes" type="array">
<!-- @todo: Should we use a namespace or prefix ? -->
<element value="abilities_api" />
<element value="WP_Ability" />
<element value="WP_Abilities" />
<element value="WP_ABILITIES_API" />
Expand Down
27 changes: 14 additions & 13 deletions src/abilities-api.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare( strict_types = 1 );

<?php
/**
* Abilities API
*
Expand All @@ -10,6 +9,8 @@
* @since 0.1.0
*/

declare( strict_types = 1 );

/**
* Registers a new ability using Abilities API.
*
Expand All @@ -19,13 +20,13 @@
*
* @since 0.1.0
*
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
* contain lowercase alphanumeric characters, dashes and the forward slash.
* @param array $properties Optional. An associative array of properties for the ability. This should
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
* @return ?WP_Ability An instance of registered ability on success, null on failure.
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance.
* The name must be a string containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
* contain lowercase alphanumeric characters, dashes and the forward slash.
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
* @return ?\WP_Ability An instance of registered ability on success, null on failure.
*/
function wp_register_ability( $name, array $properties = array() ): ?WP_Ability {
if ( ! did_action( 'abilities_api_init' ) ) {
Expand All @@ -35,7 +36,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
/* translators: 1: abilities_api_init, 2: string value of the ability name. */
esc_html__( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ),
'<code>abilities_api_init</code>',
'<code>' . esc_attr( $name ) . '</code>'
'<code>' . esc_html( $name instanceof WP_Ability ? $name->get_name() : $name ) . '</code>'
),
'0.1.0'
);
Expand All @@ -53,7 +54,7 @@ function wp_register_ability( $name, array $properties = array() ): ?WP_Ability
* @since 0.1.0
*
* @param string $name The name of the registered ability, with its namespace.
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
*/
function wp_unregister_ability( string $name ): ?WP_Ability {
return WP_Abilities_Registry::get_instance()->unregister( $name );
Expand All @@ -67,7 +68,7 @@ function wp_unregister_ability( string $name ): ?WP_Ability {
* @since 0.1.0
*
* @param string $name The name of the registered ability, with its namespace.
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
*/
function wp_get_ability( string $name ): ?WP_Ability {
return WP_Abilities_Registry::get_instance()->get_registered( $name );
Expand All @@ -80,7 +81,7 @@ function wp_get_ability( string $name ): ?WP_Ability {
*
* @since 0.1.0
*
* @return WP_Ability[] The array of registered abilities.
* @return \WP_Ability[] The array of registered abilities.
*/
function wp_get_abilities(): array {
return WP_Abilities_Registry::get_instance()->get_all_registered();
Expand Down
57 changes: 24 additions & 33 deletions src/class-wp-abilities-registry.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare( strict_types = 1 );

<?php
/**
* Abilities API
*
Expand All @@ -10,6 +9,8 @@
* @since 0.1.0
*/

declare( strict_types = 1 );

/**
* Manages the registration and lookup of abilities.
*
Expand All @@ -21,18 +22,10 @@ final class WP_Abilities_Registry {
* Holds the registered abilities.
*
* @since 0.1.0
* @var WP_Ability[]
* @var \WP_Ability[]
*/
private $registered_abilities = array();

/**
* Container for the main instance of the class.
*
* @since 0.1.0
* @var ?WP_Abilities_Registry
*/
private static $instance = null;

/**
* Registers a new ability.
*
Expand All @@ -42,13 +35,13 @@ final class WP_Abilities_Registry {
*
* @since 0.1.0
*
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
* contain lowercase alphanumeric characters, dashes and the forward slash.
* @param array $properties Optional. An associative array of properties for the ability. This should
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
* @return ?WP_Ability The registered ability instance on success, null on failure.
* @param string|\WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
* contain lowercase alphanumeric characters, dashes and the forward slash.
* @param array<string,mixed> $properties Optional. An associative array of properties for the ability. This should
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
* @return ?\WP_Ability The registered ability instance on success, null on failure.
*/
public function register( $name, array $properties = array() ): ?WP_Ability {
$ability = null;
Expand Down Expand Up @@ -159,6 +152,7 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
'meta' => $properties['meta'] ?? array(),
)
);

$this->registered_abilities[ $name ] = $ability;
return $ability;
}
Expand All @@ -173,9 +167,9 @@ public function register( $name, array $properties = array() ): ?WP_Ability {
* @since 0.1.0
*
* @param string $name The name of the registered ability, with its namespace.
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
* @return ?\WP_Ability The unregistered ability instance on success, null on failure.
*/
public function unregister( $name ): ?WP_Ability {
public function unregister( string $name ): ?WP_Ability {
if ( ! $this->is_registered( $name ) ) {
_doing_it_wrong(
__METHOD__,
Expand All @@ -201,7 +195,7 @@ public function unregister( $name ): ?WP_Ability {
*
* @since 0.1.0
*
* @return WP_Ability[] The array of registered abilities.
* @return \WP_Ability[] The array of registered abilities.
*/
public function get_all_registered(): array {
return $this->registered_abilities;
Expand All @@ -215,7 +209,7 @@ public function get_all_registered(): array {
* @param string $name The name of the registered ability, with its namespace.
* @return bool True if the ability is registered, false otherwise.
*/
public function is_registered( $name ): bool {
public function is_registered( string $name ): bool {
return isset( $this->registered_abilities[ $name ] );
}

Expand All @@ -229,9 +223,9 @@ public function is_registered( $name ): bool {
* @since 0.1.0
*
* @param string $name The name of the registered ability, with its namespace.
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
* @return ?\WP_Ability The registered ability instance, or null if it is not registered.
*/
public function get_registered( $name ): ?WP_Ability {
public function get_registered( string $name ): ?WP_Ability {
if ( ! $this->is_registered( $name ) ) {
_doing_it_wrong(
__METHOD__,
Expand All @@ -251,10 +245,10 @@ public function get_registered( $name ): ?WP_Ability {
*
* @since 0.1.0
*
* @return WP_Abilities_Registry The main registry instance.
* @return \WP_Abilities_Registry The main registry instance.
*/
public static function get_instance(): WP_Abilities_Registry {
/* @var WP_Abilities_Registry $wp_abilities */
public static function get_instance(): self {
/** @var \WP_Abilities_Registry $wp_abilities */
global $wp_abilities;

if ( empty( $wp_abilities ) ) {
Expand All @@ -267,7 +261,7 @@ public static function get_instance(): WP_Abilities_Registry {
*
* @since 0.1.0
*
* @param WP_Abilities_Registry $instance Abilities registry object.
* @param \WP_Abilities_Registry $instance Abilities registry object.
*/
do_action( 'abilities_api_init', $wp_abilities );
}
Expand All @@ -279,15 +273,12 @@ public static function get_instance(): WP_Abilities_Registry {
* Wakeup magic method.
*
* @since 0.1.0
* @throws \UnexpectedValueException If any of the registered abilities is not an instance of WP_Ability.
*/
public function __wakeup(): void {
if ( empty( $this->registered_abilities ) ) {
return;
}

foreach ( $this->registered_abilities as $ability ) {
if ( ! $ability instanceof WP_Ability ) {
throw new UnexpectedValueException();
throw new \UnexpectedValueException();
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/class-wp-ability.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare( strict_types = 1 );

<?php
/**
* Abilities API
*
Expand All @@ -10,6 +9,8 @@
* @since 0.1.0
*/

declare( strict_types = 1 );

/**
* Encapsulates the properties and methods related to a specific ability in the registry.
*
Expand Down Expand Up @@ -49,17 +50,17 @@
* The optional ability input schema.
*
* @since 0.1.0
* @var array

Check failure on line 53 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@var annotation of property \WP_Ability::$input_schema does not specify type hint for its items.
*/
protected $input_schema = array();

Check failure on line 55 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Property WP_Ability::$input_schema type has no value type specified in iterable type array.

/**
* The optional ability output schema.
*
* @since 0.1.0
* @var array

Check failure on line 61 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@var annotation of property \WP_Ability::$output_schema does not specify type hint for its items.
*/
protected $output_schema = array();

Check failure on line 63 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Property WP_Ability::$output_schema type has no value type specified in iterable type array.

/**
* The ability execute callback.
Expand All @@ -81,9 +82,9 @@
* The optional ability metadata.
*
* @since 0.1.0
* @var array

Check failure on line 85 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@var annotation of property \WP_Ability::$meta does not specify type hint for its items.
*/
protected $meta = array();

Check failure on line 87 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Property WP_Ability::$meta type has no value type specified in iterable type array.

/**
* Constructor.
Expand All @@ -95,11 +96,11 @@
* @since 0.1.0
*
* @param string $name The name of the ability, with its namespace.
* @param array $properties An associative array of properties for the ability. This should

Check failure on line 99 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@param annotation of method \WP_Ability::__construct() does not specify type hint for items of its traversable parameter $properties.
* include `label`, `description`, `input_schema`, `output_schema`,
* `execute_callback`, `permission_callback`, and `meta`.
*/
public function __construct( string $name, array $properties ) {

Check failure on line 103 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::__construct() has parameter $properties with no value type specified in iterable type array.
$this->name = $name;
foreach ( $properties as $property_name => $property_value ) {
$this->$property_name = $property_value;
Expand Down Expand Up @@ -145,9 +146,9 @@
*
* @since 0.1.0
*
* @return array The input schema for the ability.

Check failure on line 149 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@return annotation of method \WP_Ability::get_input_schema() does not specify type hint for items of its traversable return value.
*/
public function get_input_schema(): array {

Check failure on line 151 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::get_input_schema() return type has no value type specified in iterable type array.
return $this->input_schema;
}

Expand All @@ -156,9 +157,9 @@
*
* @since 0.1.0
*
* @return array The output schema for the ability.

Check failure on line 160 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@return annotation of method \WP_Ability::get_output_schema() does not specify type hint for items of its traversable return value.
*/
public function get_output_schema(): array {

Check failure on line 162 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::get_output_schema() return type has no value type specified in iterable type array.
return $this->output_schema;
}

Expand All @@ -167,9 +168,9 @@
*
* @since 0.1.0
*
* @return array The metadata for the ability.

Check failure on line 171 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@return annotation of method \WP_Ability::get_meta() does not specify type hint for items of its traversable return value.
*/
public function get_meta(): array {

Check failure on line 173 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::get_meta() return type has no value type specified in iterable type array.
return $this->meta;
}

Expand All @@ -178,10 +179,10 @@
*
* @since 0.1.0
*
* @param array $input Optional. The input data to validate.

Check failure on line 182 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

@param annotation of method \WP_Ability::validate_input() does not specify type hint for items of its traversable parameter $input.
* @return bool Returns true if valid, false if validation fails.
*/
protected function validate_input( array $input = array() ): bool {

Check failure on line 185 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::validate_input() has parameter $input with no value type specified in iterable type array.
$input_schema = $this->get_input_schema();
if ( empty( $input_schema ) ) {
return true;
Expand Down Expand Up @@ -217,7 +218,7 @@
* @param array $input Optional. The input data for permission checking.
* @return bool Whether the ability has the necessary permission.
*/
public function has_permission( array $input = array() ): bool {

Check failure on line 221 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::has_permission() has parameter $input with no value type specified in iterable type array.
if ( ! $this->validate_input( $input ) ) {
return false;
}
Expand All @@ -235,9 +236,9 @@
* @since 0.1.0
*
* @param array $input The input data for the ability.
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
*/
protected function do_execute( array $input ) {

Check failure on line 241 in src/class-wp-ability.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method WP_Ability::do_execute() has parameter $input with no value type specified in iterable type array.
if ( ! is_callable( $this->execute_callback ) ) {
_doing_it_wrong(
__METHOD__,
Expand Down Expand Up @@ -293,7 +294,7 @@
* @since 0.1.0
*
* @param array $input Optional. The input data for the ability.
* @return mixed|WP_Error The result of the ability execution, or WP_Error on failure.
* @return mixed|\WP_Error The result of the ability execution, or WP_Error on failure.
*/
public function execute( array $input = array() ) {
if ( ! $this->has_permission( $input ) ) {
Expand Down Expand Up @@ -326,6 +327,6 @@
* @since 0.1.0
*/
public function __wakeup(): void {
throw new \LogicException( __CLASS__ . ' should never be unserialized.' );
throw new \LogicException( self::class . ' should never be unserialized.' );
}
}
Loading