| Conditions | 15 |
| Paths | 14 |
| Total Lines | 45 |
| Code Lines | 34 |
| 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 |
||
| 40 | public static function castColumns(array $matrix, array $castMap, $allKeysMustBePresent = true) |
||
| 41 | { |
||
| 42 | if (!is_bool($allKeysMustBePresent)) { |
||
| 43 | throw new InvalidArgumentException('Third parameter must be a boolean'); |
||
| 44 | } |
||
| 45 | if (empty($matrix)) { |
||
| 46 | return $matrix; |
||
| 47 | } |
||
| 48 | if (self::countMinDepth($matrix) < 2) { |
||
| 49 | throw new UnexpectedValueException('Can not cast columns on one dimensional array'); |
||
| 50 | } |
||
| 51 | |||
| 52 | foreach ($matrix as $key => $row) { |
||
| 53 | foreach ($castMap as $column => $type) { |
||
| 54 | if (isset($row[$column]) || array_key_exists($column, $row)) { |
||
| 55 | switch ($type) { |
||
| 56 | case self::TYPE_INT: |
||
| 57 | $matrix[$key][$column] = (int)$row[$column]; |
||
| 58 | break; |
||
| 59 | case self::TYPE_STRING: |
||
| 60 | $matrix[$key][$column] = (string)$row[$column]; |
||
| 61 | break; |
||
| 62 | case self::TYPE_FLOAT: |
||
| 63 | $matrix[$key][$column] = (float)$row[$column]; |
||
| 64 | break; |
||
| 65 | case self::TYPE_BOOL: |
||
| 66 | $matrix[$key][$column] = (bool)$row[$column]; |
||
| 67 | break; |
||
| 68 | case self::TYPE_ARRAY: |
||
| 69 | $matrix[$key][$column] = (array)$row[$column]; |
||
| 70 | break; |
||
| 71 | case self::TYPE_OBJECT: |
||
| 72 | $matrix[$key][$column] = (object)$row[$column]; |
||
| 73 | break; |
||
| 74 | default: |
||
| 75 | throw new UnexpectedValueException('Invalid type: ' . $type); |
||
| 76 | } |
||
| 77 | } elseif ($allKeysMustBePresent) { |
||
| 78 | throw new UnexpectedValueException('Column: ' . $column . ' missing in row: ' . $key); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | return $matrix; |
||
| 84 | } |
||
| 85 | |||
| 184 | } |