| @@ -14,8 +14,9 @@ | ||
| 14 | 14 | defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | 17 | * TablePress WP Option Wrapper class |
| 18 | + * | |
| 18 | 19 | * @package TablePress |
| 19 | 20 | * @subpackage Classes |
| 20 | 21 | * @author Tobias Bäthge |
| 21 | 22 | * @since 1.0.0 |
| @@ -25,19 +26,18 @@ | ||
| 25 | 26 | /** |
| 26 | 27 | * Name/Key of the Option (in its location in the database). |
| 27 | 28 | * |
| 28 | 29 | * @since 1.0.0 |
| 29 | - * @var string | |
| 30 | 30 | */ |
| 31 | - protected $option_name; | |
| 31 | + protected string $option_name; | |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Current value of the option. |
| 35 | 35 | * |
| 36 | 36 | * @since 1.0.0 |
| 37 | - * @var array | |
| 37 | + * @var mixed[] | |
| 38 | 38 | */ |
| 39 | - protected $option_value; | |
| 39 | + protected array $option_value; | |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * Initialize with Option Name. |
| 43 | 43 | * |
| @@ -42,11 +42,13 @@ | ||
| 42 | 42 | * Initialize with Option Name. |
| 43 | 43 | * |
| 44 | 44 | * @since 1.0.0 |
| 45 | 45 | * |
| 46 | - * @param array $params { | |
| 46 | + * @param array{option_name: string, default_value: mixed[]} $params { | |
| 47 | + * An array of Option parameters. | |
| 48 | + * | |
| 47 | 49 | * @type string $option_name Name of the Option. |
| 48 | - * @type array $default_value Default values for the Option. | |
| 50 | + * @type array $default_value Default values for the Option, as an array. | |
| 49 | 51 | * } |
| 50 | 52 | */ |
| 51 | 53 | public function __construct( array $params ) { |
| 52 | 54 | $this->option_name = $params['option_name']; |
| @@ -66,9 +68,9 @@ | ||
| 66 | 68 | * |
| 67 | 69 | * @param string $name Name of the option to check. |
| 68 | 70 | * @return bool Whether the option is set. |
| 69 | 71 | */ |
| 70 | - public function is_set( $name ) { | |
| 72 | + public function is_set( string $name ): bool { | |
| 71 | 73 | return isset( $this->option_value[ $name ] ); |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | /** |
| @@ -75,13 +77,13 @@ | ||
| 75 | 77 | * Get a single Option, or get all Options. |
| 76 | 78 | * |
| 77 | 79 | * @since 1.0.0 |
| 78 | 80 | * |
| 79 | - * @param string|bool $name Optional. Name of a single option to get, or false for all options. | |
| 80 | - * @param mixed $default_value Optional. Default value to return, if a single option $name does not exist. | |
| 81 | - * @return mixed|array Value of the retrieved option $name or $default_value if it does not exist, or all options. | |
| 81 | + * @param string|false $name Optional. Name of a single option to get, or false for all options. | |
| 82 | + * @param mixed $default_value Optional. Default value to return, if a single option $name does not exist. | |
| 83 | + * @return mixed Value of the retrieved option $name or $default_value if it does not exist, or all options. | |
| 82 | 84 | */ |
| 83 | - public function get( $name = false, $default_value = null ) { | |
| 85 | + public function get( /* string|false */ $name = false, /* string|int|float|bool|array|null */ $default_value = null ) /* : string|int|float|bool|array|null */ { | |
| 84 | 86 | if ( false === $name ) { |
| 85 | 87 | return $this->option_value; |
| 86 | 88 | } |
| 87 | 89 | |
| @@ -97,14 +99,14 @@ | ||
| 97 | 99 | * Update Option. |
| 98 | 100 | * |
| 99 | 101 | * @since 1.0.0 |
| 100 | 102 | * |
| 101 | - * @param array $new_options New options (name => value). | |
| 103 | + * @param array<string, mixed> $new_options New options (name => value). | |
| 102 | 104 | * @return bool True on success, false on failure. |
| 103 | 105 | */ |
| 104 | - public function update( array $new_options ) { | |
| 106 | + public function update( array $new_options ): bool { | |
| 105 | 107 | $this->option_value = $new_options; |
| 106 | - return $this->_update_option( $this->option_name, wp_json_encode( $this->option_value, TABLEPRESS_JSON_OPTIONS ) ); | |
| 108 | + return $this->_update_option( $this->option_name, wp_json_encode( $this->option_value, TABLEPRESS_JSON_OPTIONS ) ); // @phpstan-ignore argument.type | |
| 107 | 109 | } |
| 108 | 110 | |
| 109 | 111 | /** |
| 110 | 112 | * Delete Option. |
| @@ -112,13 +114,13 @@ | ||
| 112 | 114 | * @since 1.0.0 |
| 113 | 115 | * |
| 114 | 116 | * @return bool True on success, false on failure. |
| 115 | 117 | */ |
| 116 | - public function delete() { | |
| 118 | + public function delete(): bool { | |
| 117 | 119 | return $this->_delete_option( $this->option_name ); |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | - /** | |
| 122 | + /* | |
| 121 | 123 | * Internal functions mapping - This needs to be re-defined by child classes. |
| 122 | 124 | */ |
| 123 | 125 | |
| 124 | 126 | /** |
| @@ -129,9 +131,9 @@ | ||
| 129 | 131 | * @param string $option_name Name of the WP Option. |
| 130 | 132 | * @param mixed $default_value Default value of the WP Option. |
| 131 | 133 | * @return mixed Current value of the WP Option, or $default_value if it does not exist. |
| 132 | 134 | */ |
| 133 | - protected function _get_option( $option_name, $default_value ) { | |
| 135 | + protected function _get_option( string $option_name, /* string|int|float|bool|array|null */ $default_value ) /* : string|int|float|bool|array|null */ { | |
| 134 | 136 | return get_option( $option_name, $default_value ); |
| 135 | 137 | } |
| 136 | 138 | |
| 137 | 139 | /** |
| @@ -142,9 +144,9 @@ | ||
| 142 | 144 | * @param string $option_name Name of the WP Option. |
| 143 | 145 | * @param string $new_value New value of the WP Option. |
| 144 | 146 | * @return bool True on success, false on failure. |
| 145 | 147 | */ |
| 146 | - protected function _update_option( $option_name, $new_value ) { | |
| 148 | + protected function _update_option( string $option_name, string $new_value ): bool { | |
| 147 | 149 | return update_option( $option_name, $new_value ); |
| 148 | 150 | } |
| 149 | 151 | |
| 150 | 152 | /** |
| @@ -154,9 +156,9 @@ | ||
| 154 | 156 | * |
| 155 | 157 | * @param string $option_name Name of the WP Option. |
| 156 | 158 | * @return bool True on success, false on failure. |
| 157 | 159 | */ |
| 158 | - protected function _delete_option( $option_name ) { | |
| 160 | + protected function _delete_option( string $option_name ): bool { | |
| 159 | 161 | return delete_option( $option_name ); |
| 160 | 162 | } |
| 161 | 163 | |
| 162 | 164 | } // class TablePress_WP_Option |