Iterator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 3
b 0
f 0
dl 0
loc 87
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A rewind() 0 3 1
A current() 0 3 1
A valid() 0 3 1
A __construct() 0 4 1
A next() 0 3 1
1
<?php
2
3
/**
4
 * chdemko\BitArray\Iterator class
5
 *
6
 * @author    Christophe Demko <[email protected]>
7
 * @copyright Copyright (C) 2012-2024 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-bitarray package https://github.com/chdemko/php-bitarray
12
 */
13
14
// Declare chdemko\BitArray namespace
15
namespace chdemko\BitArray;
16
17
/**
18
 * Iterator
19
 *
20
 * @package BitArray
21
 *
22
 * @since 1.0.0
23
 */
24
class Iterator implements \Iterator
25
{
26
    /**
27
     * @var integer  Index
28
     *
29
     * @since 1.0.0
30
     */
31
    private $index;
32
33
    /**
34
     * @var BitArray  bits
35
     *
36
     * @since 1.0.0
37
     */
38
    private $bits;
39
40
    /**
41
     * Constructor
42
     *
43
     * @param BitArray $bits BitArray
44
     *
45
     * @since 1.0.0
46
     */
47
    public function __construct(BitArray $bits)
48
    {
49
        $this->bits = $bits;
50
        $this->rewind();
51
    }
52
53
    /**
54
     * Rewind the Iterator to the first element
55
     *
56
     * @return void
57
     *
58
     * @since 1.0.0
59
     */
60
    public function rewind(): void
61
    {
62
        $this->index = 0;
63
    }
64
65
    /**
66
     * Return the current key
67
     *
68
     * @return mixed  The current key
69
     *
70
     * @since 1.0.0
71
     */
72
    public function key(): mixed
73
    {
74
        return $this->index;
75
    }
76
77
    /**
78
     * Return the current value
79
     *
80
     * @return mixed  The current value
81
     *
82
     * @since 1.0.0
83
     */
84
    public function current(): mixed
85
    {
86
        return $this->bits[$this->index];
87
    }
88
89
    /**
90
     * Move forward to the next element
91
     *
92
     * @return void
93
     *
94
     * @since 1.0.0
95
     */
96
    public function next(): void
97
    {
98
        $this->index++;
99
    }
100
101
    /**
102
     * Checks if current position is valid
103
     *
104
     * @return boolean
105
     *
106
     * @since 1.0.0
107
     */
108
    public function valid(): bool
109
    {
110
        return $this->index < $this->bits->size;
111
    }
112
}
113