Passed
Push — master ( f7fc6f...76d32b )
by Fike
05:07 queued 02:23
created

Node::isEnumerable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\TreeAccess;
4
5
use AmaTeam\TreeAccess\API\NodeInterface;
6
7
class Node implements NodeInterface
8
{
9
    /**
10
     * @var string[]
11
     */
12
    private $path;
13
    /**
14
     * @var mixed
15
     */
16
    private $value;
17
    /**
18
     * @var bool
19
     */
20
    private $readable;
21
    /**
22
     * @var bool
23
     */
24
    private $writable;
25
26
    /**
27
     * @param string[] $path
28
     * @param mixed $value
29
     * @param bool $readable
30
     * @param bool $writable
31
     *
32
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
33
     */
34 35
    public function __construct(
35
        array $path,
36
        &$value,
37
        $readable = true,
38
        $writable = true
39
    ) {
40 35
        $this->path = $path;
41 35
        $this->value = &$value;
42 35
        $this->readable = $readable;
43 35
        $this->writable = $writable;
44 35
    }
45
46
    /**
47
     * @return string[]
48
     */
49 15
    public function getPath()
50
    {
51 15
        return $this->path;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 2
    public function getKey()
58
    {
59 2
        return empty($this->path) ? null : $this->path[sizeof($this->path) - 1];
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65 34
    public function &getValue()
66
    {
67 34
        return $this->value;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73 26
    public function isReadable()
74
    {
75 26
        return $this->readable;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 26
    public function isWritable()
82
    {
83 26
        return $this->writable;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function isEnumerable()
90
    {
91
        return is_array($this->value) || is_object($this->value);
92
    }
93
94
    /**
95
     * @param NodeInterface $node
96
     * @param string[] $path
97
     * @return Node
98
     */
99 15
    public static function withPath(NodeInterface $node, array $path)
100
    {
101 15
        return new Node(
102 15
            $path,
103 15
            $node->getValue(),
104 15
            $node->isReadable(),
105 15
            $node->isWritable()
106
        );
107
    }
108
}
109