Conditions | 12 |
Paths | 6 |
Total Lines | 38 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
9 | public static function parameterToString($object, int $deep=2) |
||
10 | { |
||
11 | if (is_null($object)) { |
||
12 | return 'NULL'; |
||
13 | } elseif (is_object($object)) { |
||
14 | $objectName = get_class($object); |
||
15 | $parameterString = ''; |
||
16 | |||
17 | if ($deep > 0) { |
||
18 | $vars = get_class_vars($objectName); |
||
19 | foreach ($vars as $key => $value) { |
||
20 | if (is_string($value) && strlen($value) > self::$strLength) { |
||
21 | $parameterString .= $key.'='.json_encode(substr($value, 0, 80), JSON_UNESCAPED_UNICODE) .'...,'; |
||
22 | } else { |
||
23 | $parameterString .= $key.'='.self::parameterToString($value, $deep-1) .','; |
||
24 | } |
||
25 | } |
||
26 | } else { |
||
27 | $parameterString = '...'; |
||
28 | } |
||
29 | |||
30 | return $objectName.' {'.trim($parameterString, ',').'}'; |
||
31 | } elseif (is_array($object)) { |
||
32 | $parameterString = ''; |
||
33 | if ($deep > 0) { |
||
34 | foreach ($object as $key => $value) { |
||
35 | if (is_string($value) && strlen($value) > self::$strLength) { |
||
36 | $parameterString .= json_encode(substr($value, 0, 80), JSON_UNESCAPED_UNICODE) .'...,'; |
||
37 | } else { |
||
38 | $parameterString .= static::parameterToString($value, $deep-1) .','; |
||
39 | } |
||
40 | } |
||
41 | } else { |
||
42 | $parameterString = '...'; |
||
43 | } |
||
44 | return '['.trim($parameterString, ',').']'; |
||
45 | } |
||
46 | return $object; |
||
47 | } |
||
89 |