Passed
Push — master ( 7a981c...2773c2 )
by stéphane
04:34
created

Node::getShortMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Dallgoot\Yaml;
4
5
use Dallgoot\Yaml as Y;
6
use Dallgoot\Yaml\Regex as R;
7
8
/**
9
 * Class for node.
10
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
11
final class Node
12
{
13
    /** @var int */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
14
    public $indent = -1;
15
    /** @var int */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
16
    public $line;
17
    /** @var int */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
18
    public $type;
19
    /** @var null|string|boolean */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
20
    public $identifier;
21
    /** @var Node|NodeList|null|string */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    public $value;
23
24
    /** @var null|Node */
2 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
26
27
    public function __construct($nodeString = null, $line = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
28
    {
29
        $this->line = $line;
30
        if (is_null($nodeString)) {
31
            $this->type = Y\ROOT;
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
        } else {
33
            $this->parse($nodeString);
34
        }
35
    }
36
37
    /**
38
     * Sets the parent of the current Node
39
     * @param      Node       $node   The node
3 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
40
     *
41
     * @return     Node|self  The currentNode
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
42
     */
43
    public function setParent(Node $node):Node
44
    {
45
        $this->parent = $node;
46
        return $this;
47
    }
48
49
    /**
50
     * Gets the ancestor with specified $indent or the direct $parent OR the current Node itself
51
     *
52
     * @param      integer    $indent  The indent
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
53
     *
54
     * @return     Node|self  The parent.
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
55
     */
56
    public function getParent(int $indent = null):Node
57
    {
58
        if (!is_int($indent)) return $this->parent ?? $this;
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
59
        $cursor = $this;
60
        while ($cursor instanceof Node && $cursor->indent >= $indent) {
61
            $cursor = $cursor->parent;
62
        }
63
        return $cursor;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cursor could return the type null which is incompatible with the type-hinted return Dallgoot\Yaml\Node. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
66
    /**
67
     * Set the value for the current Node :
68
     * - if value is null , then value = $child (Node)
69
     * - if value is Node, then value is a NodeList with (previous value AND $child)
70
     * - if value is a NodeList, simply push $child into
71
     *
72
     * @param      Node  $child  The child
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
73
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
74
    public function add(Node $child):void
75
    {
76
        $child->setParent($this);
77
        $current = $this->value;
78
        if (is_null($current)) {
79
            $this->value = $child;
80
            return;
81
        } else {
82
            if ($current instanceof Node) {
83
                $this->value = new NodeList();
84
                $this->value->type = Y\LITT_FOLDED;
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT_FOLDED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The property type does not seem to exist on Dallgoot\Yaml\NodeList.
Loading history...
85
                $this->value->setIteratorMode(NodeList::IT_MODE_KEEP);
86
                $this->value->push($current);
87
            }
88
            $this->value->push($child);
0 ignored issues
show
Bug introduced by
The method push() does not exist on null. ( Ignorable by Annotation )

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

88
            $this->value->/** @scrutinizer ignore-call */ 
89
                          push($child);

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...
Bug introduced by
The method push() does not exist on Dallgoot\Yaml\Node. ( Ignorable by Annotation )

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

88
            $this->value->/** @scrutinizer ignore-call */ 
89
                          push($child);

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...
89
            //modify type according to child
90
            switch ($child->type) {
91
                case Y\COMMENT: //fall through
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
92
                case Y\KEY:     $this->value->type = Y\MAPPING;break;
2 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
93
                case Y\ITEM:    $this->value->type = Y\SEQUENCE;break;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ITEM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
94
                // case Y\COMMENT: $this->value->type = Y\RAW;break;
95
            }
96
        }
97
        // if ($this->type & Y\LITTERALS) $child->type = Y\SCALAR;
98
    }
99
100
    public function getDeepestNode():Node
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
101
    {
102
        $cursor = $this;
103
        while ($cursor->value instanceof Node) {
104
            $cursor = $cursor->value;
105
        }
106
        return $cursor;
107
    }
108
109
    /**
110
     * Parses the string (assumed to be a line from a valid YAML)
111
     *
112
     * @param      string     $nodeString  The node string
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 5 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
113
     *
114
     * @return     Node|self  ( description_of_the_return_value )
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
115
     */
116
    public function parse(string $nodeString):Node
117
    {
118
        $nodeValue = preg_replace("/^\t+/m", " ", $nodeString);//permissive to tabs but replacement
119
        $this->indent = strspn($nodeValue, ' ');
120
        $nodeValue = ltrim($nodeValue);
121
        if ($nodeValue === '') {
122
            $this->type = Y\BLANK;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\BLANK was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
123
            // $this->indent = 0; // remove if no bugs
124
        } elseif (substr($nodeValue, 0, 3) === '...') {//TODO: can have something on same line ?
125
            $this->type = Y\DOC_END;
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\DOC_END was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
126
        } elseif (preg_match(R::KEY, $nodeValue, $matches)) {
127
            $this->onKey($matches);
128
        } else {//NOTE: can be of another type according to parent
129
            list($this->type, $value) = $this->define($nodeValue);
130
            is_object($value) ? $this->add($value) : $this->value = $value;
131
        }
132
        return $this;
133
    }
134
135
    /**
136
     *  Set the type and value according to first character
137
     *
138
     * @param      string  $nodeValue  The node value
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 6
Loading history...
139
     * @return     array   contains [node->type, node->value]
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
140
     */
141
    private function define($nodeValue):array
0 ignored issues
show
Coding Style introduced by
Private method name "Node::define" must be prefixed with an underscore
Loading history...
142
    {
143
        $v = substr($nodeValue, 1);
144
        $first = $nodeValue[0];
145
        if (in_array($first, ['"', "'"])) {
146
            $type = R::isProperlyQuoted($nodeValue) ? Y\QUOTED : Y\PARTIAL;
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\QUOTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
147
            return [$type, $nodeValue];
148
        }
149
        if (in_array($first, ['{', '[']))      return $this->onObject($nodeValue);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
150
        if (in_array($first, ['!', '&', '*'])) return $this->onNodeAction($nodeValue);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
151
        switch ($first) {
152
            case '#': return [Y\COMMENT, ltrim($v)];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
153
            case "-": return $this->onHyphen($nodeValue);
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
154
            case '%': return [Y\DIRECTIVE, ltrim($v)];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\DIRECTIVE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
155
            case '?': return [Y\SET_KEY,   empty($v) ? null : new Node(ltrim($v), $this->line)];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SET_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
156
            case ':': return [Y\SET_VALUE, empty($v) ? null : new Node(ltrim($v), $this->line)];
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\SET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
157
            case '>': return [Y\LITT_FOLDED, null];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT_FOLDED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
158
            case '|': return [Y\LITT, null];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
159
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
160
                return [Y\SCALAR, $nodeValue];
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SCALAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
161
        }
162
    }
163
164
    /**
165
     * Process when a "key: value" syntax is found in the parsed string
166
     * Note : key is match 1, value is match 2 as per regex from R::KEY
167
     * @param      array  $matches  The matches provided by 'preg_match' function
3 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
168
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
169
    private function onKey(array $matches):void
0 ignored issues
show
Coding Style introduced by
Private method name "Node::onKey" must be prefixed with an underscore
Loading history...
170
    {
171
        $this->type = Y\KEY;
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
172
        $this->identifier = trim($matches[1]);
173
        $keyValue = isset($matches[2]) ? trim($matches[2]) : null;
174
        if (!empty($keyValue)) {
175
            $n = new Node($keyValue, $this->line);
176
            $hasComment = strpos($keyValue, ' #');
177
            if (!is_bool($hasComment)) {
0 ignored issues
show
introduced by
The condition is_bool($hasComment) is always false.
Loading history...
178
                $tmpNode = new Node(trim(substr($keyValue, 0, $hasComment)), $this->line);
179
                if ($tmpNode->type !== Y\PARTIAL) {
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
180
                    $comment = new Node(trim(substr($keyValue, $hasComment+1)), $this->line);
181
                    //TODO: modify "identifier" to specify if fullline comment or not
182
                    $this->add($comment);
183
                    $n = $tmpNode;
184
                }
185
            }
186
            $n->indent = $this->indent + strlen($this->identifier);
187
            $this->add($n);
188
        }
189
    }
190
191
    /**
192
     * Determines the correct type and value when a short object/array syntax is found
193
     *
194
     * @param      string  $value  The value assumed to start with { or ( or characters
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
195
     *
196
     * @return     array   array with the type and $value (unchanged for now)
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
197
     * @see self:define
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
198
     */
199
    private function onObject($value):array
1 ignored issue
show
Coding Style introduced by
Private method name "Node::onObject" must be prefixed with an underscore
Loading history...
200
    {
201
        json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES);
202
        if (json_last_error() === JSON_ERROR_NONE)  return [Y\JSON, $value];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\JSON was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
203
        if (preg_match(R::MAPPING, $value))         return [Y\COMPACT_MAPPING, $value];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
204
        if (preg_match(R::SEQUENCE, $value))        return [Y\COMPACT_SEQUENCE, $value];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
205
        return [Y\PARTIAL, $value];
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\PARTIAL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
206
    }
207
208
    /**
209
     * Determines type and value when an hyphen "-" is found
210
     *
211
     * @param      string $nodeValue  The node value
2 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
212
     *
213
     * @return     array   array with the type and $value
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
214
     */
215
    private function onHyphen($nodeValue):array
0 ignored issues
show
Coding Style introduced by
Private method name "Node::onHyphen" must be prefixed with an underscore
Loading history...
216
    {
217
        if (substr($nodeValue, 0, 3) === '---') {
218
            $rest = trim(substr($nodeValue, 3));
219
            if (empty($rest)) return [Y\DOC_START, null];
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\DOC_START was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
220
            $n = new Node($rest, $this->line);
221
            $n->indent = $this->indent + 4;
222
            return [Y\DOC_START, $n->setParent($this)];
223
        }
224
        if (preg_match(R::ITEM, $nodeValue, $matches)) {
225
            if (isset($matches[1]) && !empty(trim($matches[1]))) {
226
                $n = new Node(trim($matches[1]), $this->line);
227
                return [Y\ITEM, $n->setParent($this)];
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\ITEM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
228
            }
229
            return [Y\ITEM, null];
230
        }
231
        return [Y\SCALAR, $nodeValue];
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\SCALAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
232
    }
233
234
    /**
235
     * Determines the type and value according to $nodeValue when one of these characters is found : !,&,*
236
     *
237
     * @param      string  $nodeValue  The node value
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
238
     *
239
     * @return     array   array with the type and $value
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
240
     * @see self::define
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
241
     */
242
    private function onNodeAction($nodeValue):array
0 ignored issues
show
Coding Style introduced by
Private method name "Node::onNodeAction" must be prefixed with an underscore
Loading history...
243
    {
244
        // TODO: handle tags like  <tag:clarkevans.com,2002:invoice>
245
        $v = substr($nodeValue, 1);
246
        $type = ['!' => Y\TAG, '&' => Y\REF_DEF, '*' => Y\REF_CALL][$nodeValue[0]];
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\REF_CALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\TAG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\REF_DEF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
247
        $pos = strpos($v, ' ');
248
        $this->identifier = is_bool($pos) ? $v : strstr($v, ' ', true);
0 ignored issues
show
introduced by
The condition is_bool($pos) is always false.
Loading history...
249
        $n = is_bool($pos) ? null : (new Node(trim(substr($nodeValue, $pos+1)), $this->line))->setParent($this);
0 ignored issues
show
introduced by
The condition is_bool($pos) is always false.
Loading history...
250
        return [$type, $n];
251
    }
252
253
    /**
254
     * Returns the correct PHP datatype for the value of the current Node
255
     * (simple value Node assumed -> $value is a scalar)
256
     *
257
     * @return     mixed  The value as PHP type.
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
258
     */
259
    public function getPhpValue()
260
    {
261
        $v = $this->value;
262
        if (is_null($v)) return null;
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
263
        if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar($v);
1 ignored issue
show
Bug introduced by
It seems like $v can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; however, parameter $v of Dallgoot\Yaml\Node::getScalar() does only seem to accept string, 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

263
        if ($this->type & (Y\REF_CALL | Y\SCALAR)) return self::getScalar(/** @scrutinizer ignore-type */ $v);
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\SCALAR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\REF_CALL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
264
        if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr($v, 1, -1), $this->type);
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like $v can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; however, parameter $string of substr() does only seem to accept string, 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

264
        if ($this->type & (Y\COMPACT_MAPPING | Y\COMPACT_SEQUENCE)) return self::getCompact(substr(/** @scrutinizer ignore-type */ $v, 1, -1), $this->type);
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
265
        switch ($this->type) {
266
            case Y\JSON:   return json_decode($v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR);
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\JSON was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like $v can also be of type Dallgoot\Yaml\NodeList and Dallgoot\Yaml\Node; however, parameter $json of json_decode() does only seem to accept string, 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

266
            case Y\JSON:   return json_decode(/** @scrutinizer ignore-type */ $v, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR);
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
267
            case Y\QUOTED: return substr($v, 1, -1);
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\QUOTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
268
            case Y\RAW:    return strval($v);
1 ignored issue
show
Bug introduced by
The constant Dallgoot\Yaml\Y\RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
269
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
270
                trigger_error("Error can not get PHP type for ".Y\getName($this->type), E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The function getName was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

270
                trigger_error("Error can not get PHP type for "./** @scrutinizer ignore-call */ Y\getName($this->type), E_USER_WARNING);
Loading history...
271
                return null;
272
        }
273
    }
274
275
    /**
276
     * Returns the correct PHP type according to the string value
277
     *
278
     * @param      string  $v      a string value
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
279
     *
280
     * @return     mixed   The scalar value with appropriate PHP type
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
281
     */
282
    private static function getScalar(string $v)
0 ignored issues
show
Coding Style introduced by
Private method name "Node::getScalar" must be prefixed with an underscore
Loading history...
283
    {
284
        $types = ['yes'   => true,
285
                  'no'    => false,
286
                  'true'  => true,
287
                  'false' => false,
288
                  'null'  => null,
289
                  '.inf'  => INF,
290
                  '-.inf' => -INF,
291
                  '.nan'  => NAN
292
        ];
293
        if (isset($types[strtolower($v)])) return $types[strtolower($v)];
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
294
        if (R::isDate($v))   return date_create($v);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
295
        if (R::isNumber($v)) return self::getNumber($v);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
296
        return strval($v);
297
    }
298
299
    /**
300
     * Returns the correct PHP type according to the string value
301
     *
302
     * @param      string  $v      a string value
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 6 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
303
     *
304
     * @return     int|float   The scalar value with appropriate PHP type
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
305
     */
306
    private static function getNumber(string $v)
0 ignored issues
show
Coding Style introduced by
Private method name "Node::getNumber" must be prefixed with an underscore
Loading history...
307
    {
308
        if (preg_match("/^(0o\d+)$/i", $v))      return intval(base_convert($v, 8, 10));
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
309
        if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10));
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
310
        // TODO: remove these if not needed
311
        // if preg_match("/^([\d.]+e[-+]\d{1,2})$/", $v)://fall through
312
        // if preg_match("/^([-+]?(?:\d+|\d*.\d+))$/", $v):
313
            return is_bool(strpos($v, '.')) ? intval($v) : floatval($v);
0 ignored issues
show
introduced by
The condition is_bool(strpos($v, '.')) is always false.
Loading history...
314
    }
315
316
    private static function getCompact(string $mappingOrSeqString, int $type):object
0 ignored issues
show
Coding Style introduced by
Private method name "Node::getCompact" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
317
    {
318
        $out = new Compact();
319
        if ($type === Y\COMPACT_SEQUENCE) {
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_SEQUENCE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
320
            $f = function ($e) { return self::getScalar(trim($e));};
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
321
            //TODO : that's not robust enough, improve it
322
            foreach (array_map($f, explode(",", $mappingOrSeqString)) as $key => $value) {
323
                $out[$key] = $value;
324
            }
325
        }
326
        if ($type === Y\COMPACT_MAPPING) {
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\COMPACT_MAPPING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
327
            //TODO : that's not robust enough, improve it
328
            foreach (explode(',', $mappingOrSeqString) as $value) {
329
                list($keyName, $keyValue) = explode(':', $value);
330
                $out->{trim($keyName)} = self::getScalar(trim($keyValue));
331
            }
332
        }
333
        return $out;
334
    }
335
336
    /**
337
     * PHP internal function for debugging purpose : simplify output provided by 'var_dump'
338
     *
339
     * @return     array  the Node properties and respective values displayed by 'var_dump'
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
340
     */
341
    public function __debugInfo():array
342
    {
343
        return ['line'  => $this->line,
344
                'indent'=> $this->indent,
345
                'type'  => Y::getName($this->type).($this->identifier ? "($this->identifier)" : ''),
346
                'value' => $this->value];
347
    }
348
}
349