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

PrimeFactors::getFactors()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 6
Ratio 35.29 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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