Completed
Push — master ( a49478...ff8a6e )
by Pol
01:00 queued 10s
created

Fibonacci   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
cbo 1
dl 0
loc 96
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A __construct() 0 5 1
A next() 0 5 1
A rewind() 0 6 1
A valid() 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
use drupol\phpermutations\IteratorInterface;
7
8
/**
9
 * Class Fibonacci.
10
 */
11
class Fibonacci extends Combinatorics implements IteratorInterface
12
{
13
    /**
14
     * The maximum limit.
15
     *
16
     * @var int
17
     */
18
    protected $max;
19
20
    /**
21
     * The previous element.
22
     *
23
     * @var int
24
     */
25
    private $previous = 1;
26
27
    /**
28
     * The current element.
29
     *
30
     * @var int
31
     */
32
    private $current = 0;
33
34
    /**
35
     * The current key.
36
     *
37
     * @var int
38
     */
39
    private $key = 0;
40
41
    /**
42
     * Fibonacci constructor.
43
     */
44 2
    public function __construct()
45
    {
46 2
        $this->setMaxLimit(PHP_INT_MAX);
47 2
        parent::__construct([], null);
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 2
    public function next()
62
    {
63 2
        list($this->current, $this->previous) = [$this->current + $this->previous, $this->current];
64 2
        ++$this->key;
65 2
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    public function rewind()
71
    {
72 2
        $this->previous = 1;
73 2
        $this->current = 0;
74 2
        $this->key = 0;
75 2
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function valid()
81
    {
82 2
        return $this->current < $this->getMaxLimit();
83
    }
84
85
    /**
86
     * Set the maximum limit.
87
     *
88
     * @param int $max
89
     *                 The limit
90
     */
91 2
    public function setMaxLimit($max)
92
    {
93 2
        $this->max = $max;
94 2
    }
95
96
    /**
97
     * Get the maximum limit.
98
     *
99
     * @return int
100
     *             The limit
101
     */
102 2
    public function getMaxLimit()
103
    {
104 2
        return (int) $this->max;
105
    }
106
}
107