Completed
Push — master ( cedc93...ab3e34 )
by Pol
14:19
created

Prime::isPrimeNumber()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 7
nop 1
crap 5
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
   * @var int
16
   */
17
  protected $max;
18
19
  /**
20
   * @var int
21
   */
22
  protected $key;
23
24
  /**
25
   * @var int
26
   */
27
  protected $count = 1;
28
29
  /**
30
   * @var int
31
   */
32
  protected $current = 2;
33
34
  /**
35
   * Prime constructor.
36
   */
37 2
  public function __construct() {
38 2
    $this->setMaxLimit(PHP_INT_MAX);
39 2
  }
40
41
  /**
42
   * @inheritdoc
43
   */
44 2
  public function current() {
45 2
    return $this->current;
46
  }
47
48
  /**
49
   * @inheritdoc
50
   */
51 2
  public function next() {
52 2
    ++$this->key;
53 2
    for($i = $this->key; $i < $this->getMaxLimit(); $i++) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
54 2
      if ($this->isPrimeNumber($i)) {
55 2
        $this->key = $i;
56 2
        $this->current = $i;
57 2
        $this->count++;
58 2
        break;
59
      }
60 2
    }
61 2
  }
62
63
  /**
64
   * @inheritdoc
65
   */
66 2
  public function key() {
67 2
    return $this->key;
68
  }
69
70
  /**
71
   * @inheritdoc
72
   */
73 2
  public function valid() {
74 2
    return ($this->current == $this->key());
75
  }
76
77
  /**
78
   * @inheritdoc
79
   */
80 2
  public function rewind() {
81 2
    $this->key = 2;
82 2
    $this->current = 2;
83
  }
84
85
  /**
86
   * @return int
87 2
   */
88 2
  public function count() {
89
    return count($this->toArray());
90 2
  }
91 2
92 2
  /**
93 2
   * @return array
94 2
   */
95
  public function toArray() {
96 2
    $data = array();
97
98
    for ($this->rewind(); $this->valid(); $this->next()) {
99
      $data[] = $this->current();
100
    }
101
102 2
    return $data;
103 2
  }
104
105 2
106 2
  /**
107 2
   * @param $number
108
   *
109 2
   * @return bool
110
   */
111
  protected function isPrimeNumber($number) {
112
    $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...
113
    $i = 2;
114
115
    if (10 < $number) {
116
      if (in_array(substr($number, -1 ), array(0, 2, 4, 6, 8))) {
117
        return FALSE;
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
118 2
      }
119 2
    }
120 2
121
    while ($i <= sqrt($n)) {
122 2
      if ($n % $i == 0) {
123 2
        return FALSE;
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
124 2
      }
125
      $i++;
126 2
    }
127 2
128
    return TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
129 2
  }
130
131
  /**
132
   * @param int $max
133
   */
134
  public function setMaxLimit($max) {
135 2
    $this->max = $max;
136 2
  }
137 2
138
  /**
139
   * @return int
140
   */
141
  public function getMaxLimit() {
142 2
    return intval($this->max);
143 2
  }
144
145
}
146