Completed
Push — master ( a49478...ff8a6e )
by Pol
01:00 queued 10s
created

Perfect::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Perfect::setMinLimit() 0 4 1
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
use drupol\phpermutations\IteratorInterface;
7
8
/**
9
 * Class Perfect.
10
 */
11
class Perfect extends Combinatorics implements IteratorInterface
12
{
13
    /**
14
     * The minimum limit.
15
     *
16
     * @var int
17
     */
18
    protected $min;
19
20
    /**
21
     * The maximum limit.
22
     *
23
     * @var int
24
     */
25
    protected $max;
26
27
    /**
28
     * The key.
29
     *
30
     * @var int
31
     */
32
    protected $key;
33
34
    /**
35
     * Perfect constructor.
36
     */
37 4
    public function __construct()
38
    {
39 4
        $this->setMaxLimit(PHP_INT_MAX);
40 4
        $this->setMinLimit(2);
41 4
        parent::__construct([], null);
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
        }
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 valid()
72
    {
73 4
        return $this->current() < $this->getMaxLimit();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function rewind()
80
    {
81 4
        $this->key = $this->getMinLimit();
82 4
    }
83
84
    /**
85
     * Set the maximum limit.
86
     *
87
     * @param int $max
88
     *                 The limit
89
     */
90 4
    public function setMaxLimit($max)
91
    {
92 4
        $this->max = $max;
93 4
    }
94
95
    /**
96
     * Get the maximum limit.
97
     *
98
     * @return int
99
     *             The limit
100
     */
101 4
    public function getMaxLimit()
102
    {
103 4
        return (int) $this->max;
104
    }
105
106
    /**
107
     * Set the minimum limit.
108
     *
109
     * @param int $min
110
     *                 The limit
111
     */
112 4
    public function setMinLimit($min)
113
    {
114 4
        $this->min = $min;
115 4
    }
116
117
    /**
118
     * Get the minimum limit.
119
     *
120
     * @return int
121
     *             The limit
122
     */
123 4
    public function getMinLimit()
124
    {
125 4
        return $this->min < 2 ? 2 : $this->min;
126
    }
127
128
    /**
129
     * Test if a number is perfect or not.
130
     *
131
     * Source: http://iceyboard.no-ip.org/projects/code/php/perfect_number/
132
     *
133
     * @param int $number
134
     *                    The number to test
135
     *
136
     * @return bool
137
     *              The true if the number is perfect, false otherwise
138
     */
139 4
    protected function isPerfectNumber($number)
140
    {
141 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...
142 4
        $max = sqrt($number);
143 4
        for ($n = 2; $n <= $max; ++$n) {
144 4
            if (!($number % $n)) {
145 4
                $d += $n;
146 4
                if ($n !== $number / $n) {
147 4
                    $d += $number / $n;
148
                }
149
            }
150
        }
151
152 4
        return ++$d === $number;
153
    }
154
}
155