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

PrimeFactors::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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