Completed
Push — master ( 0b4795...d0bddc )
by Pol
02:26
created

PrimeFactors::setNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
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
   * 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 2
  public function current() {
39 2
    return current($this->factors);
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45 2
  public function next() {
46 2
    $this->key++;
47 2
    next($this->factors);
48 2
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53 2
  public function key() {
54 2
    return $this->key;
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60 2
  public function valid() {
61 2
    return isset($this->factors[$this->key()]);
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67 2
  public function rewind() {
68 2
    $this->key = 0;
69 2
  }
70
71
  /**
72
   * Count elements of an object.
73
   *
74
   * @return int
75
   *   The number of element.
76
   */
77 2
  public function count() {
78 2
    return count($this->factors);
79
  }
80
81
  /**
82
   * Convert the iterator into an array.
83
   *
84
   * @return array
85
   *   The elements.
86
   */
87 2
  public function toArray() {
88 2
    $data = array();
89
90 2
    for ($this->rewind(); $this->valid(); $this->next()) {
91 2
      $data[] = $this->current();
92 2
    }
93
94 2
    return $data;
95
  }
96
97
  /**
98
   * Set the number.
99
   *
100
   * @param int $number
101
   *   The number.
102
   */
103 2
  public function setNumber($number) {
104 2
    $this->number = $number;
105 2
    $this->factors = $this->getFactors($this->getNumber());
106 2
  }
107
108
  /**
109
   * Get the number.
110
   *
111
   * @return int
112
   *   The number.
113
   */
114 2
  public function getNumber() {
115 2
    return intval($this->number);
116
  }
117
118
  /**
119
   * Compute the prime factors of the number.
120
   *
121
   * @return int[]
122
   *   The factors.
123
   */
124 2
  private function getFactors($number) {
125 2
    if ($number <= 0) {
126
      $factors = array();
127
    }
128
129 2 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...
130 2
      while ($number % $i == 0) {
131 2
        $factors[] = $i;
132 2
        $number /= $i;
133 2
      }
134 2
    }
135
136 2
    if ($number > 1) {
137 2
      $factors[] = $number;
138 2
    }
139
140 2
    return $factors;
141
  }
142
143
}
144