Complex classes like space 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 space, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class space extends \ArrayObject implements \JsonSerializable { |
||
23 | |||
24 | /** |
||
25 | * The name of the current color space |
||
26 | * @var string |
||
27 | */ |
||
28 | protected static $name = ''; |
||
29 | |||
30 | /** |
||
31 | * The specification for each key of the color space |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $specs = array(); |
||
35 | |||
36 | /** |
||
37 | * Import an array as the current color space |
||
38 | * |
||
39 | * @param array $input The color array to import |
||
40 | */ |
||
41 | public function __construct(array $input) { |
||
46 | |||
47 | /** |
||
48 | * Get the name of the current color space |
||
49 | * |
||
50 | * @return string The current color space |
||
51 | */ |
||
52 | public static function name() :string { |
||
55 | |||
56 | /** |
||
57 | * Export the array in its intended format |
||
58 | * |
||
59 | * @return array The formatted color array |
||
60 | */ |
||
61 | public function export() :array { |
||
70 | |||
71 | /** |
||
72 | * Import a value according to its specification |
||
73 | * |
||
74 | * @param array $value The value to import |
||
|
|||
75 | * @param string $key The key being imported |
||
76 | * @param array $spec The specification to follow |
||
77 | * @return void |
||
78 | */ |
||
79 | protected function _import_input(float $value, string $key, array $spec) { |
||
86 | |||
87 | /** |
||
88 | * Check that the color space child class was correctly configured |
||
89 | * |
||
90 | * @param array &$specs The specification array to check |
||
91 | * @return void |
||
92 | */ |
||
93 | protected static function _check_specs(&$specs) { |
||
109 | |||
110 | /** |
||
111 | * Check that the name of a specification is correctly formatted |
||
112 | * |
||
113 | * @param mixed $name The name value to check |
||
114 | * @param string $key The key being checked (for debug) |
||
115 | * @return void |
||
116 | */ |
||
117 | protected static function _check_spec_name($name, string $key) { |
||
133 | |||
134 | /** |
||
135 | * Check that the name of a specification is correctly formatted |
||
136 | * |
||
137 | * @param mixed $min The min value to check |
||
138 | * @param mixed $max The max value to check |
||
139 | * @param string $key The key being checked (for debug) |
||
140 | * @return void |
||
141 | */ |
||
142 | protected static function _check_spec_minmax($min, $max, string $key) { |
||
158 | |||
159 | /** |
||
160 | * Check that the overflow value is valid |
||
161 | * |
||
162 | * @param mixed &$overflow The overflow value to check |
||
163 | * @param string $key The key being checked (for debug) |
||
164 | * @return void |
||
165 | */ |
||
166 | protected static function _check_spec_overflow(&$overflow, string $key) { |
||
171 | |||
172 | /** |
||
173 | * Run the specification check on a key's value |
||
174 | * |
||
175 | * @param float $value The value to check |
||
176 | * @param string $key The key to check against |
||
177 | * @param array $spec The specification to use |
||
178 | * @return float The value after applying the specification |
||
179 | */ |
||
180 | protected static function _run_spec(float $value, string $key, array $spec) :float { |
||
186 | |||
187 | /** |
||
188 | * Force a value into the specification's range |
||
189 | * |
||
190 | * @param float $value The value to check |
||
191 | * @param string $key The key to check against |
||
192 | * @param array $spec The specification to use |
||
193 | * @return float The valid value |
||
194 | */ |
||
195 | protected static function _spec_range(float $value, string $key, array $spec) :float { |
||
211 | |||
212 | /** |
||
213 | * Force a value into the specification's range by looping back around |
||
214 | * |
||
215 | * @param float $value The value to check |
||
216 | * @param array $spec The specification to use |
||
217 | * @return float The valid value |
||
218 | */ |
||
219 | protected static function _overflow_loop(float $value, array $spec) :float { |
||
225 | |||
226 | /** |
||
227 | * Force a value into the specification's range by limiting to the nearest |
||
228 | * minimum/maximum |
||
229 | * |
||
230 | * @param float $value The value to check |
||
231 | * @param array $spec The specification to use |
||
232 | * @return float The valid value |
||
233 | */ |
||
234 | protected static function _overflow_limit(float $value, array $spec) :float { |
||
237 | |||
238 | /** |
||
239 | * Get the raw values to be serialized by JSON |
||
240 | * |
||
241 | * @return array The current color array |
||
242 | */ |
||
243 | public function jsonSerialize() { |
||
246 | |||
247 | /** |
||
248 | * Set an existing offset according to its specification |
||
249 | * |
||
250 | * @param string $key The key to set |
||
251 | * @param mixed $value The value to check and set |
||
252 | * @return float The value that was set |
||
253 | */ |
||
254 | public function offsetSet($key, $value) { |
||
266 | |||
267 | /** |
||
268 | * Set a value to its minimum value |
||
269 | * |
||
270 | * @param string $key The key to set |
||
271 | * @return void |
||
272 | */ |
||
273 | public function offsetUnset($key) { |
||
282 | |||
283 | /** |
||
284 | * Replace the current color array with another array of the same type |
||
285 | * |
||
286 | * @param array $input The array to import |
||
287 | * @return void |
||
288 | */ |
||
289 | public function exchangeArray($input) { |
||
297 | |||
298 | /** |
||
299 | * Associate expected keys when working with numerically indexed arrays |
||
300 | * |
||
301 | * @param array $specs The specification to follow |
||
302 | * @param array &$input The input array |
||
303 | * @return void |
||
304 | */ |
||
305 | protected static function _assoc_keys(array $specs, array &$input) { |
||
318 | |||
319 | /*** DISABLED METHODS ***/ |
||
320 | |||
321 | /** |
||
322 | * DISABLED |
||
323 | * |
||
324 | * @param mixed $disabled Disabled |
||
325 | * @return void |
||
326 | * @codeCoverageIgnore |
||
327 | */ |
||
328 | public function append($disbaled) {} |
||
329 | |||
330 | /** |
||
331 | * DISABLED |
||
332 | * |
||
333 | * @return void |
||
334 | * @codeCoverageIgnore |
||
335 | */ |
||
336 | public function asort() {} |
||
337 | |||
338 | /** |
||
339 | * DISABLED |
||
340 | * |
||
341 | * @return void |
||
342 | * @codeCoverageIgnore |
||
343 | */ |
||
344 | public function ksort() {} |
||
345 | |||
346 | /** |
||
347 | * DISABLED |
||
348 | * |
||
349 | * @param mixed $disabled Disabled |
||
350 | * @return void |
||
351 | * @codeCoverageIgnore |
||
352 | */ |
||
353 | public function uasort($disabled) {} |
||
354 | |||
355 | /** |
||
356 | * DISABLED |
||
357 | * |
||
358 | * @param mixed $disabled Disabled |
||
359 | * @return void |
||
360 | * @codeCoverageIgnore |
||
361 | */ |
||
362 | public function uksort($disabled) {} |
||
363 | |||
364 | /** |
||
365 | * DISABLED |
||
366 | * |
||
367 | * @return void |
||
368 | * @codeCoverageIgnore |
||
369 | */ |
||
370 | public function natcasesort() {} |
||
371 | |||
372 | /** |
||
373 | * DISABLED |
||
374 | * |
||
375 | * @return void |
||
376 | * @codeCoverageIgnore |
||
377 | */ |
||
378 | public function natsort() {} |
||
379 | |||
380 | /** |
||
381 | * DISABLED |
||
382 | * |
||
383 | * @param mixed $disabled Disabled |
||
384 | * @return void |
||
385 | * @codeCoverageIgnore |
||
386 | */ |
||
387 | public function setFlags($disabled) {} |
||
388 | |||
389 | /** |
||
390 | * DISABLED |
||
391 | * |
||
392 | * @param mixed $disabled Disabled |
||
393 | * @return void |
||
394 | * @codeCoverageIgnore |
||
395 | */ |
||
396 | public function setIteratorClass($disabled) {} |
||
397 | } |
||
398 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.