Test Setup Failed
Push — master ( 9a00d4...fd0b8a )
by Pol
06:00
created

Fibonacci   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 11
cbo 1
dl 0
loc 119
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A current() 0 3 1
A key() 0 3 1
A next() 0 4 1
A rewind() 0 5 1
A valid() 0 3 1
A count() 0 3 1
A setMaxLimit() 0 3 1
A getMaxLimit() 0 3 1
A toArray() 0 9 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Fibonacci.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Fibonacci extends Combinatorics implements \Iterator, \Countable {
13
14
  /**
15
   * The maximum limit.
16
   *
17
   * @var int
18
   */
19
  protected $max;
20
21
  /**
22
   * The previous element.
23
   *
24
   * @var int
25
   */
26
  private $previous = 1;
27
28
  /**
29
   * The current element.
30
   *
31
   * @var int
32
   */
33
  private $current = 0;
34
35
  /**
36
   * The current key.
37
   *
38
   * @var int
39
   */
40
  private $key = 0;
41
42
  /**
43
   * Fibonacci constructor.
44
   */
45 2
  public function __construct() {
46 2
    $this->setMaxLimit(PHP_INT_MAX);
47 2
  }
48
49
  /**
50
   * {@inheritdoc}
51
   */
52 2
  public function current() {
53 2
    return $this->current;
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function key() {
60
    return $this->key;
61
  }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66 2
  public function next() {
67 2
    list($this->current, $this->previous) = array($this->current + $this->previous, $this->current);
68 2
    $this->key++;
69 2
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74 2
  public function rewind() {
75 2
    $this->previous = 1;
76 2
    $this->current = 0;
77 2
    $this->key = 0;
78 2
  }
79
80
  /**
81
   * {@inheritdoc}
82
   */
83 2
  public function valid() {
84 2
    return ($this->current < $this->getMaxLimit());
85
  }
86
87
  /**
88
   * {@inheritdoc}
89
   */
90 2
  public function count() {
91 2
    return count($this->toArray());
92
  }
93
94
  /**
95
   * Convert the iterator into an array.
96
   *
97
   * @return array
98
   *   The elements.
99
   */
100 2
  public function toArray() {
101 2
    $data = array();
102
103 2
    for ($this->rewind(); $this->valid(); $this->next()) {
104 2
      $data[] = $this->current();
105
    }
106
107 2
    return $data;
108
  }
109
110
  /**
111
   * Set the maximum limit.
112
   *
113
   * @param int $max
114
   *   The limit.
115
   */
116 2
  public function setMaxLimit($max) {
117 2
    $this->max = $max;
118 2
  }
119
120
  /**
121
   * Get the maximum limit.
122
   *
123
   * @return int
124
   *   The limit.
125
   */
126 2
  public function getMaxLimit() {
127 2
    return intval($this->max);
128
  }
129
130
}
131