Conditions | 15 |
Paths | 132 |
Total Lines | 62 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
41 | public static function spliceKeyValue(array $data, array $options = []): string |
||
42 | { |
||
43 | $text = ''; |
||
44 | |||
45 | $options = array_merge([ |
||
46 | 'leftChar' => '', // e.g ' ', ' * ' |
||
47 | 'sepChar' => ' ', // e.g ' | ' OUT: key | value |
||
48 | 'keyStyle' => '', // e.g 'info','comment' |
||
49 | 'valStyle' => '', // e.g 'info','comment' |
||
50 | 'keyMinWidth' => 8, |
||
51 | 'keyMaxWidth' => 0, |
||
52 | 'keyPadPos' => 'right', |
||
53 | 'ucFirst' => true, // upper first char for value |
||
54 | ], $options); |
||
55 | |||
56 | $keyStyle = trim($options['keyStyle']); |
||
57 | $keyPadPos = (string) $options['keyPadPos']; |
||
58 | |||
59 | if ($options['keyMaxWidth'] < 1) { |
||
60 | $options['keyMaxWidth'] = Arr::getMaxWidth($data); |
||
61 | } |
||
62 | |||
63 | // compare |
||
64 | if ((int) $options['keyMinWidth'] > $options['keyMaxWidth']) { |
||
65 | $options['keyMaxWidth'] = $options['keyMinWidth']; |
||
66 | } |
||
67 | |||
68 | foreach ($data as $key => $value) { |
||
69 | $hasKey = ! is_int($key); |
||
70 | $text .= $options['leftChar']; |
||
71 | |||
72 | if ($hasKey) { |
||
73 | $key = Str::padBoth((string) $key, $options['keyMaxWidth'], ' ', $keyPadPos); |
||
|
|||
74 | $text .= static::wrap($key, $keyStyle).$options['sepChar']; |
||
75 | } |
||
76 | |||
77 | // if value is array, translate array to string |
||
78 | if (is_array($value)) { |
||
79 | $temp = '['; |
||
80 | |||
81 | foreach ($value as $k => $val) { |
||
82 | if (is_bool($val)) { |
||
83 | $val = $val ? '(True)' : '(False)'; |
||
84 | } else { |
||
85 | $val = is_scalar($val) ? (string) $val : $val; |
||
86 | } |
||
87 | |||
88 | $temp .= ( ! is_numeric($k) ? "$k: " : '') . "$val, "; |
||
89 | } |
||
90 | |||
91 | $value = rtrim($temp, ' ,') . ']'; |
||
92 | } elseif (is_bool($value)) { |
||
93 | $value = $value ? '(True)' : '(False)'; |
||
94 | } else { |
||
95 | $value = (string) $value; |
||
96 | } |
||
97 | |||
98 | $value = $hasKey && $options['ucFirst'] ? ucfirst($value) : $value; |
||
99 | $text .= static::wrap($value, $options['valStyle'])."\n"; |
||
100 | } |
||
101 | |||
102 | return $text; |
||
103 | } |
||
121 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.