Completed
Push — master ( 72d462...18bdd2 )
by Pol
06:00 queued 04:04
created

Combinations   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 129
Duplicated Lines 5.43 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A key() 0 4 1
A current() 0 9 2
A next() 0 8 2
A rewind() 0 5 1
A valid() 0 4 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
    public function __construct(array $dataset = [], $length = null)
37
    {
38
        parent::__construct(array_values($dataset), $length);
39
        $this->rewind();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function key()
46
    {
47
        return $this->key;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function current()
54
    {
55
        $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
        for ($i = 0; $i < $this->length; $i++) {
57
            $r[] = $this->dataset[$this->c[$i]];
58
        }
59
60
        return $r;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function next()
67
    {
68
        if ($this->nextHelper()) {
69
            $this->key++;
70
        } else {
71
            $this->key = -1;
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function rewind()
79
    {
80
        $this->c = range(0, $this->length);
81
        $this->key = 0;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function valid()
88
    {
89
        return $this->key >= 0;
90
    }
91
92
    /**
93
     * Convert the iterator into an array.
94
     *
95
     * @return array
96
     *   The elements.
97
     */
98 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
        $data = [];
101
102
        for ($this->rewind(); $this->valid(); $this->next()) {
103
            $data[] = $this->current();
104
        }
105
106
        return $data;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function count()
113
    {
114
        return $this->fact(count($this->getDataset())) /
115
          ($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
    protected function nextHelper()
125
    {
126
        $i = $this->length - 1;
127
        while ($i >= 0 && $this->c[$i] === $this->datasetCount - $this->length + $i) {
128
            $i--;
129
        }
130
        if ($i < 0) {
131
            return false;
132
        }
133
        $this->c[$i]++;
134
        while ($i++ < $this->length - 1) {
135
            $this->c[$i] = $this->c[$i - 1] + 1;
136
        }
137
138
        return true;
139
    }
140
}
141