| Conditions | 8 |
| Paths | 7 |
| Total Lines | 23 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | public static function arrayMapWithIndex(callable $fn, $arr) { |
||
| 12 | $result = []; |
||
| 13 | $assoc = null; |
||
| 14 | foreach ($arr as $idx => $value) { |
||
| 15 | list($newidx, $newvalue) = $fn($idx, $value); |
||
| 16 | |||
| 17 | if ($assoc === null) { |
||
| 18 | $assoc = $newidx !== null; |
||
| 19 | } |
||
| 20 | |||
| 21 | if ($newidx === null && $assoc || $newidx !== null && !$assoc) { |
||
| 22 | throw new \Exception("Mix of non assoc and assoc keys"); |
||
| 23 | } |
||
| 24 | |||
| 25 | if ($assoc) { |
||
| 26 | $result[$newidx] = $newvalue; |
||
| 27 | } else { |
||
| 28 | $result[] = $newvalue; |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | return $result; |
||
| 33 | } |
||
| 34 | } |
||
| 35 |