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

Prime   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 185
Duplicated Lines 5.41 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
cbo 1
dl 10
loc 185
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A count() 0 4 1
A toArray() 0 10 2
A current() 10 12 3
A getMaxLimit() 0 4 1
A __construct() 0 5 1
A setMaxLimit() 0 4 1
A setMinLimit() 0 4 1
A getMinLimit() 0 4 2
B isPrimeNumber() 0 23 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 Prime.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Prime extends Combinatorics implements \Iterator, \Countable
13
{
14
    /**
15
     * The minimum limit.
16
     *
17
     * @var int
18
     */
19
    protected $min;
20
21
    /**
22
     * The maximum limit.
23
     *
24
     * @var int
25
     */
26
    protected $max;
27
28
    /**
29
     * The key.
30
     *
31
     * @var int
32
     */
33
    protected $key;
34
35
    /**
36
     * Prime constructor.
37
     */
38 4
    public function __construct()
39
    {
40 4
        $this->setMaxLimit(PHP_INT_MAX);
41 4
        $this->setMinLimit(0);
42 4
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 4 View Code Duplication
    public function current()
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...
48
    {
49 4
        for ($i = $this->key(); $i < $this->getMaxLimit(); $i++) {
50 4
            if ($this->isPrimeNumber($i)) {
51 4
                $this->key = $i;
52
53 4
                return $i;
54
            }
55 4
        }
56
57 4
        return $this->getMaxLimit();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    public function next()
64
    {
65 4
        $this->key++;
66 4
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function key()
72
    {
73 4
        return $this->key;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function valid()
80
    {
81 4
        return $this->current() < $this->getMaxLimit();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function rewind()
88
    {
89 4
        $this->key = $this->getMinLimit();
90 4
    }
91
92
    /**
93
     * Count elements of an object.
94
     *
95
     * @return int
96
     *   The number of element.
97
     */
98 4
    public function count()
99
    {
100 4
        return count($this->toArray());
101
    }
102
103
    /**
104
     * Convert the iterator into an array.
105
     *
106
     * @return array
107
     *   The elements.
108
     */
109 4
    public function toArray()
110
    {
111 4
        $data = [];
112
113 4
        for ($this->rewind(); $this->valid(); $this->next()) {
114 4
            $data[] = $this->current();
115 4
        }
116
117 4
        return $data;
118
    }
119
120
    /**
121
     * Set the maximum limit.
122
     *
123
     * @param int $max
124
     *   The limit.
125
     */
126 4
    public function setMaxLimit($max)
127
    {
128 4
        $this->max = $max;
129 4
    }
130
131
    /**
132
     * Get the maximum limit.
133
     *
134
     * @return int
135
     *   The limit.
136
     */
137 4
    public function getMaxLimit()
138
    {
139 4
        return intval($this->max);
140
    }
141
142
    /**
143
     * Set the minimum limit.
144
     *
145
     * @param int $min
146
     *   The limit.
147
     */
148 4
    public function setMinLimit($min)
149
    {
150 4
        $this->min = $min;
151 4
    }
152
153
    /**
154
     * Get the minimum limit.
155
     *
156
     * @return int
157
     *   The limit.
158
     */
159 4
    public function getMinLimit()
160
    {
161 4
        return $this->min <= 2 ? 2 : intval($this->min);
162
    }
163
164
    /**
165
     * Test if a number is prime or not.
166
     *
167
     * @param int $number
168
     *   The number to test.
169
     *
170
     * @return bool
171
     *   The true if the number is prime, false otherwise.
172
     */
173 4
    protected function isPrimeNumber($number)
174
    {
175 4
        $number = abs($number);
176
177
        // 2 is an exception.
178 4
        if (2 === $number) {
179 2
            return true;
180
        }
181
182
        // Check if number is even.
183 4
        if (!($number & 1)) {
184 4
            return false;
185
        }
186
187 4
        $sqrtNumber = sqrt($number);
188 4
        for ($divisor = 3; $divisor <= $sqrtNumber; $divisor += 2) {
189 4
            if ($number % $divisor === 0) {
190 4
                return false;
191
            }
192 4
        }
193
194 4
        return true;
195
    }
196
}
197