Complex classes like Functions 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 Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Functions |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var \LesserPhp\Library\Assertions |
||
25 | */ |
||
26 | private $assertions; |
||
27 | |||
28 | /** |
||
29 | * @var \LesserPhp\Library\Coerce |
||
30 | */ |
||
31 | private $coerce; |
||
32 | |||
33 | /** |
||
34 | * @var \LesserPhp\Compiler |
||
35 | */ |
||
36 | private $compiler; |
||
37 | |||
38 | /** |
||
39 | * @var \LesserPhp\Color\Converter |
||
40 | */ |
||
41 | private $converter; |
||
42 | |||
43 | public static $TRUE = ["keyword", "true"]; |
||
44 | public static $FALSE = ["keyword", "false"]; |
||
45 | public static $lengths = ["px", "m", "cm", "mm", "in", "pt", "pc"]; |
||
46 | public static $times = ["s", "ms"]; |
||
47 | public static $angles = ["rad", "deg", "grad", "turn"]; |
||
48 | public static $lengths_to_base = [1, 3779.52755906, 37.79527559, 3.77952756, 96, 1.33333333, 16]; |
||
49 | |||
50 | 64 | ||
51 | /** |
||
52 | 64 | * Functions constructor. |
|
53 | 64 | * |
|
54 | 64 | * @param \LesserPhp\Library\Assertions $assertions |
|
55 | 64 | * @param \LesserPhp\Library\Coerce $coerce |
|
56 | 64 | * @param \LesserPhp\Compiler $compiler |
|
57 | * @param \LesserPhp\Color\Converter $converter |
||
58 | 1 | */ |
|
59 | public function __construct(Assertions $assertions, Coerce $coerce, Compiler $compiler, Converter $converter) |
||
66 | |||
67 | /** |
||
68 | * @param array $args |
||
69 | 1 | * |
|
70 | * @return array |
||
71 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
72 | */ |
||
73 | public function pow(array $args) |
||
83 | 1 | ||
84 | 1 | /** |
|
85 | * @return float |
||
86 | */ |
||
87 | public function pi() |
||
91 | 1 | ||
92 | /** |
||
93 | 1 | * @param array $args |
|
94 | 1 | * |
|
95 | * @return array |
||
96 | * @throws \LesserPhp\Exception\GeneralException |
||
97 | */ |
||
98 | 1 | public function mod(array $args) |
|
104 | 1 | ||
105 | /** |
||
106 | * @param array $color |
||
107 | * |
||
108 | 1 | * @return int |
|
109 | * @throws \LesserPhp\Exception\GeneralException |
||
110 | */ |
||
111 | 2 | public function red(array $color) |
|
120 | 2 | ||
121 | /** |
||
122 | * @param array $color |
||
123 | 1 | * |
|
124 | * @return int |
||
125 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
126 | */ |
||
127 | public function green(array $color) |
||
136 | |||
137 | 1 | /** |
|
138 | 1 | * @param array $color |
|
139 | * |
||
140 | 1 | * @return int |
|
141 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
142 | 1 | */ |
|
143 | public function blue(array $color) |
||
152 | |||
153 | 2 | /** |
|
154 | * @param array $args |
||
155 | 2 | * |
|
156 | 2 | * @return array |
|
157 | * @throws \LesserPhp\Exception\GeneralException |
||
158 | 2 | */ |
|
159 | 2 | public function convert(array $args) |
|
170 | 1 | ||
171 | /** |
||
172 | 1 | * @param array $num |
|
173 | * |
||
174 | * @return array |
||
175 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
176 | */ |
||
177 | 1 | public function abs(array $num) |
|
181 | |||
182 | 1 | /** |
|
183 | * @param array $args |
||
184 | * |
||
185 | 1 | * @return int |
|
186 | * @throws \LesserPhp\Exception\GeneralException |
||
187 | 1 | */ |
|
188 | public function min(array $args) |
||
208 | 1 | ||
209 | /** |
||
210 | * @param array $args |
||
211 | 1 | * |
|
212 | * @return int |
||
213 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
214 | 1 | */ |
|
215 | public function max(array $args) |
||
235 | 3 | ||
236 | /** |
||
237 | * @param $num |
||
238 | 1 | * |
|
239 | * @return float |
||
240 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
241 | */ |
||
242 | public function tan($num) |
||
246 | |||
247 | /** |
||
248 | 1 | * @param $num |
|
249 | * |
||
250 | 1 | * @return float |
|
251 | * @throws \LesserPhp\Exception\GeneralException |
||
252 | */ |
||
253 | 1 | public function sin($num) |
|
257 | |||
258 | /** |
||
259 | * @param $num |
||
260 | * |
||
261 | * @return float |
||
262 | * @throws \LesserPhp\Exception\GeneralException |
||
263 | 1 | */ |
|
264 | public function cos($num) |
||
268 | |||
269 | /** |
||
270 | 1 | * @param $num |
|
271 | 1 | * |
|
272 | 1 | * @return array |
|
273 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
274 | 1 | */ |
|
275 | 1 | public function atan($num) |
|
281 | 1 | ||
282 | /** |
||
283 | * @param $num |
||
284 | * |
||
285 | * @return array |
||
286 | * @throws \LesserPhp\Exception\GeneralException |
||
287 | */ |
||
288 | public function asin($num) |
||
294 | 1 | ||
295 | /** |
||
296 | 1 | * @param $num |
|
297 | * |
||
298 | 1 | * @return array |
|
299 | * @throws \LesserPhp\Exception\GeneralException |
||
300 | 1 | */ |
|
301 | 1 | public function acos($num) |
|
307 | |||
308 | 1 | /** |
|
309 | * @param $num |
||
310 | 1 | * |
|
311 | * @return float |
||
312 | * @throws \LesserPhp\Exception\GeneralException |
||
313 | */ |
||
314 | public function sqrt($num) |
||
318 | |||
319 | 13 | /** |
|
320 | * @param $value |
||
321 | 13 | * |
|
322 | 13 | * @return mixed |
|
323 | 2 | * @throws \LesserPhp\Exception\GeneralException |
|
324 | 2 | */ |
|
325 | 1 | public function extract($value) |
|
336 | |||
337 | /** |
||
338 | * @param $value |
||
339 | 1 | * |
|
340 | * @return array |
||
341 | 1 | */ |
|
342 | public function isnumber($value) |
||
346 | 1 | ||
347 | /** |
||
348 | 1 | * @param $value |
|
349 | 1 | * |
|
350 | 1 | * @return array |
|
351 | 1 | */ |
|
352 | 1 | public function isstring($value) |
|
356 | 1 | ||
357 | /** |
||
358 | * @param $value |
||
359 | * |
||
360 | 1 | * @return array |
|
361 | 1 | */ |
|
362 | 1 | public function iscolor($value) |
|
367 | |||
368 | /** |
||
369 | * @param $value |
||
370 | * |
||
371 | 1 | * @return array |
|
372 | */ |
||
373 | 1 | public function iskeyword($value) |
|
377 | |||
378 | 1 | /** |
|
379 | * @param $value |
||
380 | 1 | * |
|
381 | * @return array |
||
382 | */ |
||
383 | 1 | public function ispixel($value) |
|
387 | 1 | ||
388 | /** |
||
389 | * @param $value |
||
390 | 1 | * |
|
391 | * @return array |
||
392 | 1 | */ |
|
393 | 1 | public function ispercentage($value) |
|
397 | |||
398 | /** |
||
399 | * @param $value |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function isem($value) |
||
407 | 1 | ||
408 | /** |
||
409 | * @param $value |
||
410 | 1 | * |
|
411 | 1 | * @return array |
|
412 | 1 | */ |
|
413 | public function isrem($value) |
||
417 | |||
418 | /** |
||
419 | * @param $color |
||
420 | 1 | * |
|
421 | * @return string |
||
422 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
423 | */ |
||
424 | 1 | public function rgbahex($color) |
|
439 | |||
440 | 1 | /** |
|
441 | * @param $color |
||
442 | 1 | * |
|
443 | * @return string |
||
444 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
445 | 1 | */ |
|
446 | public function argb($color) |
||
450 | 1 | ||
451 | /** |
||
452 | 1 | * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
|
453 | * |
||
454 | 1 | * @param array $value either an argument list (two strings) or a single string |
|
455 | 1 | * |
|
456 | * @return string formatted url(), either as a link or base64-encoded |
||
457 | 1 | */ |
|
458 | public function data_uri($value) |
||
484 | 1 | ||
485 | 1 | // utility func to unquote a string |
|
486 | |||
487 | 1 | /** |
|
488 | * @param array $arg |
||
489 | * |
||
490 | 1 | * @return array |
|
491 | * @throws \LesserPhp\Exception\GeneralException |
||
492 | 1 | */ |
|
493 | public function e(array $arg) |
||
512 | |||
513 | 1 | /** |
|
514 | * @param array $args |
||
515 | * |
||
516 | * @return array |
||
517 | * @throws \LesserPhp\Exception\GeneralException |
||
518 | */ |
||
519 | public function _sprintf(array $args) |
||
555 | 1 | ||
556 | /** |
||
557 | * @param array $arg |
||
558 | * |
||
559 | 1 | * @return array |
|
560 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
561 | 1 | */ |
|
562 | public function floor(array $arg) |
||
568 | |||
569 | 1 | /** |
|
570 | * @param array $arg |
||
571 | * |
||
572 | 1 | * @return array |
|
573 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
574 | */ |
||
575 | 1 | public function ceil(array $arg) |
|
581 | 1 | ||
582 | 1 | /** |
|
583 | * @param array $arg |
||
584 | * |
||
585 | 1 | * @return array |
|
586 | 1 | * @throws \LesserPhp\Exception\GeneralException |
|
587 | */ |
||
588 | public function round(array $arg) |
||
601 | 1 | ||
602 | 1 | /** |
|
603 | 1 | * @param array $arg |
|
604 | 1 | * |
|
605 | 1 | * @return array |
|
606 | * @throws \LesserPhp\Exception\GeneralException |
||
607 | 1 | */ |
|
608 | public function unit(array $arg) |
||
622 | |||
623 | |||
624 | 1 | /** |
|
625 | 1 | * @param array $args |
|
626 | 1 | * |
|
627 | * @return array |
||
628 | */ |
||
629 | 1 | public function darken(array $args) |
|
638 | |||
639 | 3 | /** |
|
640 | * @param array $args |
||
641 | 3 | * |
|
642 | 3 | * @return array |
|
643 | */ |
||
644 | public function lighten(array $args) |
||
653 | |||
654 | 1 | /** |
|
655 | 1 | * @param array $args |
|
656 | 1 | * |
|
657 | * @return array |
||
658 | 1 | */ |
|
659 | public function saturate(array $args) |
||
668 | 1 | ||
669 | /** |
||
670 | 1 | * @param array $args |
|
671 | * |
||
672 | * @return array |
||
673 | 1 | */ |
|
674 | public function desaturate(array $args) |
||
683 | |||
684 | 1 | /** |
|
685 | 1 | * @param array $args |
|
686 | * |
||
687 | 1 | * @return array |
|
688 | 1 | */ |
|
689 | public function spin(array $args) |
||
702 | |||
703 | /** |
||
704 | 1 | * @param array $args |
|
705 | 1 | * |
|
706 | * @return int[] |
||
707 | */ |
||
708 | 1 | public function fadeout(array $args) |
|
715 | |||
716 | /** |
||
717 | * @param array $args |
||
718 | 2 | * |
|
719 | * @return int[] |
||
720 | */ |
||
721 | public function fadein(array $args) |
||
728 | 7 | ||
729 | 5 | /** |
|
730 | * @param array $color |
||
731 | 7 | * |
|
732 | * @return float |
||
733 | * @throws \LesserPhp\Exception\GeneralException |
||
734 | */ |
||
735 | public function hue(array $color) |
||
741 | |||
742 | 4 | /** |
|
743 | * @param array $color |
||
744 | 4 | * |
|
745 | 4 | * @return float |
|
746 | 4 | * @throws \LesserPhp\Exception\GeneralException |
|
747 | 4 | */ |
|
748 | public function saturation(array $color) |
||
754 | |||
755 | /** |
||
756 | * @param array $color |
||
757 | * |
||
758 | * @return float |
||
759 | 4 | * @throws \LesserPhp\Exception\GeneralException |
|
760 | */ |
||
761 | 4 | public function lightness(array $color) |
|
767 | |||
768 | /** |
||
769 | * get the alpha of a color |
||
770 | * defaults to 1 for non-colors or colors without an alpha |
||
771 | * |
||
772 | * @param array $value |
||
773 | * |
||
774 | * @return int|null |
||
775 | */ |
||
776 | public function alpha(array $value) |
||
785 | |||
786 | /** |
||
787 | * set the alpha of the color |
||
788 | * |
||
789 | * @param array $args |
||
790 | * |
||
791 | * @return int[] |
||
792 | */ |
||
793 | public function fade(array $args) |
||
800 | |||
801 | /** |
||
802 | * @param array $arg |
||
803 | * |
||
804 | * @return array |
||
805 | * @throws \LesserPhp\Exception\GeneralException |
||
806 | */ |
||
807 | public function percentage(array $arg) |
||
813 | |||
814 | /** |
||
815 | * mixes two colors by weight |
||
816 | * mix(@color1, @color2, [@weight: 50%]); |
||
817 | * http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
||
818 | * |
||
819 | * @param array $args |
||
820 | * |
||
821 | * @return mixed |
||
822 | * @throws \LesserPhp\Exception\GeneralException |
||
823 | */ |
||
824 | public function mix(array $args) |
||
863 | |||
864 | /** |
||
865 | * @param array $args |
||
866 | * |
||
867 | * @return array|null |
||
868 | * @throws \LesserPhp\Exception\GeneralException |
||
869 | */ |
||
870 | public function contrast(array $args) |
||
909 | |||
910 | /** |
||
911 | * @param array $color |
||
912 | * |
||
913 | * @return float |
||
914 | */ |
||
915 | public function luma(array $color) |
||
920 | |||
921 | |||
922 | /** |
||
923 | * @param array $number |
||
924 | * @param $to |
||
925 | * |
||
926 | * @return array |
||
927 | * @throws \LesserPhp\Exception\GeneralException |
||
928 | */ |
||
929 | public function convertMe(array $number, $to) |
||
1010 | |||
1011 | /** |
||
1012 | * @param bool $a |
||
1013 | * |
||
1014 | * @return array |
||
1015 | */ |
||
1016 | public function toBool($a) |
||
1024 | |||
1025 | /** |
||
1026 | * attempts to find the path of an import url, returns null for css files |
||
1027 | * |
||
1028 | * @param string $url |
||
1029 | * |
||
1030 | * @return null|string |
||
1031 | */ |
||
1032 | public function findImport($url) |
||
1043 | |||
1044 | /** |
||
1045 | * @param string $name |
||
1046 | * |
||
1047 | * @return bool |
||
1048 | */ |
||
1049 | public function fileExists($name) |
||
1053 | } |
||
1054 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: