@@ -58,7 +58,7 @@ public static function initialize(int $flags, int $parsedLineNumber = null, stri
5858 *
5959 * @throws ParseException
6060 */
61- public static function parse (string $ value = null , int $ flags = 0 , array $ references = [])
61+ public static function parse (string $ value = null , int $ flags = 0 , array & $ references = [])
6262 {
6363 self ::initialize ($ flags );
6464
@@ -267,7 +267,7 @@ private static function dumpNull(int $flags): string
267267 *
268268 * @throws ParseException When malformed inline YAML string is parsed
269269 */
270- public static function parseScalar (string $ scalar , int $ flags = 0 , array $ delimiters = null , int &$ i = 0 , bool $ evaluate = true , array $ references = [])
270+ public static function parseScalar (string $ scalar , int $ flags = 0 , array $ delimiters = null , int &$ i = 0 , bool $ evaluate = true , array & $ references = [])
271271 {
272272 if (\in_array ($ scalar [$ i ], ['" ' , "' " ])) {
273273 // quoted scalar
@@ -343,7 +343,7 @@ private static function parseQuotedScalar(string $scalar, int &$i): string
343343 *
344344 * @throws ParseException When malformed inline YAML string is parsed
345345 */
346- private static function parseSequence (string $ sequence , int $ flags , int &$ i = 0 , array $ references = []): array
346+ private static function parseSequence (string $ sequence , int $ flags , int &$ i = 0 , array & $ references = []): array
347347 {
348348 $ output = [];
349349 $ len = \strlen ($ sequence );
@@ -385,6 +385,11 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
385385 }
386386 }
387387
388+ if (\is_string ($ value ) && '' !== $ value && '& ' === $ value [0 ] && Parser::preg_match (Parser::REFERENCE_PATTERN , $ value , $ matches )) {
389+ $ references [$ matches ['ref ' ]] = $ matches ['value ' ];
390+ $ value = $ matches ['value ' ];
391+ }
392+
388393 --$ i ;
389394 }
390395
@@ -407,7 +412,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
407412 *
408413 * @throws ParseException When malformed inline YAML string is parsed
409414 */
410- private static function parseMapping (string $ mapping , int $ flags , int &$ i = 0 , array $ references = [])
415+ private static function parseMapping (string $ mapping , int $ flags , int &$ i = 0 , array & $ references = [])
411416 {
412417 $ output = [];
413418 $ len = \strlen ($ mapping );
@@ -433,14 +438,14 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
433438 // key
434439 $ offsetBeforeKeyParsing = $ i ;
435440 $ isKeyQuoted = \in_array ($ mapping [$ i ], ['" ' , "' " ], true );
436- $ key = self ::parseScalar ($ mapping , $ flags , [': ' , ' ' ], $ i , false , [] );
441+ $ key = self ::parseScalar ($ mapping , $ flags , [': ' , ' ' ], $ i , false );
437442
438443 if ($ offsetBeforeKeyParsing === $ i ) {
439444 throw new ParseException ('Missing mapping key. ' , self ::$ parsedLineNumber + 1 , $ mapping );
440445 }
441446
442447 if ('!php/const ' === $ key ) {
443- $ key .= ' ' .self ::parseScalar ($ mapping , $ flags , [': ' ], $ i , false , [] );
448+ $ key .= ' ' .self ::parseScalar ($ mapping , $ flags , [': ' ], $ i , false );
444449 $ key = self ::evaluateScalar ($ key , $ flags );
445450 }
446451
@@ -522,6 +527,11 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
522527 if ('<< ' === $ key ) {
523528 $ output += $ value ;
524529 } elseif ($ allowOverwrite || !isset ($ output [$ key ])) {
530+ if (\is_string ($ value ) && '' !== $ value && '& ' === $ value [0 ] && Parser::preg_match (Parser::REFERENCE_PATTERN , $ value , $ matches )) {
531+ $ references [$ matches ['ref ' ]] = $ matches ['value ' ];
532+ $ value = $ matches ['value ' ];
533+ }
534+
525535 if (null !== $ tag ) {
526536 $ output [$ key ] = new TaggedValue ($ tag , $ value );
527537 } else {
@@ -548,7 +558,7 @@ private static function parseMapping(string $mapping, int $flags, int &$i = 0, a
548558 *
549559 * @throws ParseException when object parsing support was disabled and the parser detected a PHP object or when a reference could not be resolved
550560 */
551- private static function evaluateScalar (string $ scalar , int $ flags , array $ references = [])
561+ private static function evaluateScalar (string $ scalar , int $ flags , array & $ references = [])
552562 {
553563 $ scalar = trim ($ scalar );
554564 $ scalarLower = strtolower ($ scalar );
0 commit comments