Complex classes like FormOptions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FormOptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class FormOptions implements ArrayAccess { |
||
| 36 | /** @name Type constants |
||
| 37 | * Used internally to map an option value to a WebRequest accessor |
||
| 38 | */ |
||
| 39 | /* @{ */ |
||
| 40 | /** Mark value for automatic detection (for simple data types only) */ |
||
| 41 | const AUTO = -1; |
||
| 42 | /** String type, maps guessType() to WebRequest::getText() */ |
||
| 43 | const STRING = 0; |
||
| 44 | /** Integer type, maps guessType() to WebRequest::getInt() */ |
||
| 45 | const INT = 1; |
||
| 46 | /** Float type, maps guessType() to WebRequest::getFloat() |
||
| 47 | * @since 1.23 */ |
||
| 48 | const FLOAT = 4; |
||
| 49 | /** Boolean type, maps guessType() to WebRequest::getBool() */ |
||
| 50 | const BOOL = 2; |
||
| 51 | /** Integer type or null, maps to WebRequest::getIntOrNull() |
||
| 52 | * This is useful for the namespace selector. |
||
| 53 | */ |
||
| 54 | const INTNULL = 3; |
||
| 55 | /** Array type, maps guessType() to WebRequest::getArray() |
||
| 56 | * @since 1.28 */ |
||
| 57 | const ARR = 5; |
||
| 58 | /* @} */ |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Map of known option names to information about them. |
||
| 62 | * |
||
| 63 | * Each value is an array with the following keys: |
||
| 64 | * - 'default' - the default value as passed to add() |
||
| 65 | * - 'value' - current value, start with null, can be set by various functions |
||
| 66 | * - 'consumed' - true/false, whether the option was consumed using |
||
| 67 | * consumeValue() or consumeValues() |
||
| 68 | * - 'type' - one of the type constants (but never AUTO) |
||
| 69 | */ |
||
| 70 | protected $options = []; |
||
| 71 | |||
| 72 | # Setting up |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Add an option to be handled by this FormOptions instance. |
||
| 76 | * |
||
| 77 | * @param string $name Request parameter name |
||
| 78 | * @param mixed $default Default value when the request parameter is not present |
||
| 79 | * @param int $type One of the type constants (optional, defaults to AUTO) |
||
| 80 | */ |
||
| 81 | public function add( $name, $default, $type = self::AUTO ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Remove an option being handled by this FormOptions instance. This is the inverse of add(). |
||
| 98 | * |
||
| 99 | * @param string $name Request parameter name |
||
| 100 | */ |
||
| 101 | public function delete( $name ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Used to find out which type the data is. All types are defined in the 'Type constants' section |
||
| 108 | * of this class. |
||
| 109 | * |
||
| 110 | * Detection of the INTNULL type is not supported; INT will be assumed if the data is an integer, |
||
| 111 | * MWException will be thrown if it's null. |
||
| 112 | * |
||
| 113 | * @param mixed $data Value to guess the type for |
||
| 114 | * @throws MWException If unable to guess the type |
||
| 115 | * @return int Type constant |
||
| 116 | */ |
||
| 117 | public static function guessType( $data ) { |
||
| 132 | |||
| 133 | # Handling values |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Verify that the given option name exists. |
||
| 137 | * |
||
| 138 | * @param string $name Option name |
||
| 139 | * @param bool $strict Throw an exception when the option doesn't exist instead of returning false |
||
| 140 | * @throws MWException |
||
| 141 | * @return bool True if the option exists, false otherwise |
||
| 142 | */ |
||
| 143 | public function validateName( $name, $strict = false ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Use to set the value of an option. |
||
| 157 | * |
||
| 158 | * @param string $name Option name |
||
| 159 | * @param mixed $value Value for the option |
||
| 160 | * @param bool $force Whether to set the value when it is equivalent to the default value for this |
||
| 161 | * option (default false). |
||
| 162 | */ |
||
| 163 | public function setValue( $name, $value, $force = false ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the value for the given option name. Uses getValueReal() internally. |
||
| 176 | * |
||
| 177 | * @param string $name Option name |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | public function getValue( $name ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return current option value, based on a structure taken from $options. |
||
| 188 | * |
||
| 189 | * @param array $option Array structure describing the option |
||
| 190 | * @return mixed Value, or the default value if it is null |
||
| 191 | */ |
||
| 192 | protected function getValueReal( $option ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Delete the option value. |
||
| 202 | * This will make future calls to getValue() return the default value. |
||
| 203 | * @param string $name Option name |
||
| 204 | */ |
||
| 205 | public function reset( $name ) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the value of given option and mark it as 'consumed'. Consumed options are not returned |
||
| 212 | * by getUnconsumedValues(). |
||
| 213 | * |
||
| 214 | * @see consumeValues() |
||
| 215 | * @throws MWException If the option does not exist |
||
| 216 | * @param string $name Option name |
||
| 217 | * @return mixed Value, or the default value if it is null |
||
| 218 | */ |
||
| 219 | public function consumeValue( $name ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the values of given options and mark them as 'consumed'. Consumed options are not returned |
||
| 228 | * by getUnconsumedValues(). |
||
| 229 | * |
||
| 230 | * @see consumeValue() |
||
| 231 | * @throws MWException If any option does not exist |
||
| 232 | * @param array $names Array of option names as strings |
||
| 233 | * @return array Array of option values, or the default values if they are null |
||
| 234 | */ |
||
| 235 | public function consumeValues( $names ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @see validateBounds() |
||
| 249 | */ |
||
| 250 | public function validateIntBounds( $name, $min, $max ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Constrain a numeric value for a given option to a given range. The value will be altered to fit |
||
| 256 | * in the range. |
||
| 257 | * |
||
| 258 | * @since 1.23 |
||
| 259 | * |
||
| 260 | * @param string $name Option name |
||
| 261 | * @param int|float $min Minimum value |
||
| 262 | * @param int|float $max Maximum value |
||
| 263 | * @throws MWException If option is not of type INT |
||
| 264 | */ |
||
| 265 | public function validateBounds( $name, $min, $max ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get all remaining values which have not been consumed by consumeValue() or consumeValues(). |
||
| 281 | * |
||
| 282 | * @param bool $all Whether to include unchanged options (default: false) |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getUnconsumedValues( $all = false ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return options modified as an array ( name => value ) |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function getChangedValues() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Format options to an array ( name => value ) |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function getAllValues() { |
||
| 328 | |||
| 329 | # Reading values |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Fetch values for all options (or selected options) from the given WebRequest, making them |
||
| 333 | * available for accessing with getValue() or consumeValue() etc. |
||
| 334 | * |
||
| 335 | * @param WebRequest $r The request to fetch values from |
||
| 336 | * @param array $optionKeys Which options to fetch the values for (default: |
||
| 337 | * all of them). Note that passing an empty array will also result in |
||
| 338 | * values for all keys being fetched. |
||
| 339 | * @throws MWException If the type of any option is invalid |
||
| 340 | */ |
||
| 341 | public function fetchValuesFromRequest( WebRequest $r, $optionKeys = null ) { |
||
| 378 | |||
| 379 | /** @name ArrayAccess functions |
||
| 380 | * These functions implement the ArrayAccess PHP interface. |
||
| 381 | * @see http://php.net/manual/en/class.arrayaccess.php |
||
| 382 | */ |
||
| 383 | /* @{ */ |
||
| 384 | /** |
||
| 385 | * Whether the option exists. |
||
| 386 | * @param string $name |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | public function offsetExists( $name ) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieve an option value. |
||
| 395 | * @param string $name |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | public function offsetGet( $name ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set an option to given value. |
||
| 404 | * @param string $name |
||
| 405 | * @param mixed $value |
||
| 406 | */ |
||
| 407 | public function offsetSet( $name, $value ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Delete the option. |
||
| 413 | * @param string $name |
||
| 414 | */ |
||
| 415 | public function offsetUnset( $name ) { |
||
| 418 | /* @} */ |
||
| 419 | } |
||
| 420 |