| Conditions | 8 |
| Paths | 8 |
| Total Lines | 21 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 14 |
| CRAP Score | 8 |
| Changes | 0 | ||
| 1 | <?php |
||
| 28 | function to_array($items, $force = false) |
||
| 29 | { |
||
| 30 | 10 | if (method_exists($items, 'toArray')) { |
|
| 31 | 2 | return $items->toArray(); |
|
| 32 | 8 | } elseif (is_array($items)) { |
|
| 33 | 4 | return $items; |
|
| 34 | 4 | } elseif ($items instanceof Traversable) { |
|
| 35 | 1 | return iterator_to_array($items); |
|
| 36 | 3 | } elseif (is_string($items)) { |
|
| 37 | 2 | $json = json_decode($items, true); |
|
| 38 | 2 | if (is_array($json) && json_last_error() == JSON_ERROR_NONE) { |
|
| 39 | 1 | return $json; |
|
| 40 | } |
||
| 41 | 1 | } |
|
| 42 | |||
| 43 | 2 | if ($force) { |
|
| 44 | 1 | return (array) $items; |
|
| 45 | } |
||
| 46 | |||
| 47 | throw new RuntimeException(__FUNCTION__ . " could not convert items to an array"); |
||
| 48 | } |