Passed
Push — master ( 1e7555...a49478 )
by Pol
03:09
created

Combinations   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 5.43 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 16
cbo 1
dl 7
loc 129
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 9 2
A next() 0 8 2
A toArray() 7 10 2
A __construct() 0 5 1
A key() 0 4 1
A rewind() 0 5 1
A valid() 0 4 1
A count() 0 5 1
B nextHelper() 0 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Combinations.
9
 */
10
class Combinations extends Combinatorics implements \Iterator
11
{
12
    /**
13
     * The values.
14
     *
15
     * @var array
16
     */
17
    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...
18
19
    /**
20
     * The key.
21
     *
22
     * @var int
23
     */
24
    protected $key = 0;
25
26
    /**
27
     * Combinations constructor.
28
     *
29
     * @param array    $dataset
30
     *                          The dataset
31
     * @param int|null $length
32
     *                          The length
33
     */
34 10
    public function __construct(array $dataset = [], $length = null)
35
    {
36 10
        parent::__construct(array_values($dataset), $length);
37 10
        $this->rewind();
38 10
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function key()
44
    {
45
        return $this->key;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function current()
52
    {
53 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...
54 4
        for ($i = 0; $i < $this->length; ++$i) {
55 4
            $r[] = $this->dataset[$this->c[$i]];
56
        }
57
58 4
        return $r;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 4
    public function next()
65
    {
66 4
        if ($this->nextHelper()) {
67 3
            ++$this->key;
68
        } else {
69 4
            $this->key = -1;
70
        }
71 4
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 10
    public function rewind()
77
    {
78 10
        $this->c = range(0, $this->length);
79 10
        $this->key = 0;
80 10
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 4
    public function valid()
86
    {
87 4
        return $this->key >= 0;
88
    }
89
90
    /**
91
     * Convert the iterator into an array.
92
     *
93
     * @return array
94
     *               The elements
95
     */
96 4 View Code Duplication
    public function toArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98 4
        $data = [];
99
100 4
        for ($this->rewind(); $this->valid(); $this->next()) {
101 4
            $data[] = $this->current();
102
        }
103
104 4
        return $data;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 10
    public function count()
111
    {
112 10
        return $this->fact(count($this->getDataset())) /
113 10
          ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
114
    }
115
116
    /**
117
     * Custom next() callback.
118
     *
119
     * @return bool
120
     *              Return true or false
121
     */
122 4
    protected function nextHelper()
123
    {
124 4
        $i = $this->length - 1;
125 4
        while ($i >= 0 && $this->c[$i] === $this->datasetCount - $this->length + $i) {
126 4
            --$i;
127
        }
128 4
        if ($i < 0) {
129 4
            return false;
130
        }
131 3
        ++$this->c[$i];
132 3
        while ($i++ < $this->length - 1) {
133 3
            $this->c[$i] = $this->c[$i - 1] + 1;
134
        }
135
136 3
        return true;
137
    }
138
}
139