Completed
Pull Request — master (#28)
by Tom
02:28
created

ConfigIterator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 82
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A current() 0 4 1
A key() 0 4 1
A next() 0 11 2
A rewind() 0 11 2
A valid() 0 16 3
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use Iterator;
6
use RecursiveArrayIterator;
7
8
final class ConfigIterator implements Iterator
9
{
10
    /**
11
     * @var string[]
12
     */
13
    private $path = [];
14
15
    /**
16
     * @var RecursiveArrayIterator
17
     */
18
    private $stack = [];
19
20
    /**
21
     * @var RecursiveArrayIterator
22
     */
23
    private $current;
24
25
    /**
26
     * @var string
27
     */
28
    private $separator;
29
30
    /**
31
     * @param Config $config
32
     */
33
    public function __construct(Config $config)
34
    {
35
        $this->separator = $config->getSeparator();
36
        $this->current   = new RecursiveArrayIterator($config->asArray());
37
    }
38
39
    public function current()
40
    {
41
        return $this->current->current();
42
    }
43
44
    public function key()
45
    {
46
        return implode($this->separator, array_merge($this->path, [$this->current->key()]));
47
    }
48
49
    public function next()
50
    {
51
        if ($this->current->hasChildren()) {
52
            $it = &$this->current;
53
            array_push($this->stack, $it);
54
            array_push($this->path, $it->key());
55
            $this->current = $it->getChildren();
56
        } else {
57
            $this->current->next();
58
        }
59
    }
60
61
    public function rewind()
62
    {
63
        if (!empty($this->stack)) {
64
            $this->current = array_shift($this->stack);
65
        }
66
67
        $this->stack = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<RecursiveArrayIterator> of property $stack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
        $this->path = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
70
        $this->current->rewind();
71
    }
72
73
    public function valid()
74
    {
75
        if ($this->current->valid()) {
76
            return true;
77
        }
78
79
        if (empty($this->stack)) {
80
            return false;
81
        }
82
83
        array_pop($this->path);
84
        $this->current = array_pop($this->stack);
85
        $this->current->next();
86
87
        return $this->valid();
88
    }
89
}
90