Completed
Push — master ( 809c98...158972 )
by stéphane
02:21
created

TypesBuilder::buildReference()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dallgoot\Yaml;
4
5
use Dallgoot\Yaml\{Yaml as Y, Regex as R};
6
7
/**
8
 * Constructs the result (YamlObject or array) for specific Node Types
9
 *
10
 * @author  Stéphane Rebai <[email protected]>
11
 * @license Apache 2.0
12
 * @link    TODO : url to specific online doc
13
 */
14
final class TypesBuilder
15
{
16
17
    public static function buildReference($node, $parent)
18
    {
19
        $tmp = is_null($node->value) ? null : Builder::build($node->value, $parent);
20
        if ($node->type === Y::REF_DEF) Builder::$_root->addReference($node->identifier, $tmp);
21
        return Builder::$_root->getReference($node->identifier);
22
    }
23
24
25
    /**
26
     * Builds a key and set the property + value to the given parent
27
     *
28
     * @param Node $node       The node with type YAML::KEY
29
     * @param object|array $parent       The parent
30
     *
31
     * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed
32
     * @return null
33
     */
34
    public static function buildKey(Node $node, &$parent=null)
35
    {
36
        extract((array) $node, EXTR_REFS);
37
        if (is_null($identifier)) {
38
            throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $line));
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\TypesBuilder::ERROR_NO_KEYNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        } else {
40
            if ($value instanceof Node) {
41
                if ($value->type & (Y::ITEM|Y::KEY)) {
42
                    $value = new NodeList($value);
43
                } else {
44
                    $result = Builder::build($value);
45
                }
46
            }
47
            if ($value instanceof NodeList) {
48
                $result = Builder::buildNodeList($value);
49
            }
50
            if (is_null($parent)) {
51
                return $result;
52
            } else {
53
                if (is_array($parent)) {
54
                    $parent[$identifier] = $result;
55
                } else {
56
                    $parent->{$identifier} = $result;
57
                }
58
            }
59
        }
60
    }
61
62
    /**
63
     * Builds an item. Adds the item value to the parent array|Iterator
64
     *
65
     * @param      Node        $node    The node with type YAML::ITEM
66
     * @param      array|\Iterator      $parent  The parent
67
     *
68
     * @throws     \Exception  if parent is another type than array or object Iterator
69
     * @return null
70
     */
71
    public static function buildItem(Node $node, &$parent)
72
    {
73
        extract((array) $node, EXTR_REFS);
74
        if (!is_array($parent) && !($parent instanceof \ArrayIterator)) {
75
            throw new \Exception("parent must be an Iterable not ".(is_object($parent) ? get_class($parent) : gettype($parent)), 1);
76
        }
77
        $ref = $parent instanceof \ArrayIterator ? $parent->getArrayCopy() : $parent;
0 ignored issues
show
introduced by
$parent is never a sub-type of ArrayIterator.
Loading history...
78
        $numKeys = array_filter(array_keys($ref), 'is_int');
79
        $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0;
80
        if ($value instanceof Node) {
81
            if($value->type & Y::KEY) {
82
                self::buildKey($node->value, $parent);
83
                return;
84
            } elseif ($value->type & Y::ITEM) {
85
                $a = [];
86
                $result = self::buildItem($value, $a);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as self::buildItem($value, $a) targeting Dallgoot\Yaml\TypesBuilder::buildItem() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
87
            }
88
        }
89
        $result = Builder::build($value);
90
        $parent[$key] = $result;
91
    }
92
93
94
    /**
95
     * Builds a set key.
96
     *
97
     * @param      Node        $node    The node of type YAML::SET_KEY.
98
     * @param      object      $parent  The parent
99
     *
100
     * @throws     \Exception  if a problem occurs during serialisation (json format) of the key
101
     */
102
    public function buildSetKey(Node $node, &$parent)
103
    {
104
        $built = is_object($node->value) ? Builder::build($node->value) : null;
105
        $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built;
106
        $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
107
        if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1);
108
        $parent->{trim($key, '\'" ')} = null;
109
    }
110
111
    /**
112
     * Builds a set value.
113
     *
114
     * @param      Node    $node    The node of type YAML::SET_VALUE
115
     * @param      object  $parent  The parent (the document object or any previous object created through a mapping key)
116
     */
117
    public function buildSetValue(Node $node, &$parent)
118
    {
119
        $prop = array_keys(get_object_vars($parent));
120
        $key = end($prop);
121
        if ($node->value->type & (Y::ITEM|Y::KEY )) {
122
            $node->value = new NodeList($node->value);
123
        }
124
        $parent->{$key} = Builder::build($node->value);
0 ignored issues
show
Bug introduced by
It seems like $node->value can also be of type null and string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

124
        $parent->{$key} = Builder::build(/** @scrutinizer ignore-type */ $node->value);
Loading history...
125
    }
126
127
    /**
128
     * Builds a tag and its value (also built) and encapsulates them in a Tag object.
129
     *
130
     * @param      Node    $node    The node of type YAML::TAG
131
     * @param      mixed  $parent  The parent
132
     *
133
     * @return     Tag|null     The tag object of class Dallgoot\Yaml\Tag.
134
     * @todo       have somewhere a list of common tags and handle their treatment here
135
     */
136
    public static function buildTag(Node $node, &$parent)
137
    {
138
        $name = (string) $node->identifier;
139
        if ($parent === Builder::$_root && empty($node->value)) {
140
            $parent->addTag($name);
141
        } else {
142
            $target = $node->value;
143
            if ($node->value instanceof Node) {
144
                if ($node->value->type & (Y::KEY|Y::ITEM)) {
145
                    if (is_null($parent)) {
146
                        $target = new NodeList($node->value);
147
                    } else {
148
                        Builder::build($node->value, $parent);
149
                    }
150
                }
151
            }
152
            return new Tag($name, is_object($target) ? Builder::build($target) : null);
153
        }
154
    }
155
156
    /**
157
     * Builds a directive. NOT IMPLEMENTED YET
158
     *
159
     * @param      Node  $node    The node
160
     * @param      mixed  $parent  The parent
161
     * @todo implement if requested
162
     */
163
    public function buildDirective(Node $node, $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed. ( Ignorable by Annotation )

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

163
    public function buildDirective(Node $node, /** @scrutinizer ignore-unused */ $parent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $node is not used and could be removed. ( Ignorable by Annotation )

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

163
    public function buildDirective(/** @scrutinizer ignore-unused */ Node $node, $parent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        // TODO : implement
166
    }
167
}