Passed
Push — master ( f8295f...487b9a )
by stéphane
02:46
created

Key::isAwaitingChild()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.3752

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 13
nop 1
dl 0
loc 26
ccs 15
cts 18
cp 0.8333
crap 9.3752
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml\NodeFactory;
6
use Dallgoot\Yaml\Regex;
7
8
/**
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    https://github.com/dallgoot/yaml
13
 */
14
class Key extends NodeGeneric
15
{
16
    const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d";
17
18 9
    public function __construct(string $nodeString, int $line, array $matches = null)
19
    {
20 9
        parent::__construct($nodeString, $line);
21 9
        if (is_null($matches)) {
22 9
            if (!((bool) preg_match(Regex::KEY, ltrim($nodeString), $matches))) {
23 1
                throw new \ParseError("Not a KEY:VALUE syntax ($nodeString)", 1);
24
            }
25
        }
26 9
        $this->setIdentifier($matches[1]);
27
28 9
        $value = isset($matches[2]) ? trim($matches[2]) : null;
29 9
        if (!empty($value)) {
30 9
            $child = NodeFactory::get($value, $line);
31 9
            $child->indent = null;
32 9
            $this->add($child);
33
        }
34 9
    }
35
36 9
    public function setIdentifier(string $keyString)
37
    {
38 9
        if ($keyString === '') {
39 1
           throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $this->line));
40
        } else {
41 9
            $node = NodeFactory::get($keyString);
42 9
            if ($node->isOneOf('Tag', 'Quoted')) {
43 2
                $built = $node->build();
44 2
                if (is_object($built)) {
45
                    $this->identifier = $built->value;
46
                } else {
47 2
                    $this->identifier = (string) $node->build();
48
                }
49 9
            } elseif ($node instanceof Scalar) {
50 9
                $this->identifier = trim($node->raw);
51
            }
52
        }
53 9
    }
54
55 9
    public function add(NodeGeneric $child):NodeGeneric
56
    {
57 9
        if ($this->value instanceof NodeGeneric && $this->value->isOneOf('Literal','LiteralFolded', 'Anchor')) {
58 1
            return $this->value->add($child);
59
        } else {
60 9
            return parent::add($child);
61
        }
62
    }
63
64 1
    public function getTargetOnEqualIndent(NodeGeneric &$node):NodeGeneric
65
    {
66 1
        if ($node instanceof Item) {
67 1
            return $this;
68
        }
69 1
        return $this->getParent();
70
    }
71
72 1
    public function getTargetOnMoreIndent(NodeGeneric &$node):NodeGeneric
73
    {
74 1
        if (!is_null($this->value)) {
75 1
            if ($this->getDeepestNode()->isAwaitingChild($node)) {
76 1
                return $this->getDeepestNode();
77
            }
78
        }
79 1
        return $this;
80
    }
81
82
83 1
    public function isAwaitingChild(NodeGeneric $node):bool
84
    {
85 1
        if (is_null($this->value) || $node instanceof Comment) {
86
            return true;
87 1
        } elseif($this->value instanceof NodeGeneric) {
88 1
            $current = $this->value;
89
        } else {
90
            $current = $this->value->current();
91
        }
92 1
        if ($current instanceof Comment) {
93 1
            return true;
94
        }
95 1
        if ($current instanceof Scalar) {
96 1
            return $node->isOneOf('Scalar', 'Blank');
97
        }
98 1
        if ($current->isOneOf('Key', 'Item')) {
99 1
            $cClass = get_class($current);
100 1
            return $node instanceof $cClass;
101
        }
102 1
        if ($current instanceof Literals) {
103 1
            return $node->indent > $this->indent;
104
        }
105 1
        if ($current instanceof Anchor) {
106 1
            return $current->isAwaitingChild($node);
107
        }
108
        return false;
109
    }
110
111
    /**
112
     * Builds a key and set the property + value to the given parent
113
     *
114
     * @param object|array $parent The parent
115
     *
116
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
117
     * @return null|\StdClass
118
     */
119 1
    public function build(&$parent = null)
120
    {
121
        // var_dump("DEBUG KEY:".$this->identifier);
122 1
        if ($this->value instanceof Anchor) {
123
            $result = &$this->value->build();
124
        } else {
125 1
            $result = is_null($this->value) ? null : $this->value->build();
126
        }
127 1
        if (is_null($parent)) {
128 1
            $parent = new \StdClass;
129 1
            $parent->{$this->identifier} = &$result;
130 1
            return $parent;
131
        } else {
132 1
            $parent->{$this->identifier} = &$result;
133
        }
134
    }
135
}