NodeCollection::atPosition()   C
last analyzed

Complexity

Conditions 13
Paths 9

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 18
nc 9
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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