| Conditions | 10 |
| Paths | 43 |
| Total Lines | 45 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 51 | protected function packArray(array $array, int $level = 0): string |
||
| 52 | { |
||
| 53 | if ($array === []) { |
||
| 54 | return '[]'; |
||
| 55 | } |
||
| 56 | //Delimiters between rows and sub-arrays. |
||
| 57 | $subIndent = "\n" . str_repeat(self::INDENT, $level + 2); |
||
| 58 | $keyIndent = "\n" . str_repeat(self::INDENT, $level + 1); |
||
| 59 | //No keys for associated array |
||
| 60 | $associated = array_diff_key($array, array_keys(array_keys($array))); |
||
| 61 | $result = []; |
||
| 62 | $innerIndent = 0; |
||
| 63 | if (!empty($associated)) { |
||
| 64 | foreach ($array as $key => $value) { |
||
| 65 | //Based on biggest key length |
||
| 66 | $innerIndent = max(strlen(var_export($key, true)), $innerIndent); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | foreach ($array as $key => $value) { |
||
| 70 | $prefix = ''; |
||
| 71 | if (!empty($associated)) { |
||
| 72 | //Key prefix |
||
| 73 | $prefix = str_pad( |
||
| 74 | var_export($key, true), |
||
| 75 | $innerIndent, |
||
| 76 | ' ', |
||
| 77 | STR_PAD_RIGHT |
||
| 78 | ) . ' => '; |
||
| 79 | } |
||
| 80 | if (!is_array($value)) { |
||
| 81 | $result[] = $prefix . $this->packValue($value); |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | if ($value === []) { |
||
| 85 | $result[] = $prefix . '[]'; |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | $subArray = $this->packArray($value, $level + 1); |
||
| 89 | $result[] = $prefix . "[{$subIndent}" . $subArray . "{$keyIndent}]"; |
||
| 90 | } |
||
| 91 | if ($level !== 0) { |
||
| 92 | return $result ? implode(",{$keyIndent}", $result) : ''; |
||
| 93 | } |
||
| 94 | |||
| 95 | return "[{$keyIndent}" . implode(",{$keyIndent}", $result) . "\n]"; |
||
| 96 | } |
||
| 139 |