SetKey   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 14
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isAwaitingChild() 0 3 1
A build() 0 8 5
A __construct() 0 8 2
1
<?php
2
3
namespace Dallgoot\Yaml\Nodes;
4
5
use Dallgoot\Yaml\NodeFactory;
6
use Dallgoot\Yaml\Regex;
7
use Dallgoot\Yaml\Nodes\Generic\NodeGeneric;
8
9
/**
10
 *
11
 * @author  Stéphane Rebai <[email protected]>
12
 * @license Apache 2.0
13
 * @link    https://github.com/dallgoot/yaml
14
 */
15
class SetKey extends NodeGeneric
16
{
17 3
    public function __construct(string $nodeString, int $line)
18
    {
19 3
        parent::__construct($nodeString, $line);
20 3
        $v = substr(trim($nodeString), 1);
21 3
        if (!empty($v)) {
22 3
            $value = NodeFactory::get($v, $line);
23 3
            $value->indent = null;
24 3
            $this->add($value);
25
        }
26
    }
27
28
    /**
29
     * @param object $parent The parent
30
     *
31
     * @throws \Exception  if a problem occurs during serialisation (json format) of the key
32
     */
33 1
    public function build(&$parent = null)
34
    {
35 1
        $built = is_null($this->value) ? null : $this->value->build();
36 1
        $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built;
37 1
        $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
38 1
        if (empty($key)) throw new \Exception("Cant serialize complex key: " . var_export($this->value, true));
39 1
        $parent->{trim($key, '\'" ')} = null;
40 1
        return null;
41
    }
42
43 1
    public function isAwaitingChild(NodeGeneric $child): bool
44
    {
45 1
        return is_null($this->value);
46
    }
47
}
48