Passed
Push — master ( 65ca73...10de3a )
by Pol
02:10
created

PrimeFactors   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 4.23 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
cbo 1
dl 6
loc 142
ccs 0
cts 58
cp 0
rs 10
c 1
b 0
f 0

10 Methods

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