Skip to content

Commit

Permalink
minor #48163 [Yaml] Extract duplicate code to private function (alami…
Browse files Browse the repository at this point in the history
…rault)

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
  • Loading branch information
nicolas-grekas committed Nov 9, 2022
2 parents fec1de7 + 6026422 commit adbf678
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ public static function dump(mixed $value, int $flags = 0): string
}

if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
$output = [];

foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
}

return sprintf('{ %s }', implode(', ', $output));
return self::dumpHashArray($value, $flags);
}

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

// hash
return self::dumpHashArray($value, $flags);
}

/**
* Dumps hash array to a YAML string.
*
* @param array|\ArrayObject|\stdClass $value The hash array to dump
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
*/
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
{
$output = [];
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
Expand Down

0 comments on commit adbf678

Please sign in to comment.