Complex classes like Input 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 Input, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Input |
||
| 15 | { |
||
| 16 | |||
| 17 | /** @var Post Access $_POST parameters */ |
||
| 18 | public $post; |
||
| 19 | /** @var Get Access $_GET parameters */ |
||
| 20 | public $get; |
||
| 21 | /** @var Server Access $_SERVER parameters */ |
||
| 22 | public $server; |
||
| 23 | |||
| 24 | protected $access; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Callable |
||
| 28 | */ |
||
| 29 | protected $filter; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Intilizes the dokuwiki\Input\Input class and it subcomponents |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Apply the set filter to the given value |
||
| 44 | * |
||
| 45 | * @param string $data |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | protected function applyfilter($data) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Return a filtered copy of the input object |
||
| 56 | * |
||
| 57 | * Expects a callable that accepts one string parameter and returns a filtered string |
||
| 58 | * |
||
| 59 | * @param Callable|string $filter |
||
| 60 | * @return Input |
||
| 61 | */ |
||
| 62 | public function filter($filter = 'stripctl') |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if a parameter was set |
||
| 72 | * |
||
| 73 | * Basically a wrapper around isset. When called on the $post and $get subclasses, |
||
| 74 | * the parameter is set to $_POST or $_GET and to $_REQUEST |
||
| 75 | * |
||
| 76 | * @see isset |
||
| 77 | * @param string $name Parameter name |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function has($name) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Remove a parameter from the superglobals |
||
| 87 | * |
||
| 88 | * Basically a wrapper around unset. When NOT called on the $post and $get subclasses, |
||
| 89 | * the parameter will also be removed from $_POST or $_GET |
||
| 90 | * |
||
| 91 | * @see isset |
||
| 92 | * @param string $name Parameter name |
||
| 93 | */ |
||
| 94 | public function remove($name) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Access a request parameter without any type conversion |
||
| 110 | * |
||
| 111 | * @param string $name Parameter name |
||
| 112 | * @param mixed $default Default to return if parameter isn't set |
||
| 113 | * @param bool $nonempty Return $default if parameter is set but empty() |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | public function param($name, $default = null, $nonempty = false) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets a parameter |
||
| 126 | * |
||
| 127 | * @param string $name Parameter name |
||
| 128 | * @param mixed $value Value to set |
||
| 129 | */ |
||
| 130 | public function set($name, $value) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get a reference to a request parameter |
||
| 137 | * |
||
| 138 | * This avoids copying data in memory, when the parameter is not set it will be created |
||
| 139 | * and intialized with the given $default value before a reference is returned |
||
| 140 | * |
||
| 141 | * @param string $name Parameter name |
||
| 142 | * @param mixed $default If parameter is not set, initialize with this value |
||
| 143 | * @param bool $nonempty Init with $default if parameter is set but empty() |
||
| 144 | * @return mixed (reference) |
||
| 145 | */ |
||
| 146 | public function &ref($name, $default = '', $nonempty = false) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Access a request parameter as int |
||
| 157 | * |
||
| 158 | * @param string $name Parameter name |
||
| 159 | * @param int $default Default to return if parameter isn't set or is an array |
||
| 160 | * @param bool $nonempty Return $default if parameter is set but empty() |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | public function int($name, $default = 0, $nonempty = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Access a request parameter as string |
||
| 176 | * |
||
| 177 | * @param string $name Parameter name |
||
| 178 | * @param string $default Default to return if parameter isn't set or is an array |
||
| 179 | * @param bool $nonempty Return $default if parameter is set but empty() |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function str($name, $default = '', $nonempty = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Access a request parameter and make sure it is has a valid value |
||
| 194 | * |
||
| 195 | * Please note that comparisons to the valid values are not done typesafe (request vars |
||
| 196 | * are always strings) however the function will return the correct type from the $valids |
||
| 197 | * array when an match was found. |
||
| 198 | * |
||
| 199 | * @param string $name Parameter name |
||
| 200 | * @param array $valids Array of valid values |
||
| 201 | * @param mixed $default Default to return if parameter isn't set or not valid |
||
| 202 | * @return null|mixed |
||
| 203 | */ |
||
| 204 | public function valid($name, $valids, $default = null) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Access a request parameter as bool |
||
| 216 | * |
||
| 217 | * Note: $nonempty is here for interface consistency and makes not much sense for booleans |
||
| 218 | * |
||
| 219 | * @param string $name Parameter name |
||
| 220 | * @param mixed $default Default to return if parameter isn't set |
||
| 221 | * @param bool $nonempty Return $default if parameter is set but empty() |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function bool($name, $default = false, $nonempty = false) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Access a request parameter as array |
||
| 237 | * |
||
| 238 | * @param string $name Parameter name |
||
| 239 | * @param mixed $default Default to return if parameter isn't set |
||
| 240 | * @param bool $nonempty Return $default if parameter is set but empty() |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function arr($name, $default = array(), $nonempty = false) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Create a simple key from an array key |
||
| 254 | * |
||
| 255 | * This is useful to access keys where the information is given as an array key or as a single array value. |
||
| 256 | * For example when the information was submitted as the name of a submit button. |
||
| 257 | * |
||
| 258 | * This function directly changes the access array. |
||
| 259 | * |
||
| 260 | * Eg. $_REQUEST['do']['save']='Speichern' becomes $_REQUEST['do'] = 'save' |
||
| 261 | * |
||
| 262 | * This function returns the $INPUT object itself for easy chaining |
||
| 263 | * |
||
| 264 | * @param string $name |
||
| 265 | * @return Input |
||
| 266 | */ |
||
| 267 | public function extract($name) |
||
| 287 | } |
||
| 288 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.