Completed
Push — master ( fa4893...deeb78 )
by Pol
06:37
created

PrimeFactors   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 142
Duplicated Lines 4.23 %

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