Passed
Push — master ( 65ca73...10de3a )
by Pol
02:10
created

Fibonacci   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

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

10 Methods

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