Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adbf678

Browse files
minor #48163 [Yaml] Extract duplicate code to private function (alamirault)
This PR was merged into the 6.2 branch. Discussion ---------- [Yaml] Extract duplicate code to private function | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> I saw the same code/logic was duplicated in `Inline` class while working on #48127. This little patch can avoid bug/inconsistency Commits ------- 6026422 [Yaml] Extract duplicate code to private function
2 parents fec1de7 + 6026422 commit adbf678

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,7 @@ public static function dump(mixed $value, int $flags = 0): string
123123
}
124124

125125
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
126-
$output = [];
127-
128-
foreach ($value as $key => $val) {
129-
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
130-
}
131-
132-
return sprintf('{ %s }', implode(', ', $output));
126+
return self::dumpHashArray($value, $flags);
133127
}
134128

135129
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -232,7 +226,17 @@ private static function dumpArray(array $value, int $flags): string
232226
return sprintf('[%s]', implode(', ', $output));
233227
}
234228

235-
// hash
229+
return self::dumpHashArray($value, $flags);
230+
}
231+
232+
/**
233+
* Dumps hash array to a YAML string.
234+
*
235+
* @param array|\ArrayObject|\stdClass $value The hash array to dump
236+
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
237+
*/
238+
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
239+
{
236240
$output = [];
237241
foreach ($value as $key => $val) {
238242
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));

0 commit comments

Comments
 (0)