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

Combinations   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 5.43 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 16
cbo 1
dl 7
loc 129
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 8 2
A rewind() 0 5 1
A valid() 0 4 1
A key() 0 4 1
A current() 0 9 2
A __construct() 0 5 1
A toArray() 7 10 2
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
 * @package drupol\phpermutations\Iterators
11
 */
12
class Combinations extends Combinatorics implements \Iterator
13
{
14
    /**
15
     * The values.
16
     *
17
     * @var array
18
     */
19
    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...
20
21
    /**
22
     * The key.
23
     *
24
     * @var int
25
     */
26
    protected $key = 0;
27
28
    /**
29
     * Combinations constructor.
30
     *
31
     * @param array $dataset
32
     *   The dataset.
33
     * @param int|null $length
34
     *   The length.
35
     */
36 10
    public function __construct(array $dataset = [], $length = null)
37
    {
38 10
        parent::__construct(array_values($dataset), $length);
39 10
        $this->rewind();
40 10
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function key()
46
    {
47
        return $this->key;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4
    public function current()
54
    {
55 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...
56 4
        for ($i = 0; $i < $this->length; $i++) {
57 4
            $r[] = $this->dataset[$this->c[$i]];
58 4
        }
59
60 4
        return $r;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    public function next()
67
    {
68 4
        if ($this->nextHelper()) {
69 3
            $this->key++;
70 3
        } else {
71 4
            $this->key = -1;
72
        }
73 4
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 10
    public function rewind()
79
    {
80 10
        $this->c = range(0, $this->length);
81 10
        $this->key = 0;
82 10
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function valid()
88
    {
89 4
        return $this->key >= 0;
90
    }
91
92
    /**
93
     * Convert the iterator into an array.
94
     *
95
     * @return array
96
     *   The elements.
97
     */
98 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...
99
    {
100 4
        $data = [];
101
102 4
        for ($this->rewind(); $this->valid(); $this->next()) {
103 4
            $data[] = $this->current();
104 4
        }
105
106 4
        return $data;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 10
    public function count()
113
    {
114 10
        return $this->fact(count($this->getDataset())) /
115 10
          ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
116
    }
117
118
    /**
119
     * Custom next() callback.
120
     *
121
     * @return bool
122
     *   Return true or false.
123
     */
124 4
    protected function nextHelper()
125
    {
126 4
        $i = $this->length - 1;
127 4
        while ($i >= 0 && $this->c[$i] === $this->datasetCount - $this->length + $i) {
128 4
            $i--;
129 4
        }
130 4
        if ($i < 0) {
131 4
            return false;
132
        }
133 3
        $this->c[$i]++;
134 3
        while ($i++ < $this->length - 1) {
135 3
            $this->c[$i] = $this->c[$i - 1] + 1;
136 3
        }
137
138 3
        return true;
139
    }
140
}
141