@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * Create the Node object and parses $nodeString IF not null (else assume a root type Node) |
33 | 33 | * |
34 | 34 | * @param string|null $nodeString The node string |
35 | - * @param int|null $line The line |
|
35 | + * @param integer $line The line |
|
36 | 36 | */ |
37 | 37 | public function __construct($nodeString = null, $line = 0) |
38 | 38 | { |
@@ -200,12 +200,18 @@ discard block |
||
200 | 200 | } |
201 | 201 | } |
202 | 202 | |
203 | + /** |
|
204 | + * @param string $nodeValue |
|
205 | + */ |
|
203 | 206 | private function onQuoted($nodeValue) |
204 | 207 | { |
205 | 208 | $this->type = R::isProperlyQuoted($nodeValue) ? Y::QUOTED : Y::PARTIAL; |
206 | 209 | $this->value = $nodeValue; |
207 | 210 | } |
208 | 211 | |
212 | + /** |
|
213 | + * @param string $nodeValue |
|
214 | + */ |
|
209 | 215 | private function onSetElement($nodeValue) |
210 | 216 | { |
211 | 217 | $this->type = $nodeValue[0] === '?' ? Y::SET_KEY : Y::SET_VALUE; |
@@ -220,7 +226,7 @@ discard block |
||
220 | 226 | * Process when a "key: value" syntax is found in the parsed string |
221 | 227 | * Note : key is match 1, value is match 2 as per regex from R::KEY |
222 | 228 | * |
223 | - * @param array $matches The matches provided by 'preg_match' function in Node::parse |
|
229 | + * @param string[] $matches The matches provided by 'preg_match' function in Node::parse |
|
224 | 230 | */ |
225 | 231 | private function onKey(array $matches) |
226 | 232 | { |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | elseif (in_array($first, ['?', ':'])) $this->onSetElement($nodeValue); |
186 | 186 | elseif (in_array($first, ['!', '&', '*'])) $this->onNodeAction($nodeValue); |
187 | 187 | else { |
188 | - $characters = [ '#' => [Y::COMMENT, $v], |
|
188 | + $characters = ['#' => [Y::COMMENT, $v], |
|
189 | 189 | '%' => [Y::DIRECTIVE, $v], |
190 | 190 | '>' => [Y::LITT_FOLDED, null], |
191 | 191 | '|' => [Y::LITT, null] |
@@ -254,28 +254,28 @@ discard block |
||
254 | 254 | private function onCompact($value) |
255 | 255 | { |
256 | 256 | $this->value = json_decode($value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
257 | - if (json_last_error() === JSON_ERROR_NONE){ |
|
257 | + if (json_last_error() === JSON_ERROR_NONE) { |
|
258 | 258 | $this->type = Y::JSON; |
259 | 259 | return; |
260 | 260 | } |
261 | 261 | $this->value = new NodeList(); |
262 | - if (preg_match(R::MAPPING, $value)){ |
|
262 | + if (preg_match(R::MAPPING, $value)) { |
|
263 | 263 | $this->type = Y::COMPACT_MAPPING; |
264 | 264 | $this->value->type = Y::COMPACT_MAPPING; |
265 | - preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1,-1)), $matches); |
|
265 | + preg_match_all(R::MAPPING_VALUES, trim(substr($value, 1, -1)), $matches); |
|
266 | 266 | foreach ($matches['k'] as $index => $property) { |
267 | 267 | $n = new Node('', $this->line); |
268 | 268 | $n->type = Y::KEY; |
269 | - $n->identifier = trim($property, '"\' ');//TODO : maybe check for proper quoting first ? |
|
269 | + $n->identifier = trim($property, '"\' '); //TODO : maybe check for proper quoting first ? |
|
270 | 270 | $n->value = new Node($matches['v'][$index], $this->line); |
271 | 271 | $this->value->push($n); |
272 | 272 | } |
273 | 273 | return; |
274 | 274 | } |
275 | - if (preg_match(R::SEQUENCE, $value)){ |
|
275 | + if (preg_match(R::SEQUENCE, $value)) { |
|
276 | 276 | $this->type = Y::COMPACT_SEQUENCE; |
277 | 277 | $this->value->type = Y::COMPACT_SEQUENCE; |
278 | - $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1,-1)), $matches); |
|
278 | + $count = preg_match_all(R::SEQUENCE_VALUES, trim(substr($value, 1, -1)), $matches); |
|
279 | 279 | foreach ($matches['item'] as $key => $item) { |
280 | 280 | $i = new Node('', $this->line); |
281 | 281 | $i->type = Y::ITEM; |
@@ -68,8 +68,12 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function getParent(int $indent = null, $type = 0):Node |
70 | 70 | { |
71 | - if ($this->type === Y::ROOT) return $this; |
|
72 | - if (!is_int($indent)) return $this->parent ?? $this; |
|
71 | + if ($this->type === Y::ROOT) { |
|
72 | + return $this; |
|
73 | + } |
|
74 | + if (!is_int($indent)) { |
|
75 | + return $this->parent ?? $this; |
|
76 | + } |
|
73 | 77 | $cursor = $this; |
74 | 78 | while ($cursor instanceof Node && $cursor->indent >= $indent) { |
75 | 79 | if ($cursor->indent === $indent && $cursor->type !== $type) { |
@@ -101,7 +105,7 @@ discard block |
||
101 | 105 | if (is_null($current)) { |
102 | 106 | $this->value = $child; |
103 | 107 | return; |
104 | - }elseif ($current instanceof Node) { |
|
108 | + } elseif ($current instanceof Node) { |
|
105 | 109 | $this->value = new NodeList(); |
106 | 110 | if ($current->type & Y::LITTERALS) { |
107 | 111 | $this->value->type = $current->type; |
@@ -179,12 +183,17 @@ discard block |
||
179 | 183 | { |
180 | 184 | $v = ltrim(substr($nodeValue, 1)); |
181 | 185 | $first = $nodeValue[0]; |
182 | - if ($first === "-") $this->onHyphen($nodeValue); |
|
183 | - elseif (in_array($first, ['"', "'"])) $this->onQuoted($nodeValue); |
|
184 | - elseif (in_array($first, ['{', '['])) $this->onCompact($nodeValue); |
|
185 | - elseif (in_array($first, ['?', ':'])) $this->onSetElement($nodeValue); |
|
186 | - elseif (in_array($first, ['!', '&', '*'])) $this->onNodeAction($nodeValue); |
|
187 | - else { |
|
186 | + if ($first === "-") { |
|
187 | + $this->onHyphen($nodeValue); |
|
188 | + } elseif (in_array($first, ['"', "'"])) { |
|
189 | + $this->onQuoted($nodeValue); |
|
190 | + } elseif (in_array($first, ['{', '['])) { |
|
191 | + $this->onCompact($nodeValue); |
|
192 | + } elseif (in_array($first, ['?', ':'])) { |
|
193 | + $this->onSetElement($nodeValue); |
|
194 | + } elseif (in_array($first, ['!', '&', '*'])) { |
|
195 | + $this->onNodeAction($nodeValue); |
|
196 | + } else { |
|
188 | 197 | $characters = [ '#' => [Y::COMMENT, $v], |
189 | 198 | '%' => [Y::DIRECTIVE, $v], |
190 | 199 | '>' => [Y::LITT_FOLDED, null], |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @return mixed The parent (object|array) or a string representing the NodeList. |
77 | 77 | */ |
78 | - private static function buildNodeList(NodeList $node, &$parent=null) |
|
78 | + private static function buildNodeList(NodeList $node, &$parent = null) |
|
79 | 79 | { |
80 | 80 | $node->forceType(); |
81 | - if ($node->type & (Y::RAW | Y::LITTERALS)) { |
|
81 | + if ($node->type & (Y::RAW|Y::LITTERALS)) { |
|
82 | 82 | return self::buildLitteral($node, (int) $node->type); |
83 | 83 | } |
84 | - $action = function ($child, &$parent, &$out) { |
|
84 | + $action = function($child, &$parent, &$out) { |
|
85 | 85 | self::build($child, $out); |
86 | 86 | }; |
87 | 87 | if ($node->type & (Y::COMPACT_MAPPING|Y::MAPPING|Y::SET)) { |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $out = $parent ?? []; |
91 | 91 | } else { |
92 | 92 | $out = ''; |
93 | - $action = function ($child, &$parent, &$out) { |
|
93 | + $action = function($child, &$parent, &$out) { |
|
94 | 94 | if ($child->type & (Y::SCALAR|Y::QUOTED)) { |
95 | 95 | if ($parent) { |
96 | 96 | $parent->setText(self::build($child)); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | private static function buildNode(Node $node, &$parent) |
121 | 121 | { |
122 | 122 | extract((array) $node, EXTR_REFS); |
123 | - if ($type & (Y::REF_DEF | Y::REF_CALL)) { |
|
123 | + if ($type&(Y::REF_DEF|Y::REF_CALL)) { |
|
124 | 124 | if (is_object($value)) { |
125 | 125 | $tmp = self::build($value, $parent) ?? $parent; |
126 | 126 | } else { |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
130 | 130 | return self::$_root->getReference($identifier); |
131 | 131 | } |
132 | - if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
132 | + if ($type&(Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
|
133 | 133 | return self::buildNodeList($node->value, $parent); |
134 | 134 | } |
135 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
135 | + if ($type&Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
136 | 136 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
137 | 137 | Y::ITEM => 'buildItem', |
138 | 138 | Y::KEY => 'buildKey', |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * @throws \ParseError if Key has no name(identifier) Note: empty string is allowed |
156 | 156 | * @return null |
157 | 157 | */ |
158 | - private static function buildKey(Node $node, &$parent=null) |
|
158 | + private static function buildKey(Node $node, &$parent = null) |
|
159 | 159 | { |
160 | 160 | extract((array) $node, EXTR_REFS); |
161 | 161 | if (is_null($identifier)) { |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | $numKeys = array_filter(array_keys($ref), 'is_int'); |
203 | 203 | $key = count($numKeys) > 0 ? max($numKeys) + 1 : 0; |
204 | 204 | if ($value instanceof Node) { |
205 | - if($value->type & Y::KEY) { |
|
205 | + if ($value->type & Y::KEY) { |
|
206 | 206 | self::buildKey($node->value, $parent); |
207 | 207 | return; |
208 | 208 | } elseif ($value->type & Y::ITEM) { |
@@ -231,13 +231,13 @@ discard block |
||
231 | 231 | //remove trailing blank |
232 | 232 | while ($list->top()->type & Y::BLANK) $list->pop(); |
233 | 233 | $result = ''; |
234 | - $separator = [ 0 => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][(int) $type]; |
|
234 | + $separator = [0 => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][(int) $type]; |
|
235 | 235 | foreach ($list as $child) { |
236 | 236 | if ($child->value instanceof NodeList) { |
237 | 237 | $result .= self::buildLitteral($child->value, $type).$separator; |
238 | 238 | } else { |
239 | 239 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
240 | - if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
240 | + if ($type&Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
|
241 | 241 | if ($result[-1] === $separator) |
242 | 242 | $result[-1] = "\n"; |
243 | 243 | if ($result[-1] === "\n") |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | private function buildSetKey(Node $node, &$parent) |
262 | 262 | { |
263 | 263 | $built = is_object($node->value) ? self::build($node->value) : null; |
264 | - $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
|
264 | + $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" ') : $built; |
|
265 | 265 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
266 | 266 | if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
267 | 267 | $parent->{trim($key, '\'" ')} = null; |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | { |
278 | 278 | $prop = array_keys(get_object_vars($parent)); |
279 | 279 | $key = end($prop); |
280 | - if ($node->value->type & (Y::ITEM|Y::KEY )) { |
|
280 | + if ($node->value->type & (Y::ITEM|Y::KEY)) { |
|
281 | 281 | $node->value = new NodeList($node->value); |
282 | 282 | } |
283 | 283 | $parent->{$key} = self::build($node->value); |
@@ -35,10 +35,14 @@ discard block |
||
35 | 35 | $documents = []; |
36 | 36 | $_root->value instanceof NodeList && $_root->value->setIteratorMode(NodeList::IT_MODE_DELETE); |
37 | 37 | foreach ($_root->value as $child) { |
38 | - if ($child->type & Y::DOC_START) $totalDocStart++; |
|
38 | + if ($child->type & Y::DOC_START) { |
|
39 | + $totalDocStart++; |
|
40 | + } |
|
39 | 41 | //if 0 or 1 DOC_START = we are still in first document |
40 | 42 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
41 | - if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new NodeList(); |
|
43 | + if (!isset($documents[$currentDoc])) { |
|
44 | + $documents[$currentDoc] = new NodeList(); |
|
45 | + } |
|
42 | 46 | $documents[$currentDoc]->push($child); |
43 | 47 | } |
44 | 48 | $content = []; |
@@ -63,7 +67,9 @@ discard block |
||
63 | 67 | */ |
64 | 68 | private static function build(object $node, &$parent = null) |
65 | 69 | { |
66 | - if ($node instanceof NodeList) return self::buildNodeList($node, $parent); |
|
70 | + if ($node instanceof NodeList) { |
|
71 | + return self::buildNodeList($node, $parent); |
|
72 | + } |
|
67 | 73 | return self::buildNode($node, $parent); |
68 | 74 | } |
69 | 75 | |
@@ -126,13 +132,17 @@ discard block |
||
126 | 132 | } else { |
127 | 133 | $tmp = Node2PHP::get($node); |
128 | 134 | } |
129 | - if ($type === Y::REF_DEF) self::$_root->addReference($identifier, $tmp); |
|
135 | + if ($type === Y::REF_DEF) { |
|
136 | + self::$_root->addReference($identifier, $tmp); |
|
137 | + } |
|
130 | 138 | return self::$_root->getReference($identifier); |
131 | 139 | } |
132 | 140 | if ($type & (Y::COMPACT_MAPPING|Y::COMPACT_SEQUENCE)) { |
133 | 141 | return self::buildNodeList($node->value, $parent); |
134 | 142 | } |
135 | - if ($type & Y::COMMENT) self::$_root->addComment($node->line, $node->value); |
|
143 | + if ($type & Y::COMMENT) { |
|
144 | + self::$_root->addComment($node->line, $node->value); |
|
145 | + } |
|
136 | 146 | $typesActions = [Y::DIRECTIVE => 'buildDirective', |
137 | 147 | Y::ITEM => 'buildItem', |
138 | 148 | Y::KEY => 'buildKey', |
@@ -229,7 +239,9 @@ discard block |
||
229 | 239 | $list->rewind(); |
230 | 240 | $refIndent = $list->current()->indent; |
231 | 241 | //remove trailing blank |
232 | - while ($list->top()->type & Y::BLANK) $list->pop(); |
|
242 | + while ($list->top()->type & Y::BLANK) { |
|
243 | + $list->pop(); |
|
244 | + } |
|
233 | 245 | $result = ''; |
234 | 246 | $separator = [ 0 => '', Y::LITT => "\n", Y::LITT_FOLDED => ' '][(int) $type]; |
235 | 247 | foreach ($list as $child) { |
@@ -238,10 +250,12 @@ discard block |
||
238 | 250 | } else { |
239 | 251 | $val = $child->type & (Y::SCALAR|Y::BLANK) ? $child->value : substr($child->raw, $refIndent); |
240 | 252 | if ($type & Y::LITT_FOLDED && ($child->indent > $refIndent || ($child->type & Y::BLANK))) { |
241 | - if ($result[-1] === $separator) |
|
242 | - $result[-1] = "\n"; |
|
243 | - if ($result[-1] === "\n") |
|
244 | - $result .= $val; |
|
253 | + if ($result[-1] === $separator) { |
|
254 | + $result[-1] = "\n"; |
|
255 | + } |
|
256 | + if ($result[-1] === "\n") { |
|
257 | + $result .= $val; |
|
258 | + } |
|
245 | 259 | continue; |
246 | 260 | } |
247 | 261 | $result .= $val.$separator; |
@@ -263,7 +277,9 @@ discard block |
||
263 | 277 | $built = is_object($node->value) ? self::build($node->value) : null; |
264 | 278 | $stringKey = is_string($built) && Regex::isProperlyQuoted($built) ? trim($built, '\'" '): $built; |
265 | 279 | $key = json_encode($stringKey, JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
266 | - if (empty($key)) throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
280 | + if (empty($key)) { |
|
281 | + throw new \Exception("Cant serialize complex key: ".var_export($node->value, true), 1); |
|
282 | + } |
|
267 | 283 | $parent->{trim($key, '\'" ')} = null; |
268 | 284 | } |
269 | 285 |