Completed
Push — master ( 4ae196...831fe8 )
by Pol
02:12
created

Fibonacci   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 11
cbo 1
dl 0
loc 102
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 toArray() 0 9 2
A setMaxLimit() 0 3 1
A getMaxLimit() 0 3 1
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 {
13
14
  /**
15
   * @var int
16
   */
17
  protected $max;
18
19
  /**
20
   * @var int
21
   */
22
  private $previous = 1;
23
24
  /**
25
   * @var int
26
   */
27
  private $current = 0;
28
29
  /**
30
   * @var int
31
   */
32
  private $key = 0;
33
34
  /**
35
   * Fibonacci constructor.
36
   */
37 2
  public function __construct() {
38 2
    $this->setMaxLimit(PHP_INT_MAX);
39 2
  }
40
41
  /**
42
   * @return int
43
   */
44 2
  public function current() {
45 2
    return $this->current;
46
  }
47
48
  /**
49
   * @return int
50
   */
51
  public function key() {
52
    return $this->key;
53
  }
54
55
  /**
56
   *
57
   */
58 2
  public function next() {
59 2
    list($this->current, $this->previous) = array($this->current + $this->previous, $this->current);
60 2
    $this->key++;
61 2
  }
62
63
  /**
64
   *
65
   */
66 2
  public function rewind() {
67 2
    $this->previous = 1;
68 2
    $this->current = 0;
69 2
    $this->key = 0;
70 2
  }
71
72
  /**
73
   * @return bool
74
   */
75 2
  public function valid() {
76 2
    return ($this->current < $this->getMaxLimit());
77
  }
78
79
  /**
80
   * @return int
81
   */
82 2
  public function count() {
83 2
    return count($this->toArray());
84
  }
85
86
  /**
87
   * @return array
88
   */
89 2
  public function toArray() {
90 2
    $data = array();
91
92 2
    for ($this->rewind(); $this->valid(); $this->next()) {
93 2
      $data[] = $this->current();
94
    }
95
96 2
    return $data;
97
  }
98
99
  /**
100
   * @param int $max
101
   */
102 2
  public function setMaxLimit($max) {
103 2
    $this->max = $max;
104 2
  }
105
106
  /**
107
   * @return int
108
   */
109 2
  public function getMaxLimit() {
110 2
    return intval($this->max);
111
  }
112
113
}
114