Completed
Push — master ( c45521...844759 )
by stéphane
08:22
created

NodeSetKey::build()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 16
nop 1
dl 0
loc 7
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml;
4
5
/**
6
 *
7
 * @author  Stéphane Rebai <[email protected]>
8
 * @license Apache 2.0
9
 * @link    TODO : url to specific online doc
10
 */
11
class NodeSetKey extends Node
12
{
13
    public function __construct(string $nodeString, int $line)
14
    {
15
        $v = trim(substr($nodeString, 1));
16
        if (!empty($v)) {
17
            // $node->value = new NodeList(new Node($v, $node->line));
18
            $this->value = NodeFactory::get($v, $line);
19
        }
20
    }
21
22
    /**
23
     * Builds a set key.
24
     *
25
     * @param object $parent The parent
26
     *
27
     * @throws \Exception  if a problem occurs during serialisation (json format) of the key
28
     */
29
    public function build(&$parent = null)
30
    {
31
        $built = is_object($this->value) ? $this->value->build($parent) : null;
0 ignored issues
show
Bug introduced by
The method build() does not exist on Dallgoot\Yaml\NodeList. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        $built = is_object($this->value) ? $this->value->/** @scrutinizer ignore-call */ build($parent) : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
        $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built;
33
        $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
34
        if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($this->value, true));
35
        $parent->{trim($key, '\'" ')} = null;
36
    }
37
38
}