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

Combinations   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 32
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extend() 0 7 2
A getAllCombinations() 0 21 3
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