Passed
Push — master ( 1e7555...a49478 )
by Pol
03:09
created

PrimeFactors::getFactors()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 19
Code Lines 10

Duplication

Lines 6
Ratio 31.58 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 6
loc 19
ccs 9
cts 10
cp 0.9
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 10
nc 12
nop 1
crap 5.025
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class PrimeFactors.
9
 */
10
class PrimeFactors extends Combinatorics implements \Iterator, \Countable
11
{
12
    /**
13
     * The number.
14
     *
15
     * @var int
16
     */
17
    protected $number;
18
19
    /**
20
     * The key.
21
     *
22
     * @var int
23
     */
24
    protected $key;
25
26
    /**
27
     * The prime factors.
28
     *
29
     * @var int[]
30
     */
31
    protected $factors;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 6
    public function current()
37
    {
38 6
        return current($this->factors);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function next()
45
    {
46 6
        ++$this->key;
47 6
        next($this->factors);
48 6
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 6
    public function key()
54
    {
55 6
        return $this->key;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 6
    public function valid()
62
    {
63 6
        return isset($this->factors[$this->key()]);
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 6
    public function rewind()
70
    {
71 6
        $this->key = 0;
72 6
    }
73
74
    /**
75
     * Count elements of an object.
76
     *
77
     * @return int
78
     *             The number of element
79
     */
80 6
    public function count()
81
    {
82 6
        return count($this->factors);
83
    }
84
85
    /**
86
     * Convert the iterator into an array.
87
     *
88
     * @return array
89
     *               The elements
90
     */
91 6
    public function toArray()
92
    {
93 6
        $data = [];
94
95 6
        for ($this->rewind(); $this->valid(); $this->next()) {
96 6
            $data[] = $this->current();
97
        }
98
99 6
        return $data;
100
    }
101
102
    /**
103
     * Set the number.
104
     *
105
     * @param int $number
106
     *                    The number
107
     */
108 6
    public function setNumber($number)
109
    {
110 6
        $this->number = $number;
111 6
        $this->factors = $this->getFactors($this->getNumber());
112 6
    }
113
114
    /**
115
     * Get the number.
116
     *
117
     * @return int
118
     *             The number
119
     */
120 6
    public function getNumber()
121
    {
122 6
        return intval($this->number);
123
    }
124
125
    /**
126
     * Compute the prime factors of the number.
127
     *
128
     * @param mixed $number
129
     *
130
     * @return int[]
131
     *               The factors
132
     */
133 6
    private function getFactors($number)
134
    {
135 6
        if ($number <= 0) {
136
            $factors = [];
137
        }
138
139 6 View Code Duplication
        for ($i = 2; $i <= $number / $i; ++$i) {
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...
140 6
            while (0 === $number % $i) {
141 4
                $factors[] = $i;
142 4
                $number /= $i;
143
            }
144
        }
145
146 6
        if ($number > 1) {
147 4
            $factors[] = $number;
148
        }
149
150 6
        return $factors;
151
    }
152
}
153