| Conditions | 13 |
| Paths | 13 |
| Total Lines | 15 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 10 |
| CRAP Score | 13 |
| 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 |
||
| 18 | function iterable_column(iterable $iterable, $valueColumn, $keyColumn = null): \Generator |
||
| 19 | { |
||
| 20 | 23 | foreach ($iterable as $key => $value) { |
|
| 21 | 22 | if (is_array($value) || (is_object($value) && $value instanceof \ArrayAccess)) { |
|
| 22 | 15 | $key = isset($keyColumn) ? ($value[$keyColumn] ?? null) : $key; |
|
| 23 | 15 | $value = isset($valueColumn) ? ($value[$valueColumn] ?? null) : $value; |
|
| 24 | 15 | } elseif (is_object($value) && !$value instanceof \DateTimeInterface) { |
|
| 25 | 15 | $key = isset($keyColumn) ? ($value->$keyColumn ?? null) : $key; |
|
| 26 | 15 | $value = isset($valueColumn) ? ($value->$valueColumn ?? null) : $value; |
|
| 27 | } else { |
||
| 28 | 1 | $key = isset($keyColumn) ? null : $key; |
|
| 29 | 1 | $value = isset($valueColumn) ? null : $value; |
|
| 30 | } |
||
| 31 | |||
| 32 | 22 | yield $key => $value; |
|
| 33 | } |
||
| 35 |