Passed
Push — master ( 18bdd2...b79ef4 )
by Pol
04:33
created

Fibonacci   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 11
cbo 1
dl 0
loc 128
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A count() 0 4 1
A toArray() 0 10 2
A getMaxLimit() 0 4 1
A __construct() 0 4 1
A next() 0 5 1
A rewind() 0 6 1
A setMaxLimit() 0 4 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, \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
    {
47 2
        $this->setMaxLimit(PHP_INT_MAX);
48 2
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function current()
54
    {
55 2
        return $this->current;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function key()
62
    {
63
        return $this->key;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 2
    public function next()
70
    {
71 2
        list($this->current, $this->previous) = [$this->current + $this->previous, $this->current];
72 2
        $this->key++;
73 2
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function rewind()
79
    {
80 2
        $this->previous = 1;
81 2
        $this->current = 0;
82 2
        $this->key = 0;
83 2
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 2
    public function valid()
89
    {
90 2
        return ($this->current < $this->getMaxLimit());
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 2
    public function count()
97
    {
98 2
        return count($this->toArray());
99
    }
100
101
    /**
102
     * Convert the iterator into an array.
103
     *
104
     * @return array
105
     *   The elements.
106
     */
107 2
    public function toArray()
108
    {
109 2
        $data = [];
110
111 2
        for ($this->rewind(); $this->valid(); $this->next()) {
112 2
            $data[] = $this->current();
113 2
        }
114
115 2
        return $data;
116
    }
117
118
    /**
119
     * Set the maximum limit.
120
     *
121
     * @param int $max
122
     *   The limit.
123
     */
124 2
    public function setMaxLimit($max)
125
    {
126 2
        $this->max = $max;
127 2
    }
128
129
    /**
130
     * Get the maximum limit.
131
     *
132
     * @return int
133
     *   The limit.
134
     */
135 2
    public function getMaxLimit()
136
    {
137 2
        return intval($this->max);
138
    }
139
}
140