CircularArray   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 44
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 10 2
A fromArray() 0 10 2
A toArray() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the PHP Snippets - Circular Array.
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