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

PrimeFactors   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 4.2 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 97.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
cbo 1
dl 6
loc 143
ccs 35
cts 36
cp 0.9722
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 10 2
A current() 0 4 1
A next() 0 5 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A count() 0 4 1
A setNumber() 0 5 1
A getNumber() 0 4 1
B getFactors() 6 19 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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