Passed
Push — master ( a2fa16...8a4497 )
by Pol
11:27
created

Fibonacci   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 77
ccs 17
cts 17
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 3 1
A next() 0 4 1
A getMaxLimit() 0 3 1
A rewind() 0 5 1
A __construct() 0 4 1
A setMaxLimit() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\phpermutations\Iterators;
6
7
use drupol\phpermutations\Iterators;
8
9
use const PHP_INT_MAX;
10
11
/**
12
 * Class Fibonacci.
13
 */
14
class Fibonacci extends Iterators
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $current;
20
21
    /**
22
     * The maximum limit.
23
     *
24
     * @var int
25
     */
26
    protected $max;
27
28
    /**
29
     * The previous element.
30
     *
31
     * @var int
32
     */
33
    private $previous = 1;
34
35
    /**
36
     * Fibonacci constructor.
37
     */
38
    public function __construct()
39
    {
40
        $this->setMaxLimit(PHP_INT_MAX);
41
        parent::__construct();
42
    }
43
44 2
    /**
45
     * Get the maximum limit.
46 2
     *
47 2
     * @return int
48 2
     *             The limit
49
     */
50
    public function getMaxLimit()
51
    {
52
        return (int) $this->max;
53 2
    }
54
55 2
    /**
56
     * {@inheritdoc}
57
     */
58
    public function next()
59
    {
60
        [$this->current, $this->previous] = [$this->current + $this->previous, $this->current];
61 2
        ++$this->key;
62
    }
63 2
64 2
    /**
65 2
     * {@inheritdoc}
66
     */
67
    public function rewind()
68
    {
69
        $this->previous = 1;
70 2
        $this->current = 0;
71
        $this->key = 0;
72 2
    }
73 2
74 2
    /**
75 2
     * Set the maximum limit.
76
     *
77
     * @param int $max
78
     *                 The limit
79
     */
80 2
    public function setMaxLimit($max)
81
    {
82 2
        $this->max = $max;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function valid()
89
    {
90
        return $this->getMaxLimit() > $this->current;
91 2
    }
92
}
93