Passed
Push — master ( 2cad2a...85c281 )
by Webysther
02:12
created

CircularArray::next()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Packagist Mirror.
5
 *
6
 * For the full license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
namespace PHPSnippets\DataStructures;
11
12
use SplFixedArray;
13
14
/**
15
 * Circular Array (implies to be fixed).
16
 *
17
 * @author Webysther Nunes <[email protected]>
18
 */
19
class CircularArray extends SplFixedArray
20
{
21
    /**
22
     * Rewind if goes to last items
23
     */
24 1
    public function next()
25
    {
26 1
        if ($this->key() + 1 == $this->count()) {
27 1
            $this->rewind();
28
29 1
            return;
30
        }
31
32 1
        parent::next();
33 1
    }
34
35
    /**
36
     * Convert a simple array to fixed circular array
37
     *
38
     * @param  array   $array
39
     * @param  boolean $save_indexes
40
     * @return FixedCircularArray
41
     */
42 2
    public static function fromArray($array, $save_indexes = true)
43
    {
44 2
        $circular = new self(count($array));
45
46 2
        foreach ($array as $key => $value) {
47 2
            $circular[$key] = $value;
48
        }
49
50 2
        return $circular;
51
    }
52
53
    /**
54
     * Convert to simple array
55
     *
56
     * @return array
57
     */
58 1
    public function toArray()
59
    {
60 1
        return parent::toArray();
61
    }
62
}
63