Conditions | 132 |
Paths | 13660 |
Total Lines | 399 |
Code Lines | 241 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
116 | private function doParse(string $value, int $flags) |
||
117 | { |
||
118 | $this->currentLineNb = -1; |
||
119 | $this->currentLine = ''; |
||
120 | $value = $this->cleanup($value); |
||
121 | $this->lines = explode("\n", $value); |
||
122 | $this->numberOfParsedLines = \count($this->lines); |
||
123 | $this->locallySkippedLineNumbers = []; |
||
124 | |||
125 | if (null === $this->totalNumberOfLines) { |
||
126 | $this->totalNumberOfLines = $this->numberOfParsedLines; |
||
127 | } |
||
128 | |||
129 | if (!$this->moveToNextLine()) { |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | $data = []; |
||
134 | $context = null; |
||
135 | $allowOverwrite = false; |
||
136 | |||
137 | while ($this->isCurrentLineEmpty()) { |
||
138 | if (!$this->moveToNextLine()) { |
||
139 | return null; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // Resolves the tag and returns if end of the document |
||
144 | if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) { |
||
145 | return new TaggedValue($tag, ''); |
||
146 | } |
||
147 | |||
148 | do { |
||
149 | if ($this->isCurrentLineEmpty()) { |
||
150 | continue; |
||
151 | } |
||
152 | |||
153 | // tab? |
||
154 | if ("\t" === $this->currentLine[0]) { |
||
155 | throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
156 | } |
||
157 | |||
158 | Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename); |
||
159 | |||
160 | $isRef = $mergeNode = false; |
||
161 | if ('-' === $this->currentLine[0] && self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) { |
||
162 | if ($context && 'mapping' == $context) { |
||
163 | throw new ParseException('You cannot define a sequence item when in a mapping.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
164 | } |
||
165 | $context = 'sequence'; |
||
166 | |||
167 | if (isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) { |
||
168 | $isRef = $matches['ref']; |
||
169 | $this->refsBeingParsed[] = $isRef; |
||
170 | $values['value'] = $matches['value']; |
||
171 | } |
||
172 | |||
173 | if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) { |
||
174 | throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
175 | } |
||
176 | |||
177 | // array |
||
178 | if (isset($values['value']) && 0 === strpos(ltrim($values['value'], ' '), '-')) { |
||
179 | // Inline first child |
||
180 | $currentLineNumber = $this->getRealCurrentLineNb(); |
||
181 | |||
182 | $sequenceIndentation = \strlen($values['leadspaces']) + 1; |
||
183 | $sequenceYaml = substr($this->currentLine, $sequenceIndentation); |
||
184 | $sequenceYaml .= "\n".$this->getNextEmbedBlock($sequenceIndentation, true); |
||
185 | |||
186 | $data[] = $this->parseBlock($currentLineNumber, rtrim($sequenceYaml), $flags); |
||
187 | } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { |
||
188 | $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true) ?? '', $flags); |
||
189 | } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) { |
||
190 | $data[] = new TaggedValue( |
||
191 | $subTag, |
||
192 | $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags) |
||
193 | ); |
||
194 | } else { |
||
195 | if ( |
||
196 | isset($values['leadspaces']) |
||
197 | && ( |
||
198 | '!' === $values['value'][0] |
||
199 | || self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches) |
||
200 | ) |
||
201 | ) { |
||
202 | $block = $values['value']; |
||
203 | if ($this->isNextLineIndented() || isset($matches['value']) && '>-' === $matches['value']) { |
||
204 | $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1); |
||
205 | } |
||
206 | |||
207 | $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags); |
||
208 | } else { |
||
209 | $data[] = $this->parseValue($values['value'], $flags, $context); |
||
210 | } |
||
211 | } |
||
212 | if ($isRef) { |
||
213 | $this->refs[$isRef] = end($data); |
||
214 | array_pop($this->refsBeingParsed); |
||
215 | } |
||
216 | } elseif ( |
||
217 | self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values) |
||
218 | && (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"])) |
||
219 | ) { |
||
220 | if ($context && 'sequence' == $context) { |
||
221 | throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
222 | } |
||
223 | $context = 'mapping'; |
||
224 | |||
225 | try { |
||
226 | $key = Inline::parseScalar($values['key']); |
||
227 | } catch (ParseException $e) { |
||
228 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
229 | $e->setSnippet($this->currentLine); |
||
230 | |||
231 | throw $e; |
||
232 | } |
||
233 | |||
234 | if (!\is_string($key) && !\is_int($key)) { |
||
235 | throw new ParseException((is_numeric($key) ? 'Numeric' : 'Non-string').' keys are not supported. Quote your evaluable mapping keys instead.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
236 | } |
||
237 | |||
238 | // Convert float keys to strings, to avoid being converted to integers by PHP |
||
239 | if (\is_float($key)) { |
||
240 | $key = (string) $key; |
||
241 | } |
||
242 | |||
243 | if ('<<' === $key && (!isset($values['value']) || '&' !== $values['value'][0] || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) { |
||
244 | $mergeNode = true; |
||
245 | $allowOverwrite = true; |
||
246 | if (isset($values['value'][0]) && '*' === $values['value'][0]) { |
||
247 | $refName = substr(rtrim($values['value']), 1); |
||
248 | if (!\array_key_exists($refName, $this->refs)) { |
||
249 | if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) { |
||
250 | throw new ParseException(sprintf('Circular reference [%s] detected for reference "%s".', implode(', ', array_merge(\array_slice($this->refsBeingParsed, $pos), [$refName])), $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
251 | } |
||
252 | |||
253 | throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
254 | } |
||
255 | |||
256 | $refValue = $this->refs[$refName]; |
||
257 | |||
258 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) { |
||
259 | $refValue = (array) $refValue; |
||
260 | } |
||
261 | |||
262 | if (!\is_array($refValue)) { |
||
263 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
264 | } |
||
265 | |||
266 | $data += $refValue; // array union |
||
267 | } else { |
||
268 | if (isset($values['value']) && '' !== $values['value']) { |
||
269 | $value = $values['value']; |
||
270 | } else { |
||
271 | $value = $this->getNextEmbedBlock(); |
||
272 | } |
||
273 | $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags); |
||
274 | |||
275 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) { |
||
276 | $parsed = (array) $parsed; |
||
277 | } |
||
278 | |||
279 | if (!\is_array($parsed)) { |
||
280 | throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
281 | } |
||
282 | |||
283 | if (isset($parsed[0])) { |
||
284 | // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes |
||
285 | // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier |
||
286 | // in the sequence override keys specified in later mapping nodes. |
||
287 | foreach ($parsed as $parsedItem) { |
||
288 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) { |
||
289 | $parsedItem = (array) $parsedItem; |
||
290 | } |
||
291 | |||
292 | if (!\is_array($parsedItem)) { |
||
293 | throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename); |
||
294 | } |
||
295 | |||
296 | $data += $parsedItem; // array union |
||
297 | } |
||
298 | } else { |
||
299 | // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the |
||
300 | // current mapping, unless the key already exists in it. |
||
301 | $data += $parsed; // array union |
||
302 | } |
||
303 | } |
||
304 | } elseif ('<<' !== $key && isset($values['value']) && '&' === $values['value'][0] && self::preg_match(self::REFERENCE_PATTERN, $values['value'], $matches)) { |
||
305 | $isRef = $matches['ref']; |
||
306 | $this->refsBeingParsed[] = $isRef; |
||
307 | $values['value'] = $matches['value']; |
||
308 | } |
||
309 | |||
310 | $subTag = null; |
||
311 | if ($mergeNode) { |
||
312 | // Merge keys |
||
313 | } elseif (!isset($values['value']) || '' === $values['value'] || 0 === strpos($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) { |
||
314 | // hash |
||
315 | // if next line is less indented or equal, then it means that the current value is null |
||
316 | if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { |
||
317 | // Spec: Keys MUST be unique; first one wins. |
||
318 | // But overwriting is allowed when a merge node is used in current block. |
||
319 | if ($allowOverwrite || !isset($data[$key])) { |
||
320 | if (null !== $subTag) { |
||
321 | $data[$key] = new TaggedValue($subTag, ''); |
||
322 | } else { |
||
323 | $data[$key] = null; |
||
324 | } |
||
325 | } else { |
||
326 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
327 | } |
||
328 | } else { |
||
329 | // remember the parsed line number here in case we need it to provide some contexts in error messages below |
||
330 | $realCurrentLineNbKey = $this->getRealCurrentLineNb(); |
||
331 | $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags); |
||
332 | if ('<<' === $key) { |
||
333 | $this->refs[$refMatches['ref']] = $value; |
||
334 | |||
335 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) { |
||
336 | $value = (array) $value; |
||
337 | } |
||
338 | |||
339 | $data += $value; |
||
340 | } elseif ($allowOverwrite || !isset($data[$key])) { |
||
341 | // Spec: Keys MUST be unique; first one wins. |
||
342 | // But overwriting is allowed when a merge node is used in current block. |
||
343 | if (null !== $subTag) { |
||
344 | $data[$key] = new TaggedValue($subTag, $value); |
||
345 | } else { |
||
346 | $data[$key] = $value; |
||
347 | } |
||
348 | } else { |
||
349 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $realCurrentLineNbKey + 1, $this->currentLine); |
||
350 | } |
||
351 | } |
||
352 | } else { |
||
353 | $value = $this->parseValue(rtrim($values['value']), $flags, $context); |
||
354 | // Spec: Keys MUST be unique; first one wins. |
||
355 | // But overwriting is allowed when a merge node is used in current block. |
||
356 | if ($allowOverwrite || !isset($data[$key])) { |
||
357 | $data[$key] = $value; |
||
358 | } else { |
||
359 | throw new ParseException(sprintf('Duplicate key "%s" detected.', $key), $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
360 | } |
||
361 | } |
||
362 | if ($isRef) { |
||
363 | $this->refs[$isRef] = $data[$key]; |
||
364 | array_pop($this->refsBeingParsed); |
||
365 | } |
||
366 | } elseif ('"' === $this->currentLine[0] || "'" === $this->currentLine[0]) { |
||
367 | if (null !== $context) { |
||
368 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
369 | } |
||
370 | |||
371 | try { |
||
372 | return Inline::parse($this->lexInlineQuotedString(), $flags, $this->refs); |
||
373 | } catch (ParseException $e) { |
||
374 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
375 | $e->setSnippet($this->currentLine); |
||
376 | |||
377 | throw $e; |
||
378 | } |
||
379 | } elseif ('{' === $this->currentLine[0]) { |
||
380 | if (null !== $context) { |
||
381 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
382 | } |
||
383 | |||
384 | try { |
||
385 | $parsedMapping = Inline::parse($this->lexInlineMapping(), $flags, $this->refs); |
||
386 | |||
387 | while ($this->moveToNextLine()) { |
||
388 | if (!$this->isCurrentLineEmpty()) { |
||
389 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
390 | } |
||
391 | } |
||
392 | |||
393 | return $parsedMapping; |
||
394 | } catch (ParseException $e) { |
||
395 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
396 | $e->setSnippet($this->currentLine); |
||
397 | |||
398 | throw $e; |
||
399 | } |
||
400 | } elseif ('[' === $this->currentLine[0]) { |
||
401 | if (null !== $context) { |
||
402 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
403 | } |
||
404 | |||
405 | try { |
||
406 | $parsedSequence = Inline::parse($this->lexInlineSequence(), $flags, $this->refs); |
||
407 | |||
408 | while ($this->moveToNextLine()) { |
||
409 | if (!$this->isCurrentLineEmpty()) { |
||
410 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | return $parsedSequence; |
||
415 | } catch (ParseException $e) { |
||
416 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
417 | $e->setSnippet($this->currentLine); |
||
418 | |||
419 | throw $e; |
||
420 | } |
||
421 | } else { |
||
422 | // multiple documents are not supported |
||
423 | if ('---' === $this->currentLine) { |
||
424 | throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename); |
||
425 | } |
||
426 | |||
427 | if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) { |
||
428 | throw new ParseException('Complex mappings are not supported.', $this->getRealCurrentLineNb() + 1, $this->currentLine); |
||
429 | } |
||
430 | |||
431 | // 1-liner optionally followed by newline(s) |
||
432 | if (\is_string($value) && $this->lines[0] === trim($value)) { |
||
433 | try { |
||
434 | $value = Inline::parse($this->lines[0], $flags, $this->refs); |
||
435 | } catch (ParseException $e) { |
||
436 | $e->setParsedLine($this->getRealCurrentLineNb() + 1); |
||
437 | $e->setSnippet($this->currentLine); |
||
438 | |||
439 | throw $e; |
||
440 | } |
||
441 | |||
442 | return $value; |
||
443 | } |
||
444 | |||
445 | // try to parse the value as a multi-line string as a last resort |
||
446 | if (0 === $this->currentLineNb) { |
||
447 | $previousLineWasNewline = false; |
||
448 | $previousLineWasTerminatedWithBackslash = false; |
||
449 | $value = ''; |
||
450 | |||
451 | foreach ($this->lines as $line) { |
||
452 | $trimmedLine = trim($line); |
||
453 | if ('#' === ($trimmedLine[0] ?? '')) { |
||
454 | continue; |
||
455 | } |
||
456 | // If the indentation is not consistent at offset 0, it is to be considered as a ParseError |
||
457 | if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) { |
||
458 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
459 | } |
||
460 | |||
461 | if (false !== strpos($line, ': ')) { |
||
462 | throw new ParseException('Mapping values are not allowed in multi-line blocks.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
463 | } |
||
464 | |||
465 | if ('' === $trimmedLine) { |
||
466 | $value .= "\n"; |
||
467 | } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) { |
||
468 | $value .= ' '; |
||
469 | } |
||
470 | |||
471 | if ('' !== $trimmedLine && '\\' === substr($line, -1)) { |
||
472 | $value .= ltrim(substr($line, 0, -1)); |
||
473 | } elseif ('' !== $trimmedLine) { |
||
474 | $value .= $trimmedLine; |
||
475 | } |
||
476 | |||
477 | if ('' === $trimmedLine) { |
||
478 | $previousLineWasNewline = true; |
||
479 | $previousLineWasTerminatedWithBackslash = false; |
||
480 | } elseif ('\\' === substr($line, -1)) { |
||
481 | $previousLineWasNewline = false; |
||
482 | $previousLineWasTerminatedWithBackslash = true; |
||
483 | } else { |
||
484 | $previousLineWasNewline = false; |
||
485 | $previousLineWasTerminatedWithBackslash = false; |
||
486 | } |
||
487 | } |
||
488 | |||
489 | try { |
||
490 | return Inline::parse(trim($value)); |
||
491 | } catch (ParseException $e) { |
||
492 | // fall-through to the ParseException thrown below |
||
493 | } |
||
494 | } |
||
495 | |||
496 | throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); |
||
497 | } |
||
498 | } while ($this->moveToNextLine()); |
||
499 | |||
500 | if (null !== $tag) { |
||
501 | $data = new TaggedValue($tag, $data); |
||
502 | } |
||
503 | |||
504 | if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && 'mapping' === $context && !\is_object($data)) { |
||
505 | $object = new \stdClass(); |
||
506 | |||
507 | foreach ($data as $key => $value) { |
||
508 | $object->$key = $value; |
||
509 | } |
||
510 | |||
511 | $data = $object; |
||
512 | } |
||
513 | |||
514 | return empty($data) ? null : $data; |
||
515 | } |
||
1309 |