NodeCollection   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 59
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodes() 0 10 1
A addNode() 0 6 1
C atPosition() 0 26 13
A search() 0 9 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Table;
4
5
use Knp\FriendlyContexts\Table\Node;
6
7
class NodeCollection
8
{
9
    private $nodes = [];
10
11
    public function getNodes()
12
    {
13
        return array_reduce(
14
            $this->nodes,
15
            function ($previous, $line) {
16
                return array_merge($previous, $line);
17
            },
18
            []
19
        );
20
    }
21
22
    public function addNode(Node $node, $line, $column)
23
    {
24
        $this->nodes[$line][$column] = $node;
25
26
        return $this;
27
    }
28
29
    public function atPosition($line = null, $column = null)
30
    {
31
        switch (true) {
32
            case null === $line && null === $column:
33
                return $this->getNodes();
34
            case null !== $line && null === $column:
35
                return array_key_exists($line, $this->nodes)
36
                    ? $this->nodes[$line]
37
                    : []
38
                ;
39
            case null === $line && null !== $column:
40
                $nodes = [];
41
                foreach ($this->nodes as $i => $nodes) {
42
                    if (array_key_exists($column, $nodes)) {
43
                        $nodes[$i] = $nodes[$column];
44
                    }
45
                }
46
                return $nodes;
47
            case null !== $line && null !== $column:
48
                $nodes = $this->atPosition($line);
49
                return array_key_exists($column, $nodes)
50
                    ? $nodes[$column]
51
                    : null
52
                ;
53
        }
54
    }
55
56
    public function search($content)
57
    {
58
        return array_filter(
59
            $this->getNodes(),
60
            function ($e) use ($content) {
61
                return $e->getContent() === $content;
62
            }
63
        );
64
    }
65
}
66