| Conditions | 12 |
| Paths | 27 |
| Total Lines | 30 |
| Code Lines | 17 |
| 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 |
||
| 20 | public static function getColumns(array $array, array $columns, $allRowsMustHaveAllColumns = false) |
||
| 21 | { |
||
| 22 | // validation |
||
| 23 | foreach ($array as $key => $row) { |
||
| 24 | if (!is_array($row)) { |
||
| 25 | throw new UnexpectedValueException('Array element "' . $key . '" is not an array'); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | foreach ($columns as $key => $column) { |
||
| 29 | if (!is_string($column) && !is_numeric($column)) { |
||
| 30 | throw new InvalidArgumentException('Invalid column type in columns array, index "' . $key . '"'); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | if (!is_bool($allRowsMustHaveAllColumns)) { |
||
| 34 | throw new InvalidArgumentException('allRowsMustHaveAllColumns flag must be boolean'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $return = array_fill_keys($columns, array()); |
||
| 38 | foreach ($array as $key => $row) { |
||
| 39 | foreach ($columns as $column) { |
||
| 40 | if (isset($row[$column]) || array_key_exists($column, $row)) { |
||
| 41 | $return[$column][$key] = $row[$column]; |
||
| 42 | } elseif ($allRowsMustHaveAllColumns) { |
||
| 43 | throw new UnexpectedValueException('Row "' . $key . '" is missing column: "' . $column . '"'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | return $return; |
||
| 49 | } |
||
| 50 | |||
| 116 | } |