Passed
Push — master ( 18bdd2...b79ef4 )
by Pol
04:33
created

Perfect::current()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 10
Ratio 83.33 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 3
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
    {
40 4
        $this->setMaxLimit(PHP_INT_MAX);
41 4
        $this->setMinLimit(2);
42 4
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 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...
48
    {
49 4
        for ($i = $this->key(); $i < $this->getMaxLimit(); $i++) {
50 4
            if ($this->isPerfectNumber($i)) {
51 4
                $this->key = $i;
52
53 4
                return $i;
54
            }
55 4
        }
56
57 4
        return $this->getMaxLimit();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    public function next()
64
    {
65 4
        $this->key++;
66 4
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function key()
72
    {
73 4
        return $this->key;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function valid()
80
    {
81 4
        return $this->current() < $this->getMaxLimit();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 4
    public function rewind()
88
    {
89 4
        $this->key = $this->getMinLimit();
90 4
    }
91
92
    /**
93
     * Count elements of an object.
94
     *
95
     * @return int
96
     *   The number of element.
97
     */
98 4
    public function count()
99
    {
100 4
        return count($this->toArray());
101
    }
102
103
    /**
104
     * Convert the iterator into an array.
105
     *
106
     * @return array
107
     *   The elements.
108
     */
109 4
    public function toArray()
110
    {
111 4
        $data = [];
112
113 4
        for ($this->rewind(); $this->valid(); $this->next()) {
114 4
            $data[] = $this->current();
115 4
        }
116
117 4
        return $data;
118
    }
119
120
    /**
121
     * Set the maximum limit.
122
     *
123
     * @param int $max
124
     *   The limit.
125
     */
126 4
    public function setMaxLimit($max)
127
    {
128 4
        $this->max = $max;
129 4
    }
130
131
    /**
132
     * Get the maximum limit.
133
     *
134
     * @return int
135
     *   The limit.
136
     */
137 4
    public function getMaxLimit()
138
    {
139 4
        return intval($this->max);
140
    }
141
142
    /**
143
     * Set the minimum limit.
144
     *
145
     * @param int $min
146
     *   The limit.
147
     */
148 4
    public function setMinLimit($min)
149
    {
150 4
        $this->min = $min;
151 4
    }
152
153
    /**
154
     * Get the minimum limit.
155
     *
156
     * @return int
157
     *   The limit.
158
     */
159 4
    public function getMinLimit()
160
    {
161 4
        return $this->min < 2 ? 2 : $this->min;
162
    }
163
164
    /**
165
     * Test if a number is perfect or not.
166
     *
167
     * Source: http://iceyboard.no-ip.org/projects/code/php/perfect_number/
168
     *
169
     * @param int $number
170
     *   The number to test.
171
     *
172
     * @return bool
173
     *   The true if the number is perfect, false otherwise.
174
     */
175 4
    protected function isPerfectNumber($number)
176
    {
177 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...
178 4
        $max = sqrt($number);
179 4
        for ($n = 2; $n <= $max; $n++) {
180 4
            if (!($number % $n)) {
181 4
                $d += $n;
182 4
                if ($n !== $number / $n) {
183 4
                    $d += $number / $n;
184 4
                }
185 4
            }
186 4
        }
187
188 4
        return ++$d === $number;
189
    }
190
}
191