comparison vendor/symfony/yaml/Inline.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Yaml; 12 namespace Symfony\Component\Yaml;
13 13
14 use Symfony\Component\Yaml\Exception\DumpException;
14 use Symfony\Component\Yaml\Exception\ParseException; 15 use Symfony\Component\Yaml\Exception\ParseException;
15 use Symfony\Component\Yaml\Exception\DumpException;
16 use Symfony\Component\Yaml\Tag\TaggedValue; 16 use Symfony\Component\Yaml\Tag\TaggedValue;
17 17
18 /** 18 /**
19 * Inline implements a YAML parser/dumper for the YAML inline syntax. 19 * Inline implements a YAML parser/dumper for the YAML inline syntax.
20 * 20 *
61 * 61 *
62 * @return mixed A PHP value 62 * @return mixed A PHP value
63 * 63 *
64 * @throws ParseException 64 * @throws ParseException
65 */ 65 */
66 public static function parse($value, $flags = 0, $references = array()) 66 public static function parse($value, $flags = 0, $references = [])
67 { 67 {
68 if (is_bool($flags)) { 68 if (\is_bool($flags)) {
69 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); 69 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
70 70
71 if ($flags) { 71 if ($flags) {
72 $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE; 72 $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE;
73 } else { 73 } else {
74 $flags = 0; 74 $flags = 0;
75 } 75 }
76 } 76 }
77 77
78 if (func_num_args() >= 3 && !is_array($references)) { 78 if (\func_num_args() >= 3 && !\is_array($references)) {
79 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead.', E_USER_DEPRECATED); 79 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead.', E_USER_DEPRECATED);
80 80
81 if ($references) { 81 if ($references) {
82 $flags |= Yaml::PARSE_OBJECT; 82 $flags |= Yaml::PARSE_OBJECT;
83 } 83 }
84 84
85 if (func_num_args() >= 4) { 85 if (\func_num_args() >= 4) {
86 @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED); 86 @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED);
87 87
88 if (func_get_arg(3)) { 88 if (func_get_arg(3)) {
89 $flags |= Yaml::PARSE_OBJECT_FOR_MAP; 89 $flags |= Yaml::PARSE_OBJECT_FOR_MAP;
90 } 90 }
91 } 91 }
92 92
93 if (func_num_args() >= 5) { 93 if (\func_num_args() >= 5) {
94 $references = func_get_arg(4); 94 $references = func_get_arg(4);
95 } else { 95 } else {
96 $references = array(); 96 $references = [];
97 } 97 }
98 } 98 }
99 99
100 self::initialize($flags); 100 self::initialize($flags);
101 101
108 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) { 108 if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
109 $mbEncoding = mb_internal_encoding(); 109 $mbEncoding = mb_internal_encoding();
110 mb_internal_encoding('ASCII'); 110 mb_internal_encoding('ASCII');
111 } 111 }
112 112
113 $i = 0; 113 try {
114 $tag = self::parseTag($value, $i, $flags); 114 $i = 0;
115 switch ($value[$i]) { 115 $tag = self::parseTag($value, $i, $flags);
116 case '[': 116 switch ($value[$i]) {
117 $result = self::parseSequence($value, $flags, $i, $references); 117 case '[':
118 ++$i; 118 $result = self::parseSequence($value, $flags, $i, $references);
119 break; 119 ++$i;
120 case '{': 120 break;
121 $result = self::parseMapping($value, $flags, $i, $references); 121 case '{':
122 ++$i; 122 $result = self::parseMapping($value, $flags, $i, $references);
123 break; 123 ++$i;
124 default: 124 break;
125 $result = self::parseScalar($value, $flags, null, $i, null === $tag, $references); 125 default:
126 } 126 $result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
127 127 }
128 if (null !== $tag) { 128
129 return new TaggedValue($tag, $result); 129 if (null !== $tag) {
130 } 130 return new TaggedValue($tag, $result);
131 131 }
132 // some comments are allowed at the end 132
133 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) { 133 // some comments are allowed at the end
134 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename); 134 if (preg_replace('/\s+#.*$/A', '', substr($value, $i))) {
135 } 135 throw new ParseException(sprintf('Unexpected characters near "%s".', substr($value, $i)), self::$parsedLineNumber + 1, $value, self::$parsedFilename);
136 136 }
137 if (isset($mbEncoding)) { 137
138 mb_internal_encoding($mbEncoding); 138 return $result;
139 } 139 } finally {
140 140 if (isset($mbEncoding)) {
141 return $result; 141 mb_internal_encoding($mbEncoding);
142 }
143 }
142 } 144 }
143 145
144 /** 146 /**
145 * Dumps a given PHP variable to a YAML string. 147 * Dumps a given PHP variable to a YAML string.
146 * 148 *
151 * 153 *
152 * @throws DumpException When trying to dump PHP resource 154 * @throws DumpException When trying to dump PHP resource
153 */ 155 */
154 public static function dump($value, $flags = 0) 156 public static function dump($value, $flags = 0)
155 { 157 {
156 if (is_bool($flags)) { 158 if (\is_bool($flags)) {
157 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); 159 @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED);
158 160
159 if ($flags) { 161 if ($flags) {
160 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE; 162 $flags = Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
161 } else { 163 } else {
162 $flags = 0; 164 $flags = 0;
163 } 165 }
164 } 166 }
165 167
166 if (func_num_args() >= 3) { 168 if (\func_num_args() >= 3) {
167 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED); 169 @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::DUMP_OBJECT flag instead.', E_USER_DEPRECATED);
168 170
169 if (func_get_arg(2)) { 171 if (func_get_arg(2)) {
170 $flags |= Yaml::DUMP_OBJECT; 172 $flags |= Yaml::DUMP_OBJECT;
171 } 173 }
172 } 174 }
173 175
174 switch (true) { 176 switch (true) {
175 case is_resource($value): 177 case \is_resource($value):
176 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) { 178 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
177 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value))); 179 throw new DumpException(sprintf('Unable to dump PHP resources in a YAML file ("%s").', get_resource_type($value)));
178 } 180 }
179 181
180 return 'null'; 182 return 'null';
181 case $value instanceof \DateTimeInterface: 183 case $value instanceof \DateTimeInterface:
182 return $value->format('c'); 184 return $value->format('c');
183 case is_object($value): 185 case \is_object($value):
184 if ($value instanceof TaggedValue) { 186 if ($value instanceof TaggedValue) {
185 return '!'.$value->getTag().' '.self::dump($value->getValue(), $flags); 187 return '!'.$value->getTag().' '.self::dump($value->getValue(), $flags);
186 } 188 }
187 189
188 if (Yaml::DUMP_OBJECT & $flags) { 190 if (Yaml::DUMP_OBJECT & $flags) {
196 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) { 198 if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
197 throw new DumpException('Object support when dumping a YAML file has been disabled.'); 199 throw new DumpException('Object support when dumping a YAML file has been disabled.');
198 } 200 }
199 201
200 return 'null'; 202 return 'null';
201 case is_array($value): 203 case \is_array($value):
202 return self::dumpArray($value, $flags); 204 return self::dumpArray($value, $flags);
203 case null === $value: 205 case null === $value:
204 return 'null'; 206 return 'null';
205 case true === $value: 207 case true === $value:
206 return 'true'; 208 return 'true';
207 case false === $value: 209 case false === $value:
208 return 'false'; 210 return 'false';
209 case ctype_digit($value): 211 case ctype_digit($value):
210 return is_string($value) ? "'$value'" : (int) $value; 212 return \is_string($value) ? "'$value'" : (int) $value;
211 case is_numeric($value): 213 case is_numeric($value):
212 $locale = setlocale(LC_NUMERIC, 0); 214 $locale = setlocale(LC_NUMERIC, 0);
213 if (false !== $locale) { 215 if (false !== $locale) {
214 setlocale(LC_NUMERIC, 'C'); 216 setlocale(LC_NUMERIC, 'C');
215 } 217 }
216 if (is_float($value)) { 218 if (\is_float($value)) {
217 $repr = (string) $value; 219 $repr = (string) $value;
218 if (is_infinite($value)) { 220 if (is_infinite($value)) {
219 $repr = str_ireplace('INF', '.Inf', $repr); 221 $repr = str_ireplace('INF', '.Inf', $repr);
220 } elseif (floor($value) == $value && $repr == $value) { 222 } elseif (floor($value) == $value && $repr == $value) {
221 // Preserve float data type since storing a whole number will result in integer value. 223 // Preserve float data type since storing a whole number will result in integer value.
222 $repr = '!!float '.$repr; 224 $repr = '!!float '.$repr;
223 } 225 }
224 } else { 226 } else {
225 $repr = is_string($value) ? "'$value'" : (string) $value; 227 $repr = \is_string($value) ? "'$value'" : (string) $value;
226 } 228 }
227 if (false !== $locale) { 229 if (false !== $locale) {
228 setlocale(LC_NUMERIC, $locale); 230 setlocale(LC_NUMERIC, $locale);
229 } 231 }
230 232
281 */ 283 */
282 private static function dumpArray($value, $flags) 284 private static function dumpArray($value, $flags)
283 { 285 {
284 // array 286 // array
285 if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) { 287 if (($value || Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE & $flags) && !self::isHash($value)) {
286 $output = array(); 288 $output = [];
287 foreach ($value as $val) { 289 foreach ($value as $val) {
288 $output[] = self::dump($val, $flags); 290 $output[] = self::dump($val, $flags);
289 } 291 }
290 292
291 return sprintf('[%s]', implode(', ', $output)); 293 return sprintf('[%s]', implode(', ', $output));
292 } 294 }
293 295
294 // hash 296 // hash
295 $output = array(); 297 $output = [];
296 foreach ($value as $key => $val) { 298 foreach ($value as $key => $val) {
297 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags)); 299 $output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
298 } 300 }
299 301
300 return sprintf('{ %s }', implode(', ', $output)); 302 return sprintf('{ %s }', implode(', ', $output));
314 * 316 *
315 * @throws ParseException When malformed inline YAML string is parsed 317 * @throws ParseException When malformed inline YAML string is parsed
316 * 318 *
317 * @internal 319 * @internal
318 */ 320 */
319 public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i = 0, $evaluate = true, $references = array(), $legacyOmittedKeySupport = false) 321 public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i = 0, $evaluate = true, $references = [], $legacyOmittedKeySupport = false)
320 { 322 {
321 if (in_array($scalar[$i], array('"', "'"))) { 323 if (\in_array($scalar[$i], ['"', "'"])) {
322 // quoted scalar 324 // quoted scalar
323 $output = self::parseQuotedScalar($scalar, $i); 325 $output = self::parseQuotedScalar($scalar, $i);
324 326
325 if (null !== $delimiters) { 327 if (null !== $delimiters) {
326 $tmp = ltrim(substr($scalar, $i), ' '); 328 $tmp = ltrim(substr($scalar, $i), ' ');
327 if ('' === $tmp) { 329 if ('' === $tmp) {
328 throw new ParseException(sprintf('Unexpected end of line, expected one of "%s".', implode($delimiters)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 330 throw new ParseException(sprintf('Unexpected end of line, expected one of "%s".', implode('', $delimiters)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
329 } 331 }
330 if (!in_array($tmp[0], $delimiters)) { 332 if (!\in_array($tmp[0], $delimiters)) {
331 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 333 throw new ParseException(sprintf('Unexpected characters (%s).', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
332 } 334 }
333 } 335 }
334 } else { 336 } else {
335 // "normal" string 337 // "normal" string
336 if (!$delimiters) { 338 if (!$delimiters) {
337 $output = substr($scalar, $i); 339 $output = substr($scalar, $i);
338 $i += strlen($output); 340 $i += \strlen($output);
339 341
340 // remove comments 342 // remove comments
341 if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) { 343 if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
342 $output = substr($output, 0, $match[0][1]); 344 $output = substr($output, 0, $match[0][1]);
343 } 345 }
344 } elseif (Parser::preg_match('/^(.'.($legacyOmittedKeySupport ? '+' : '*').'?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) { 346 } elseif (Parser::preg_match('/^(.'.($legacyOmittedKeySupport ? '+' : '*').'?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
345 $output = $match[1]; 347 $output = $match[1];
346 $i += strlen($output); 348 $i += \strlen($output);
347 } else { 349 } else {
348 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar), self::$parsedLineNumber + 1, null, self::$parsedFilename); 350 throw new ParseException(sprintf('Malformed inline YAML string: %s.', $scalar), self::$parsedLineNumber + 1, null, self::$parsedFilename);
349 } 351 }
350 352
351 // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >) 353 // a non-quoted string cannot start with @ or ` (reserved) nor with a scalar indicator (| or >)
379 { 381 {
380 if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) { 382 if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
381 throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 383 throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
382 } 384 }
383 385
384 $output = substr($match[0], 1, strlen($match[0]) - 2); 386 $output = substr($match[0], 1, \strlen($match[0]) - 2);
385 387
386 $unescaper = new Unescaper(); 388 $unescaper = new Unescaper();
387 if ('"' == $scalar[$i]) { 389 if ('"' == $scalar[$i]) {
388 $output = $unescaper->unescapeDoubleQuotedString($output); 390 $output = $unescaper->unescapeDoubleQuotedString($output);
389 } else { 391 } else {
390 $output = $unescaper->unescapeSingleQuotedString($output); 392 $output = $unescaper->unescapeSingleQuotedString($output);
391 } 393 }
392 394
393 $i += strlen($match[0]); 395 $i += \strlen($match[0]);
394 396
395 return $output; 397 return $output;
396 } 398 }
397 399
398 /** 400 /**
405 * 407 *
406 * @return array 408 * @return array
407 * 409 *
408 * @throws ParseException When malformed inline YAML string is parsed 410 * @throws ParseException When malformed inline YAML string is parsed
409 */ 411 */
410 private static function parseSequence($sequence, $flags, &$i = 0, $references = array()) 412 private static function parseSequence($sequence, $flags, &$i = 0, $references = [])
411 { 413 {
412 $output = array(); 414 $output = [];
413 $len = strlen($sequence); 415 $len = \strlen($sequence);
414 ++$i; 416 ++$i;
415 417
416 // [foo, bar, ...] 418 // [foo, bar, ...]
417 while ($i < $len) { 419 while ($i < $len) {
418 if (']' === $sequence[$i]) { 420 if (']' === $sequence[$i]) {
433 case '{': 435 case '{':
434 // nested mapping 436 // nested mapping
435 $value = self::parseMapping($sequence, $flags, $i, $references); 437 $value = self::parseMapping($sequence, $flags, $i, $references);
436 break; 438 break;
437 default: 439 default:
438 $isQuoted = in_array($sequence[$i], array('"', "'")); 440 $isQuoted = \in_array($sequence[$i], ['"', "'"]);
439 $value = self::parseScalar($sequence, $flags, array(',', ']'), $i, null === $tag, $references); 441 $value = self::parseScalar($sequence, $flags, [',', ']'], $i, null === $tag, $references);
440 442
441 // the value can be an array if a reference has been resolved to an array var 443 // the value can be an array if a reference has been resolved to an array var
442 if (is_string($value) && !$isQuoted && false !== strpos($value, ': ')) { 444 if (\is_string($value) && !$isQuoted && false !== strpos($value, ': ')) {
443 // embedded mapping? 445 // embedded mapping?
444 try { 446 try {
445 $pos = 0; 447 $pos = 0;
446 $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references); 448 $value = self::parseMapping('{'.$value.'}', $flags, $pos, $references);
447 } catch (\InvalidArgumentException $e) { 449 } catch (\InvalidArgumentException $e) {
474 * 476 *
475 * @return array|\stdClass 477 * @return array|\stdClass
476 * 478 *
477 * @throws ParseException When malformed inline YAML string is parsed 479 * @throws ParseException When malformed inline YAML string is parsed
478 */ 480 */
479 private static function parseMapping($mapping, $flags, &$i = 0, $references = array()) 481 private static function parseMapping($mapping, $flags, &$i = 0, $references = [])
480 { 482 {
481 $output = array(); 483 $output = [];
482 $len = strlen($mapping); 484 $len = \strlen($mapping);
483 ++$i; 485 ++$i;
484 $allowOverwrite = false; 486 $allowOverwrite = false;
485 487
486 // {foo: bar, bar:foo, ...} 488 // {foo: bar, bar:foo, ...}
487 while ($i < $len) { 489 while ($i < $len) {
497 499
498 return $output; 500 return $output;
499 } 501 }
500 502
501 // key 503 // key
502 $isKeyQuoted = in_array($mapping[$i], array('"', "'"), true); 504 $isKeyQuoted = \in_array($mapping[$i], ['"', "'"], true);
503 $key = self::parseScalar($mapping, $flags, array(':', ' '), $i, false, array(), true); 505 $key = self::parseScalar($mapping, $flags, [':', ' '], $i, false, [], true);
504 506
505 if (':' !== $key && false === $i = strpos($mapping, ':', $i)) { 507 if (':' !== $key && false === $i = strpos($mapping, ':', $i)) {
506 break; 508 break;
507 } 509 }
508 510
511 } 513 }
512 514
513 if (!$isKeyQuoted) { 515 if (!$isKeyQuoted) {
514 $evaluatedKey = self::evaluateScalar($key, $flags, $references); 516 $evaluatedKey = self::evaluateScalar($key, $flags, $references);
515 517
516 if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) { 518 if ('' !== $key && $evaluatedKey !== $key && !\is_string($evaluatedKey) && !\is_int($evaluatedKey)) {
517 @trigger_error(self::getDeprecationMessage('Implicit casting of incompatible mapping keys to strings is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.'), E_USER_DEPRECATED); 519 @trigger_error(self::getDeprecationMessage('Implicit casting of incompatible mapping keys to strings is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.'), E_USER_DEPRECATED);
518 } 520 }
519 } 521 }
520 522
521 if (':' !== $key && !$isKeyQuoted && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) { 523 if (':' !== $key && !$isKeyQuoted && (!isset($mapping[$i + 1]) || !\in_array($mapping[$i + 1], [' ', ',', '[', ']', '{', '}'], true))) {
522 @trigger_error(self::getDeprecationMessage('Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since Symfony 3.2 and will throw a ParseException in 4.0.'), E_USER_DEPRECATED); 524 @trigger_error(self::getDeprecationMessage('Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since Symfony 3.2 and will throw a ParseException in 4.0.'), E_USER_DEPRECATED);
523 } 525 }
524 526
525 if ('<<' === $key) { 527 if ('<<' === $key) {
526 $allowOverwrite = true; 528 $allowOverwrite = true;
574 } elseif (isset($output[$key])) { 576 } elseif (isset($output[$key])) {
575 @trigger_error(self::getDeprecationMessage(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since Symfony 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key)), E_USER_DEPRECATED); 577 @trigger_error(self::getDeprecationMessage(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since Symfony 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key)), E_USER_DEPRECATED);
576 } 578 }
577 break; 579 break;
578 default: 580 default:
579 $value = self::parseScalar($mapping, $flags, array(',', '}'), $i, null === $tag, $references); 581 $value = self::parseScalar($mapping, $flags, [',', '}'], $i, null === $tag, $references);
580 // Spec: Keys MUST be unique; first one wins. 582 // Spec: Keys MUST be unique; first one wins.
581 // Parser cannot abort this mapping earlier, since lines 583 // Parser cannot abort this mapping earlier, since lines
582 // are processed sequentially. 584 // are processed sequentially.
583 // But overwriting is allowed when a merge node is used in current block. 585 // But overwriting is allowed when a merge node is used in current block.
584 if ('<<' === $key) { 586 if ('<<' === $key) {
612 * 614 *
613 * @return mixed The evaluated YAML string 615 * @return mixed The evaluated YAML string
614 * 616 *
615 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved 617 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
616 */ 618 */
617 private static function evaluateScalar($scalar, $flags, $references = array()) 619 private static function evaluateScalar($scalar, $flags, $references = [])
618 { 620 {
619 $scalar = trim($scalar); 621 $scalar = trim($scalar);
620 $scalarLower = strtolower($scalar); 622 $scalarLower = strtolower($scalar);
621 623
622 if (0 === strpos($scalar, '*')) { 624 if (0 === strpos($scalar, '*')) {
695 return; 697 return;
696 case 0 === strpos($scalar, '!php/const:'): 698 case 0 === strpos($scalar, '!php/const:'):
697 if (self::$constantSupport) { 699 if (self::$constantSupport) {
698 @trigger_error(self::getDeprecationMessage('The !php/const: tag to indicate dumped PHP constants is deprecated since Symfony 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.'), E_USER_DEPRECATED); 700 @trigger_error(self::getDeprecationMessage('The !php/const: tag to indicate dumped PHP constants is deprecated since Symfony 3.4 and will be removed in 4.0. Use the !php/const (without the colon) tag instead.'), E_USER_DEPRECATED);
699 701
700 if (defined($const = substr($scalar, 11))) { 702 if (\defined($const = substr($scalar, 11))) {
701 return constant($const); 703 return \constant($const);
702 } 704 }
703 705
704 throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 706 throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
705 } 707 }
706 if (self::$exceptionOnInvalidType) { 708 if (self::$exceptionOnInvalidType) {
709 711
710 return; 712 return;
711 case 0 === strpos($scalar, '!php/const'): 713 case 0 === strpos($scalar, '!php/const'):
712 if (self::$constantSupport) { 714 if (self::$constantSupport) {
713 $i = 0; 715 $i = 0;
714 if (defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) { 716 if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
715 return constant($const); 717 return \constant($const);
716 } 718 }
717 719
718 throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 720 throw new ParseException(sprintf('The constant "%s" is not defined.', $const), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
719 } 721 }
720 if (self::$exceptionOnInvalidType) { 722 if (self::$exceptionOnInvalidType) {
762 case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar): 764 case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
763 if (false !== strpos($scalar, ',')) { 765 if (false !== strpos($scalar, ',')) {
764 @trigger_error(self::getDeprecationMessage('Using the comma as a group separator for floats is deprecated since Symfony 3.2 and will be removed in 4.0.'), E_USER_DEPRECATED); 766 @trigger_error(self::getDeprecationMessage('Using the comma as a group separator for floats is deprecated since Symfony 3.2 and will be removed in 4.0.'), E_USER_DEPRECATED);
765 } 767 }
766 768
767 return (float) str_replace(array(',', '_'), '', $scalar); 769 return (float) str_replace([',', '_'], '', $scalar);
768 case Parser::preg_match(self::getTimestampRegex(), $scalar): 770 case Parser::preg_match(self::getTimestampRegex(), $scalar):
769 if (Yaml::PARSE_DATETIME & $flags) { 771 if (Yaml::PARSE_DATETIME & $flags) {
770 // When no timezone is provided in the parsed date, YAML spec says we must assume UTC. 772 // When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
771 return new \DateTime($scalar, new \DateTimeZone('UTC')); 773 return new \DateTime($scalar, new \DateTimeZone('UTC'));
772 } 774 }
786 /** 788 /**
787 * @param string $value 789 * @param string $value
788 * @param int &$i 790 * @param int &$i
789 * @param int $flags 791 * @param int $flags
790 * 792 *
791 * @return null|string 793 * @return string|null
792 */ 794 */
793 private static function parseTag($value, &$i, $flags) 795 private static function parseTag($value, &$i, $flags)
794 { 796 {
795 if ('!' !== $value[$i]) { 797 if ('!' !== $value[$i]) {
796 return; 798 return;
801 803
802 $nextOffset = $i + $tagLength + 1; 804 $nextOffset = $i + $tagLength + 1;
803 $nextOffset += strspn($value, ' ', $nextOffset); 805 $nextOffset += strspn($value, ' ', $nextOffset);
804 806
805 // Is followed by a scalar 807 // Is followed by a scalar
806 if ((!isset($value[$nextOffset]) || !in_array($value[$nextOffset], array('[', '{'), true)) && 'tagged' !== $tag) { 808 if ((!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && 'tagged' !== $tag) {
807 // Manage non-whitelisted scalars in {@link self::evaluateScalar()} 809 // Manage non-whitelisted scalars in {@link self::evaluateScalar()}
808 return; 810 return;
809 } 811 }
810 812
811 // Built-in tags 813 // Built-in tags
831 */ 833 */
832 public static function evaluateBinaryScalar($scalar) 834 public static function evaluateBinaryScalar($scalar)
833 { 835 {
834 $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar)); 836 $parsedBinaryData = self::parseScalar(preg_replace('/\s/', '', $scalar));
835 837
836 if (0 !== (strlen($parsedBinaryData) % 4)) { 838 if (0 !== (\strlen($parsedBinaryData) % 4)) {
837 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)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 839 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)), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
838 } 840 }
839 841
840 if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) { 842 if (!Parser::preg_match('#^[A-Z0-9+/]+={0,2}$#i', $parsedBinaryData)) {
841 throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename); 843 throw new ParseException(sprintf('The base64 encoded data (%s) contains invalid characters.', $parsedBinaryData), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
842 } 844 }