| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Checker\Empty_Check_Repository |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Checker; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
/** |
| 13 |
* Empty Check Repository class. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Empty_Check_Repository implements Check_Repository { |
| 18 |
|
| 19 |
/** |
| 20 |
* Array map holding all runtime checks. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $runtime_checks = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Array map holding all static checks. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected $static_checks = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Registers a check to the repository. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @param string $slug The checks slug. |
| 41 |
* @param Check $check The Check instance. |
| 42 |
* |
| 43 |
* @throws Exception Thrown if Check does not use correct interface, or slug already exists. |
| 44 |
*/ |
| 45 |
public function register_check( $slug, Check $check ) { |
| 46 |
if ( ! $check instanceof Runtime_Check && ! $check instanceof Static_Check ) { |
| 47 |
throw new Exception( |
| 48 |
sprintf( |
| 49 |
/* translators: %s: The Check slug. */ |
| 50 |
__( 'Check with slug "%s" must be an instance of Runtime_Check or Static_Check.', 'plugin-check' ), |
| 51 |
$slug |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $this->runtime_checks[ $slug ] ) || isset( $this->static_checks[ $slug ] ) ) { |
| 57 |
throw new Exception( |
| 58 |
sprintf( |
| 59 |
/* translators: %s: The Check slug. */ |
| 60 |
__( 'Check slug "%s" is already in use.', 'plugin-check' ), |
| 61 |
$slug |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! $check->get_categories() ) { |
| 67 |
throw new Exception( |
| 68 |
sprintf( |
| 69 |
/* translators: %s: The Check slug. */ |
| 70 |
__( 'Check with slug "%s" has no categories associated with it.', 'plugin-check' ), |
| 71 |
$slug |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$check_array = $check instanceof Runtime_Check ? 'runtime_checks' : 'static_checks'; |
| 77 |
$this->{$check_array}[ $slug ] = $check; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns an array of checks. |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* |
| 85 |
* @param int $flags The check type flag. |
| 86 |
* @return Check_Collection Check collection providing an indexed array of check instances. |
| 87 |
*/ |
| 88 |
public function get_checks( $flags = self::TYPE_ALL ) { |
| 89 |
$checks = array(); |
| 90 |
|
| 91 |
if ( $flags & self::TYPE_STATIC ) { |
| 92 |
$checks += $this->static_checks; |
| 93 |
} |
| 94 |
|
| 95 |
if ( $flags & self::TYPE_RUNTIME ) { |
| 96 |
$checks += $this->runtime_checks; |
| 97 |
} |
| 98 |
|
| 99 |
// Return all checks, including experimental if requested. |
| 100 |
if ( $flags & self::INCLUDE_EXPERIMENTAL ) { |
| 101 |
return new Default_Check_Collection( $checks ); |
| 102 |
} |
| 103 |
|
| 104 |
// Remove experimental checks before returning. |
| 105 |
return ( new Default_Check_Collection( $checks ) )->filter( |
| 106 |
static function ( Check $check ) { |
| 107 |
return $check->get_stability() !== Check::STABILITY_EXPERIMENTAL; |
| 108 |
} |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|