Completed
Push — master ( 6bb48a...cd3b63 )
by Pol
02:37
created

Prime::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
   * Perfect 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
    $n = abs($number);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $n. 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...
122 4
    $i = 2;
123
124 4
    if (10 < $number) {
125 4
      if (in_array(substr($number, -1), array(0, 2, 4, 6, 8))) {
126 4
        return FALSE;
127
      }
128 4
    }
129
130 4
    while ($i <= sqrt($n)) {
131 4
      if ($n % $i == 0) {
132 4
        return FALSE;
133
      }
134 4
      $i++;
135 4
    }
136
137 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...
138
  }
139
140
  /**
141
   * Set the maximum limit.
142
   *
143
   * @param int $max
144
   *   The limit.
145
   */
146 4
  public function setMaxLimit($max) {
147 4
    $this->max = $max;
148 4
  }
149
150
  /**
151
   * Get the maximum limit.
152
   *
153
   * @return int
154
   *   The limit.
155
   */
156 4
  public function getMaxLimit() {
157 4
    return intval($this->max);
158
  }
159
160
  /**
161
   * Set the minimum limit.
162
   *
163
   * @param int $min
164
   *   The limit.
165
   */
166 4
  public function setMinLimit($min) {
167 4
    $this->min = $min;
168 4
  }
169
170
  /**
171
   * Get the minimum limit.
172
   *
173
   * @return int
174
   *   The limit.
175
   */
176 4
  public function getMinLimit() {
177 4
    return $this->min <= 2 ? 2 : intval($this->min);
178
  }
179
180
}
181