Passed
Push — master ( 18bdd2...b79ef4 )
by Pol
04:33
created

Permutations   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
cbo 2
dl 0
loc 91
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generator() 0 8 3
A toArray() 0 10 2
A count() 0 4 1
A get() 0 16 4
1
<?php
2
3
namespace drupol\phpermutations\Generators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Permutations.
9
 *
10
 * @package drupol\phpermutations\Generators
11
 *
12
 * Inspired by the work of Mark Wilson <[email protected]>
13
 */
14
class Permutations extends Combinatorics
15
{
16
    /**
17
     * The combinations generator.
18
     *
19
     * @var \drupol\phpermutations\Generators\Combinations
20
     */
21
    protected $combinations;
22
23
    /**
24
     * Combinatorics constructor.
25
     *
26
     * @param array $dataset
27
     *   The dataset.
28
     * @param int|null $length
29
     *   The length.
30
     */
31 2
    public function __construct(array $dataset = [], $length = null)
32
    {
33 2
        parent::__construct($dataset, $length);
34 2
        $this->combinations = new Combinations($dataset, $this->getLength());
35 2
    }
36
37
    /**
38
     * Alias of the get() method.
39
     *
40
     * @codingStandardsIgnoreStart
41
     * @return \Generator
42
     * @codingStandardsIgnoreEnd
43
     */
44 2
    public function generator()
45
    {
46 2
        foreach ($this->combinations->generator() as $combination) {
47 2
            foreach ($this->get($combination) as $current) {
48 2
                yield $current;
49 2
            }
50 2
        }
51 2
    }
52
53
    /**
54
     * Convert the generator into an array.
55
     *
56
     * @return array
57
     *   The elements.
58
     */
59 2
    public function toArray()
60
    {
61 2
        $data = [];
62
63 2
        foreach ($this->generator() as $value) {
64 2
            $data[] = $value;
65 2
        }
66
67 2
        return $data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function count()
74
    {
75 2
        return $this->combinations->count() * $this->fact($this->getLength());
76
    }
77
78
    /**
79
     * The permutations generator.
80
     *
81
     * @param array $dataset
82
     *   The dataset.
83
     *
84
     * @codingStandardsIgnoreStart
85
     * @return \Generator
86
     * @codingStandardsIgnoreEnd
87
     */
88 2
    protected function get(array $dataset)
89
    {
90 2
        foreach ($dataset as $key => $firstItem) {
91 2
            $remaining = $dataset;
92 2
            array_splice($remaining, $key, 1);
93 2
            if (count($remaining) === 0) {
94 2
                yield [$firstItem];
95
96 2
                continue;
97
            }
98 2
            foreach ($this->get($remaining) as $permutation) {
99 2
                array_unshift($permutation, $firstItem);
100 2
                yield $permutation;
101 2
            }
102 2
        }
103 2
    }
104
}
105