Completed
Push — master ( c58b09...6bb48a )
by Pol
03:03
created

Perfect::setMinLimit()   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 1
crap 1
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Perfect.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Perfect 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(2);
41 4
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46 4
  public function current() {
47 4
    for ($i = $this->key(); $i < $this->getMaxLimit(); $i++) {
48 4
      if ($this->isPerfectNumber($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 perfect or not.
113
   *
114
   * Source: http://iceyboard.no-ip.org/projects/code/php/perfect_number/
115
   *
116
   * @param int $number
117
   *   The number to test.
118
   *
119
   * @return bool
120
   *   The true if the number is perfect, false otherwise.
121
   */
122 4
  protected function isPerfectNumber($number) {
123 4
    $d = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $d. 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...
124 4
    $max = sqrt($number);
125 4
    for ($n = 2; $n <= $max; $n++) {
126 4
      if (!($number % $n)) {
127 4
        $d += $n;
128 4
        if ($n <> $number / $n) {
129 4
          $d += $number / $n;
130 4
        }
131 4
      }
132 4
    }
133
134 4
    return ++$d == $number;
135
  }
136
137
  /**
138
   * Set the maximum limit.
139
   *
140
   * @param int $max
141
   *   The limit.
142
   */
143 4
  public function setMaxLimit($max) {
144 4
    $this->max = $max;
145 4
  }
146
147
  /**
148
   * Get the maximum limit.
149
   *
150
   * @return int
151
   *   The limit.
152
   */
153 4
  public function getMaxLimit() {
154 4
    return intval($this->max);
155
  }
156
157
  /**
158
   * Set the minimum limit.
159
   *
160
   * @param int $min
161
   *   The limit.
162
   */
163 4
  public function setMinLimit($min) {
164 4
    $this->min = $min;
165 4
  }
166
167
  /**
168
   * Get the minimum limit.
169
   *
170
   * @return int
171
   *   The limit.
172
   */
173 4
  public function getMinLimit() {
174 4
    return $this->min < 2 ? 2 : $this->min;
175
  }
176
177
}
178