Completed
Push — master ( a49478...ff8a6e )
by Pol
01:00 queued 10s
created

Combinations   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
cbo 1
dl 0
loc 107
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 10 2
A next() 0 8 2
A rewind() 0 5 1
A valid() 0 4 1
A count() 0 5 1
B nextHelper() 0 18 5
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
use drupol\phpermutations\IteratorInterface;
7
8
/**
9
 * Class Combinations.
10
 */
11
class Combinations extends Combinatorics implements IteratorInterface
12
{
13
    /**
14
     * The values.
15
     *
16
     * @var array
17
     */
18
    protected $c = [];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
19
20
    /**
21
     * The key.
22
     *
23
     * @var int
24
     */
25
    protected $key = 0;
26
27
    /**
28
     * Combinations constructor.
29
     *
30
     * @param array    $dataset
31
     *                          The dataset
32
     * @param int|null $length
33
     *                          The length
34
     */
35 10
    public function __construct(array $dataset = [], $length = null)
36
    {
37 10
        parent::__construct(array_values($dataset), $length);
38 10
        $this->rewind();
39 10
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 4
    public function current()
45
    {
46 4
        $r = [];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
47
48 4
        for ($i = 0; $i < $this->length; ++$i) {
49 4
            $r[] = $this->dataset[$this->c[$i]];
50
        }
51
52 4
        return $r;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 4
    public function next()
59
    {
60 4
        if ($this->nextHelper()) {
61 3
            ++$this->key;
62
        } else {
63 4
            $this->key = -1;
64
        }
65 4
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 10
    public function rewind()
71
    {
72 10
        $this->c = range(0, $this->length);
73 10
        $this->key = 0;
74 10
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function valid()
80
    {
81 4
        return $this->key >= 0;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 10
    public function count()
88
    {
89 10
        return $this->fact(count($this->getDataset())) /
90 10
          ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
91
    }
92
93
    /**
94
     * Custom next() callback.
95
     *
96
     * @return bool
97
     *              Return true or false
98
     */
99 4
    protected function nextHelper()
100
    {
101 4
        $i = $this->length - 1;
102 4
        while ($i >= 0 && $this->c[$i] === $this->datasetCount - $this->length + $i) {
103 4
            --$i;
104
        }
105
106 4
        if ($i < 0) {
107 4
            return false;
108
        }
109
110 3
        ++$this->c[$i];
111 3
        while ($i++ < $this->length - 1) {
112 3
            $this->c[$i] = $this->c[$i - 1] + 1;
113
        }
114
115 3
        return true;
116
    }
117
}
118