Passed
Push — main ( 36bb88...fbfcc4 )
by Alexandra
03:15
created

Combinations::extend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AHJ\ApprovalTests;
6
7
final class Combinations
8
{
9 6
    public function getAllCombinations(array $arguments, array $currentCombination = []): array
10
    {
11 6
        $combinations = [];
12 6
        $argumentsTail = $arguments;
13 6
        $key = key($argumentsTail);
14 6
        unset($argumentsTail[$key]);
15
16 6
        if (0 === count($arguments)) {
17 6
            return [$currentCombination];
18
        }
19
20 6
        foreach (current($arguments) as $element) {
21 6
            $newCombination = $currentCombination;
22 6
            array_push($newCombination, $element);
23 6
            $combinations = $this->extend(
24 6
                $this->getAllCombinations($argumentsTail, $newCombination),
25
                $combinations
26
            );
27
        }
28
29 6
        return $combinations;
30
    }
31
32 6
    private function extend(array $first, array $second): array
33
    {
34 6
        foreach ($second as $value) {
35 6
            $first[] = $value;
36
        }
37
38 6
        return $first;
39
    }
40
}
41