Completed
Push — master ( 0318a4...4ae196 )
by Pol
02:45
created

Prime::rewind()   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 {
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
  }
83
84
  /**
85
   * @return int
86
   */
87 2
  public function count() {
88 2
    $this->count = 0;
89
90 2 View Code Duplication
    for ($j = 2; $j <= $this->getMaxLimit(); $j++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91 2
      if ($this->isPrimeNumber($j)) {
92 2
        $this->count++;
93 2
      }
94 2
    }
95
96 2
    return $this->count;
97
  }
98
99
  /**
100
   * @return array
101
   */
102 2
  public function toArray() {
103 2
    $data = array();
104
105 2
    for ($this->rewind(); $this->valid(); $this->next()) {
106 2
      $data[] = $this->current();
107 2
    }
108
109 2
    return $data;
110
  }
111
112
113
  /**
114
   * @param $number
115
   *
116
   * @return bool
117
   */
118 2
  protected function isPrimeNumber($number) {
119 2
    $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...
120 2
    $i = 2;
121
122 2
    while ($i <= sqrt($n)) {
123 2
      if ($n % $i == 0) {
124 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...
125
      }
126 2
      $i++;
127 2
    }
128
129 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...
130
  }
131
132
  /**
133
   * @param int $max
134
   */
135 2
  public function setMaxLimit($max) {
136 2
    $this->max = $max;
137 2
  }
138
139
  /**
140
   * @return int
141
   */
142 2
  public function getMaxLimit() {
143 2
    return intval($this->max);
144
  }
145
146
}
147