Passed
Push — master ( 1e7555...a49478 )
by Pol
03:09
created

Fibonacci   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 93.33%

Importance

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

10 Methods

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