PrimeFactors   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 19
c 1
b 0
f 0
dl 0
loc 113
ccs 28
cts 28
cp 1
rs 10

8 Methods

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