Completed
Push — master ( 86484b...f97b0a )
by stéphane
05:27
created

TagNamespaceLocal::setHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
namespace Dallgoot\Yaml;
3
4
use \ReflectionMethod as RM;
5
/**
6
 * Provides mechanisms to handle tags
7
 * - registering tags and their handler methods
8
 * - returning transformed values according to Node type or NodeList
9
 *
10
 * Note: currently supports ONLY local tags
11
 *
12
 * @author  Stéphane Rebai <[email protected]>
13
 * @license Apache 2.0
14
 * @link    https://github.com/dallgoot/yaml
15
 *
16
 * @todo move legacy tags handlers in a specific class : checking affecting methods to tags mechanisms when theres a global tag
17
 */
18
class TagNamespaceLocal implements TagNamespaceInterface
0 ignored issues
show
Bug introduced by
The type Dallgoot\Yaml\TagNamespaceInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
{
20
    public const LEGACY_TAGS_HANDLERS = ['!!str'       => 'strHandler',
21
                                          '!!binary'    => 'binaryHandler',
22
                                          '!set'        => 'setHandler',
23
                                          '!!omap'      => 'omapHandler',
24
                                          '!php/object' => 'symfonyPHPobjectHandler',
25
                                          '!inline'     => 'inlineHandler',
26
                                      ];
27
28
29
    /**
30
     * Specific Handler for Symfony custom tag : 'php/object'
31
     *
32
     * @param object             $node   The node
33
     * @param object|array|null  $parent The parent
34
     *
35
     * @throws Exception if unserialize fails OR if its a NodeList (no support of multiple values for this tag)
36
     * @return object    the unserialized object according to Node value
37
     */
38
    public final static function symfonyPHPobjectHandler(object $node, &$parent = null)
39
    {
40
        if ($node instanceof NodeScalar) {
41
            $phpObject = unserialize($node->raw);
42
            // NOTE : we assume this is only used for Object types (if a boolean false is serialized this will FAIL)
43
            if (is_bool($phpObject)) {
44
                throw new \Exception("value for tag 'php/object' could NOT be unserialized");
45
            }
46
            return $phpObject;
47
        } elseif ($node instanceof NodeList) {
48
            throw new \Exception("tag 'php/object' can NOT be a NodeList");
49
        }
50
    }
51
52
    /**
53
     * Specific handler for 'inline' tag
54
     *
55
     * @param object $node
56
     * @param object|array|null  $parent The parent
57
     *
58
     * @todo implements
59
     */
60
    public final static function inlineHandler(object $node, object &$parent = null)
61
    {
62
        return self::strHandler($node, $parent);
63
    }
64
65
    /**
66
     * Specific Handler for 'str' tag
67
     *
68
     * @param object $node    The Node or NodeList
69
     * @param object|array|null  $parent The parent
70
     *
71
     * @return string the value of Node converted to string if needed
72
     */
73
    public final static function strHandler(object $node, object &$parent = null)
74
    {
75
        if ($node instanceof Node) {
76
            // if ($node instanceof NodeKey) {
77
            //     return $node;
78
            // }
79
            $value = trim($node->raw);
80
            if ($node instanceof NodeQuoted) {
81
                $value = $node->build();
82
            }
83
            // return new NodeQuoted("'".$value.'"', $node->line);
84
            return $value;
85
        } elseif ($node instanceof NodeList) {
86
            $list = [];
87
            foreach ($node as $key => $child) {
88
                // $list[] = self::strHandler($child)->raw;
89
                $list[] = self::strHandler($child);
90
            }
91
            return new NodeScalar(implode('',$list), 0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Dallgoot\Yaml...(implode('', $list), 0) returns the type Dallgoot\Yaml\NodeScalar which is incompatible with the documented return type string.
Loading history...
92
        }
93
    }
94
95
    /**
96
     * Specific Handler for 'binary' tag
97
     *
98
     * @param object $node   The node or NodeList
99
     * @param object|array|null  $parent The parent
100
     *
101
     * @return string  The value considered as 'binary' Note: the difference with strHandler is that multiline have not separation
102
     */
103
    public final static function binaryHandler($node, Node &$parent = null)
104
    {
105
        return self::strHandler($node, $parent);
106
    }
107
108
    /**
109
     * Specific Handler for the '!set' tag
110
     *
111
     * @param      object     $node    The node
112
     * @param object|array|null  $parent The parent
113
     *
114
     * @throws     \Exception  if theres a set but no children (set keys or set values)
115
     * @return     YamlObject|object  process the Set, ie. an object construction with properties as serialized JSON values
116
     */
117
    public final static function setHandler(object $node, Node &$parent = null)
118
    {
119
        if (!($node instanceof NodeList)) {
120
            throw new \Exception("tag '!!set' can NOT be a single Node");
121
        } else {
122
            ///no actions needed for now
123
        }
124
    }
125
126
    /**
127
     * Specifi Handler for the 'omap' tag
128
     *
129
     * @param object $node   The node
130
     * @param object|array|null  $parent The parent
131
     *
132
     * @throws \Exception  if theres an omap but no map items
133
     * @return YamlObject|array process the omap
134
     */
135
    public final static function omapHandler(object $node, Node &$parent = null)
136
    {
137
        if ($node instanceof Node) {
138
            if ($node instanceof NodeItem) {
139
                return self::omapHandler($node->value);
0 ignored issues
show
Bug introduced by
It seems like $node->value can also be of type null; however, parameter $node of Dallgoot\Yaml\TagNamespaceLocal::omapHandler() 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

139
                return self::omapHandler(/** @scrutinizer ignore-type */ $node->value);
Loading history...
140
            } elseif ($node instanceof NodeKey) {
141
                return $node;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $node returns the type Dallgoot\Yaml\NodeKey which is incompatible with the documented return type Dallgoot\Yaml\YamlObject|array.
Loading history...
142
            } else {
143
                throw new \Exception("tag '!!omap' MUST have items _with_ a key");
144
            }
145
        } elseif ($node instanceof NodeList) {
146
            //verify that each child is an item with a key as child
147
            $list = new NodeList();
148
            foreach ($node as $key => $item) {
149
                $list->push(self::omaphandler($item));
150
            }
151
            return $list;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $list returns the type Dallgoot\Yaml\NodeList which is incompatible with the documented return type Dallgoot\Yaml\YamlObject|array.
Loading history...
152
        }
153
    }
154
155
}
156