NodeCollection   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 27
c 1
b 0
f 0
dl 0
loc 143
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getNode() 0 8 3
A add() 0 6 2
A hasNode() 0 8 3
A next() 0 3 1
A __toString() 0 9 2
A rewind() 0 3 1
A count() 0 3 1
A removeNode() 0 6 3
A key() 0 3 1
A valid() 0 3 1
A current() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Fixer\NestingConsistency;
5
6
class NodeCollection implements \Iterator, \Countable
7
{
8
    /**
9
     * @var Node[]
10
     */
11
    private $nodes = [];
12
13
    /**
14
     * @param Node $node
15
     */
16
    public function add(Node $node): void
17
    {
18
        if ($this->hasNode($node->identifier())) {
19
            $this->removeNode($node->identifier());
20
        }
21
        $this->nodes[] = $node;
22
    }
23
24
    /**
25
     * Return the current element
26
     * @link  https://php.net/manual/en/iterator.current.php
27
     * @return mixed Can return any type.
28
     * @since 5.0.0
29
     */
30
    public function current(): Node
31
    {
32
        return current($this->nodes);
33
    }
34
35
    /**
36
     * Move forward to next element
37
     * @link  https://php.net/manual/en/iterator.next.php
38
     * @return void Any returned value is ignored.
39
     * @since 5.0.0
40
     */
41
    public function next(): void
42
    {
43
        next($this->nodes);
44
    }
45
46
    /**
47
     * Return the key of the current element
48
     * @link  https://php.net/manual/en/iterator.key.php
49
     * @return mixed scalar on success, or null on failure.
50
     * @since 5.0.0
51
     */
52
    public function key(): int
53
    {
54
        return key($this->nodes);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key($this->nodes) could return the type null|string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
55
    }
56
57
    /**
58
     * Checks if current position is valid
59
     * @link  https://php.net/manual/en/iterator.valid.php
60
     * @return bool The return value will be casted to boolean and then evaluated.
61
     * Returns true on success or false on failure.
62
     * @since 5.0.0
63
     */
64
    public function valid(): bool
65
    {
66
        return key($this->nodes) !== null;
67
    }
68
69
    /**
70
     * Rewind the Iterator to the first element
71
     * @link  https://php.net/manual/en/iterator.rewind.php
72
     * @return void Any returned value is ignored.
73
     * @since 5.0.0
74
     */
75
    public function rewind(): void
76
    {
77
        reset($this->nodes);
78
    }
79
80
    /**
81
     * Count elements of an object
82
     * @link  https://php.net/manual/en/countable.count.php
83
     * @return int The custom count as an integer.
84
     * </p>
85
     * <p>
86
     * The return value is cast to an integer.
87
     * @since 5.1.0
88
     */
89
    public function count(): int
90
    {
91
        return count($this->nodes);
92
    }
93
94
    /**
95
     * @param string $string
96
     *
97
     * @return bool
98
     */
99
    public function hasNode(string $string): bool
100
    {
101
        foreach ($this->nodes as $node) {
102
            if ($node->identifier() === $string) {
103
                return true;
104
            }
105
        }
106
        return false;
107
    }
108
109
    /**
110
     * @param string $string
111
     *
112
     * @return Node|null
113
     */
114
    public function getNode(string $string): ?Node
115
    {
116
        foreach ($this->nodes as $node) {
117
            if ($node->identifier() === $string) {
118
                return $node;
119
            }
120
        }
121
        return null;
122
    }
123
124
    /**
125
     * @param string $title
126
     */
127
    private function removeNode(string $title): void
128
    {
129
        foreach ($this->nodes as $key => $node) {
130
            if ($node->identifier() === $title) {
131
                unset($this->nodes[$key]);
132
                $this->nodes = array_values($this->nodes);
133
            }
134
        }
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function __toString(): string
141
    {
142
        $string = '';
143
144
        foreach ($this->nodes as $node) {
145
            $string .= $node;
146
        }
147
148
        return $string;
149
    }
150
}
151