| Conditions | 10 |
| Paths | 31 |
| Total Lines | 32 |
| Code Lines | 20 |
| 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 |
||
| 64 | private function cleanRawData(array $rawData, int $numericPrefixLength): array |
||
| 65 | { |
||
| 66 | $data = []; |
||
| 67 | foreach ($rawData as $rawKey => $value) { |
||
| 68 | if (0 !== $numericPrefixLength && 0 === strpos($rawKey, $this->numericPrefix)) { |
||
| 69 | $rawSubKey = substr($rawKey, $numericPrefixLength); |
||
| 70 | if (is_numeric($rawSubKey)) { |
||
| 71 | $rawKey = $rawSubKey; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $key = is_numeric($rawKey) ? (int) $rawKey : $rawKey; |
||
| 76 | |||
| 77 | if (is_array($value)) { |
||
| 78 | $data[$key] = $this->cleanRawData($value, $numericPrefixLength); |
||
| 79 | } else { |
||
| 80 | if (is_numeric($value)) { |
||
| 81 | if ((string) (int) $value === $value) { |
||
| 82 | $value = (int) $value; |
||
| 83 | } else { |
||
| 84 | $value = (float) $value; |
||
| 85 | } |
||
| 86 | } elseif ('' === $value) { |
||
| 87 | $value = null; |
||
| 88 | } |
||
| 89 | |||
| 90 | $data[$key] = $value; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $data; |
||
| 95 | } |
||
| 96 | } |
||
| 97 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.