Skip to content

Commit eca1dd6

Browse files
Merge branch '3.2'
* 3.2: [Yaml] CS [DI] Fix PhpDumper generated doc block #20411 fix Yaml parsing for very long quoted strings [Workflow] add Phpdoc for better IDE support fix package name in conflict rule improve message when workflows are missing [Doctrine Bridge] fix priority for doctrine event listeners Use PHP functions as array_map callbacks when possible [Validator] revert wrong Phpdoc change Use proper line endings
2 parents d1abb47 + 62b4cdb commit eca1dd6

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

Inline.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ public static function dump($value, $flags = 0)
220220
case Escaper::requiresDoubleQuoting($value):
221221
return Escaper::escapeWithDoubleQuotes($value);
222222
case Escaper::requiresSingleQuoting($value):
223-
case preg_match('{^[0-9]+[_0-9]*$}', $value):
224-
case preg_match(self::getHexRegex(), $value):
225-
case preg_match(self::getTimestampRegex(), $value):
223+
case Parser::preg_match('{^[0-9]+[_0-9]*$}', $value):
224+
case Parser::preg_match(self::getHexRegex(), $value):
225+
case Parser::preg_match(self::getTimestampRegex(), $value):
226226
return Escaper::escapeWithSingleQuotes($value);
227227
default:
228228
return $value;
@@ -315,10 +315,10 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
315315
$i += strlen($output);
316316

317317
// remove comments
318-
if (preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
318+
if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
319319
$output = substr($output, 0, $match[0][1]);
320320
}
321-
} elseif (preg_match('/^(.'.($legacyOmittedKeySupport ? '+' : '*').'?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
321+
} elseif (Parser::preg_match('/^(.'.($legacyOmittedKeySupport ? '+' : '*').'?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
322322
$output = $match[1];
323323
$i += strlen($output);
324324
} else {
@@ -354,7 +354,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
354354
*/
355355
private static function parseQuotedScalar($scalar, &$i)
356356
{
357-
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
357+
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
358358
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
359359
}
360360

@@ -653,7 +653,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
653653
// Optimize for returning strings.
654654
case $scalar[0] === '+' || $scalar[0] === '-' || $scalar[0] === '.' || is_numeric($scalar[0]):
655655
switch (true) {
656-
case preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
656+
case Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar):
657657
$scalar = str_replace('_', '', (string) $scalar);
658658
// omitting the break / return as integers are handled in the next case
659659
case ctype_digit($scalar):
@@ -667,7 +667,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
667667

668668
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
669669
case is_numeric($scalar):
670-
case preg_match(self::getHexRegex(), $scalar):
670+
case Parser::preg_match(self::getHexRegex(), $scalar):
671671
$scalar = str_replace('_', '', $scalar);
672672

673673
return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
@@ -676,14 +676,14 @@ private static function evaluateScalar($scalar, $flags, $references = array())
676676
return -log(0);
677677
case '-.inf' === $scalarLower:
678678
return log(0);
679-
case preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
680-
case preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
679+
case Parser::preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
680+
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
681681
if (false !== strpos($scalar, ',')) {
682682
@trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
683683
}
684684

685685
return (float) str_replace(array(',', '_'), '', $scalar);
686-
case preg_match(self::getTimestampRegex(), $scalar):
686+
case Parser::preg_match(self::getTimestampRegex(), $scalar):
687687
if (Yaml::PARSE_DATETIME & $flags) {
688688
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
689689
return new \DateTime($scalar, new \DateTimeZone('UTC'));
@@ -755,7 +755,7 @@ public static function evaluateBinaryScalar($scalar)
755755
throw new ParseException(sprintf('The normalized base64 encoded data (data without whitespace characters) length must be a multiple of four (%d bytes given).', strlen($parsedBinaryData)));
756756
}
757757

758-
if (!preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
758+
if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
759759
throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData));
760760
}
761761

Parser.php

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function parse($value, $flags = 0)
8686
}
8787
}
8888

89-
if (!preg_match('//u', $value)) {
89+
if (false === preg_match('//u', $value)) {
9090
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
9191
}
9292
$this->currentLineNb = -1;
@@ -133,13 +133,13 @@ public function parse($value, $flags = 0)
133133
}
134134

135135
$isRef = $mergeNode = false;
136-
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
136+
if (self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
137137
if ($context && 'mapping' == $context) {
138138
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
139139
}
140140
$context = 'sequence';
141141

142-
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
142+
if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
143143
$isRef = $matches['ref'];
144144
$values['value'] = $matches['value'];
145145
}
@@ -154,7 +154,7 @@ public function parse($value, $flags = 0)
154154
);
155155
} else {
156156
if (isset($values['leadspaces'])
157-
&& preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
157+
&& self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->trimTag($values['value']), $matches)
158158
) {
159159
// this is a compact notation element, add to next block and parse
160160
$block = $values['value'];
@@ -170,7 +170,10 @@ public function parse($value, $flags = 0)
170170
if ($isRef) {
171171
$this->refs[$isRef] = end($data);
172172
}
173-
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]+\s+)?[^ \'"\[\{!].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
173+
} elseif (
174+
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|(?:![^\s]+\s+)?[^ \'"\[\{!].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)
175+
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
176+
) {
174177
if ($context && 'sequence' == $context) {
175178
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
176179
}
@@ -243,7 +246,7 @@ public function parse($value, $flags = 0)
243246
$data += $parsed; // array union
244247
}
245248
}
246-
} elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
249+
} elseif (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
247250
$isRef = $matches['ref'];
248251
$values['value'] = $matches['value'];
249252
}
@@ -356,27 +359,7 @@ public function parse($value, $flags = 0)
356359
}
357360
}
358361

359-
switch (preg_last_error()) {
360-
case PREG_INTERNAL_ERROR:
361-
$error = 'Internal PCRE error.';
362-
break;
363-
case PREG_BACKTRACK_LIMIT_ERROR:
364-
$error = 'pcre.backtrack_limit reached.';
365-
break;
366-
case PREG_RECURSION_LIMIT_ERROR:
367-
$error = 'pcre.recursion_limit reached.';
368-
break;
369-
case PREG_BAD_UTF8_ERROR:
370-
$error = 'Malformed UTF-8 data.';
371-
break;
372-
case PREG_BAD_UTF8_OFFSET_ERROR:
373-
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
374-
break;
375-
default:
376-
$error = 'Unable to parse.';
377-
}
378-
379-
throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
362+
throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
380363
}
381364
} while ($this->moveToNextLine());
382365

@@ -627,7 +610,7 @@ private function parseValue($value, $flags, $context)
627610
return $this->refs[$value];
628611
}
629612

630-
if (preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
613+
if (self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
631614
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
632615

633616
$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
@@ -647,7 +630,7 @@ private function parseValue($value, $flags, $context)
647630
$quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null;
648631

649632
// do not take following lines into account when the current line is a quoted single line value
650-
if (null !== $quotation && preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
633+
if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) {
651634
return Inline::parse($value, $flags, $this->refs);
652635
}
653636

@@ -713,7 +696,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
713696

714697
// determine indentation if not specified
715698
if (0 === $indentation) {
716-
if (preg_match('/^ +/', $this->currentLine, $matches)) {
699+
if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
717700
$indentation = strlen($matches[0]);
718701
}
719702
}
@@ -724,7 +707,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
724707
while (
725708
$notEOF && (
726709
$isCurrentLineBlank ||
727-
preg_match($pattern, $this->currentLine, $matches)
710+
self::preg_match($pattern, $this->currentLine, $matches)
728711
)
729712
) {
730713
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
@@ -812,10 +795,7 @@ private function isNextLineIndented()
812795
return false;
813796
}
814797

815-
$ret = false;
816-
if ($this->getCurrentLineIndentation() > $currentIndentation) {
817-
$ret = true;
818-
}
798+
$ret = $this->getCurrentLineIndentation() > $currentIndentation;
819799

820800
$this->moveToPreviousLine();
821801

@@ -916,14 +896,7 @@ private function isNextLineUnIndentedCollection()
916896
return false;
917897
}
918898

919-
$ret = false;
920-
if (
921-
$this->getCurrentLineIndentation() == $currentIndentation
922-
&&
923-
$this->isStringUnIndentedCollectionItem()
924-
) {
925-
$ret = true;
926-
}
899+
$ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem();
927900

928901
$this->moveToPreviousLine();
929902

@@ -947,7 +920,49 @@ private function isStringUnIndentedCollectionItem()
947920
*/
948921
private function isBlockScalarHeader()
949922
{
950-
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
923+
return (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
924+
}
925+
926+
/**
927+
* A local wrapper for `preg_match` which will throw a ParseException if there
928+
* is an internal error in the PCRE engine.
929+
*
930+
* This avoids us needing to check for "false" every time PCRE is used
931+
* in the YAML engine
932+
*
933+
* @throws ParseException on a PCRE internal error
934+
*
935+
* @see preg_last_error()
936+
*
937+
* @internal
938+
*/
939+
public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
940+
{
941+
if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
942+
switch (preg_last_error()) {
943+
case PREG_INTERNAL_ERROR:
944+
$error = 'Internal PCRE error.';
945+
break;
946+
case PREG_BACKTRACK_LIMIT_ERROR:
947+
$error = 'pcre.backtrack_limit reached.';
948+
break;
949+
case PREG_RECURSION_LIMIT_ERROR:
950+
$error = 'pcre.recursion_limit reached.';
951+
break;
952+
case PREG_BAD_UTF8_ERROR:
953+
$error = 'Malformed UTF-8 data.';
954+
break;
955+
case PREG_BAD_UTF8_OFFSET_ERROR:
956+
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
957+
break;
958+
default:
959+
$error = 'Error.';
960+
}
961+
962+
throw new ParseException($error);
963+
}
964+
965+
return $ret;
951966
}
952967

953968
/**
@@ -967,7 +982,7 @@ private function trimTag($value)
967982

968983
private function getLineTag($value, $flags, $nextLineCheck = true)
969984
{
970-
if ('' === $value || '!' !== $value[0] || 1 !== preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) {
985+
if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) {
971986
return;
972987
}
973988

Tests/ParserTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
class ParserTest extends TestCase
2121
{
22+
/** @var Parser */
2223
protected $parser;
2324

2425
protected function setUp()

0 commit comments

Comments
 (0)