Completed
Push — master ( a904eb...ab3d80 )
by Pol
02:45
created

Prime   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 172
Duplicated Lines 5.81 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A current() 10 10 3
A next() 0 3 1
A key() 0 3 1
A valid() 0 3 1
A rewind() 0 3 1
A count() 0 3 1
A toArray() 0 9 2
A setMaxLimit() 0 3 1
A getMaxLimit() 0 3 1
A setMinLimit() 0 3 1
A getMinLimit() 0 3 2
B isPrimeNumber() 0 22 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 4
    $this->setMaxLimit(PHP_INT_MAX);
40 4
    $this->setMinLimit(0);
41 4
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46 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...
47 4
    for ($i = $this->key(); $i < $this->getMaxLimit(); $i++) {
48 4
      if ($this->isPrimeNumber($i)) {
49 4
        $this->key = $i;
50 4
        return $i;
51
      }
52 4
    }
53
54 4
    return $this->getMaxLimit();
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60 4
  public function next() {
61 4
    $this->key++;
62 4
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67 4
  public function key() {
68 4
    return $this->key;
69
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74 4
  public function valid() {
75 4
    return $this->current() < $this->getMaxLimit();
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81 4
  public function rewind() {
82 4
    $this->key = $this->getMinLimit();
83 4
  }
84
85
  /**
86
   * Count elements of an object.
87
   *
88
   * @return int
89
   *   The number of element.
90
   */
91 4
  public function count() {
92 4
    return count($this->toArray());
93
  }
94
95
  /**
96
   * Convert the iterator into an array.
97
   *
98
   * @return array
99
   *   The elements.
100
   */
101 4
  public function toArray() {
102 4
    $data = array();
103
104 4
    for ($this->rewind(); $this->valid(); $this->next()) {
105 4
      $data[] = $this->current();
106 4
    }
107
108 4
    return $data;
109
  }
110
111
  /**
112
   * Test if a number is prime or not.
113
   *
114
   * @param int $number
115
   *   The number to test.
116
   *
117
   * @return bool
118
   *   The true if the number is prime, false otherwise.
119
   */
120 4
  protected function isPrimeNumber($number) {
121 4
    $number = abs($number);
122
123
    // 2 is an exception.
124 4
    if (2 == $number) {
125 2
      return TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
126
    }
127
128
    // Check if number is even
129 4
    if (!($number & 1)) {
130 4
      return FALSE;
131
    }
132
133 4
    $sqrtNumber = sqrt($number);
134 4
    for ($divisor = 3; $divisor <= $sqrtNumber; $divisor += 2) {
135 4
      if ($number % $divisor == 0) {
136 4
        return FALSE;
137
      }
138 4
    }
139
140 4
    return TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
141
  }
142
143
  /**
144
   * Set the maximum limit.
145
   *
146
   * @param int $max
147
   *   The limit.
148
   */
149 4
  public function setMaxLimit($max) {
150 4
    $this->max = $max;
151 4
  }
152
153
  /**
154
   * Get the maximum limit.
155
   *
156
   * @return int
157
   *   The limit.
158
   */
159 4
  public function getMaxLimit() {
160 4
    return intval($this->max);
161
  }
162
163
  /**
164
   * Set the minimum limit.
165
   *
166
   * @param int $min
167
   *   The limit.
168
   */
169 4
  public function setMinLimit($min) {
170 4
    $this->min = $min;
171 4
  }
172
173
  /**
174
   * Get the minimum limit.
175
   *
176
   * @return int
177
   *   The limit.
178
   */
179 4
  public function getMinLimit() {
180 4
    return $this->min <= 2 ? 2 : intval($this->min);
181
  }
182
183
}
184