Skip to content

Commit 7f4f77a

Browse files
committed
Fixing a bug where we didn't convert properly from a scalar value to
array
1 parent 3582b1d commit 7f4f77a

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

src/Util/YamlSourceManipulator.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private function updateData(array $newData)
130130
$this->arrayTypeForDepths[$this->depth] = $this->isHash($currentData) ? self::ARRAY_TYPE_HASH : self::ARRAY_TYPE_SEQUENCE;
131131

132132
$this->log(sprintf(
133-
'Array type: %s, format: %s ',
133+
'Changing array type & format via updateData()',
134134
$this->arrayTypeForDepths[$this->depth],
135135
$this->arrayFormatForDepths[$this->depth]
136136
));
@@ -333,12 +333,7 @@ private function addNewKeyToYaml($key, $value)
333333
// because we're inside a multi-line array, put this item
334334
// onto the *next* line & indent it
335335

336-
// But, if the *value* is an array, then ITS children will
337-
// also need to be indented artificially by the same amount
338-
$newYamlValue = str_replace("\n", "\n".$this->getCurrentIndentation(), $newYamlValue);
339-
340-
// now add the new line & indentation to the top-level
341-
$newYamlValue = "\n".$this->getCurrentIndentation().$newYamlValue;
336+
$newYamlValue = "\n".$this->indentMultilineYamlArray($newYamlValue);
342337
} else {
343338
if ($firstItemInArray) {
344339
// avoid the starting "," if first item in array
@@ -454,13 +449,23 @@ private function changeValueInYaml($value)
454449

455450
$endValuePosition = $this->findEndPositionOfValue($originalVal);
456451

457-
// empty space between key & value
458-
$newDataString = ' '.$this->convertToYaml($value);
452+
$newYamlValue = $this->convertToYaml($value);
453+
if (!is_array($originalVal) && is_array($value)) {
454+
// we're converting from a scalar to a (multiline) array
455+
// this means we need to break onto the next line
456+
457+
// increase the indentation
458+
$this->manuallyIncrementIndentation();
459+
$newYamlValue = "\n".$this->indentMultilineYamlArray($newYamlValue);
460+
} else {
461+
// empty space between key & value
462+
$newYamlValue = ' '.$newYamlValue;
463+
}
459464
$newContents = substr($this->contents, 0, $this->currentPosition)
460-
.$newDataString
465+
.$newYamlValue
461466
.substr($this->contents, $endValuePosition);
462467

463-
$newPosition = $this->currentPosition + \strlen($newDataString);
468+
$newPosition = $this->currentPosition + \strlen($newYamlValue);
464469

465470
$newData = $this->currentData;
466471
$newData = $this->setValueAtCurrentPath($value, $newData);
@@ -844,6 +849,8 @@ private function log(string $message, $includeContent = false)
844849
'depth' => $this->depth,
845850
'position' => $this->currentPosition,
846851
'indentation' => $this->indentationForDepths[$this->depth],
852+
'type' => $this->arrayTypeForDepths[$this->depth],
853+
'format' => $this->arrayFormatForDepths[$this->depth],
847854
];
848855

849856
if ($includeContent) {
@@ -1123,4 +1130,21 @@ private function isCharLineBreak(string $char): bool
11231130
{
11241131
return "\n" === $char || "\r" === $char;
11251132
}
1133+
1134+
/**
1135+
* Takes an unindented multi-line YAML string and indents it so
1136+
* it can be inserted into the current position.
1137+
*
1138+
* Usually an empty line needs to be prepended to this result before
1139+
* adding to the content.
1140+
*/
1141+
private function indentMultilineYamlArray(string $yaml): string
1142+
{
1143+
// But, if the *value* is an array, then ITS children will
1144+
// also need to be indented artificially by the same amount
1145+
$yaml = str_replace("\n", "\n".$this->getCurrentIndentation(), $yaml);
1146+
1147+
// now indent this level
1148+
return $this->getCurrentIndentation().$yaml;
1149+
}
11261150
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
security:
2+
firewalls:
3+
main: ~
4+
other_firewall:
5+
security: false
6+
===
7+
$data['security']['firewalls']['main'] = [
8+
'anonymous' => true,
9+
'guard' => [
10+
'authenticators' => [
11+
'some_key' => true,
12+
'great_colors' => ['green', 'pink', 'orange'],
13+
],
14+
],
15+
];
16+
===
17+
security:
18+
firewalls:
19+
main:
20+
anonymous: true
21+
guard:
22+
authenticators:
23+
some_key: true
24+
great_colors:
25+
- green
26+
- pink
27+
- orange
28+
other_firewall:
29+
security: false
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
security:
2+
firewalls:
3+
main: ~
4+
===
5+
$data['security']['firewalls']['main'] = ['anonymous' => true];
6+
===
7+
security:
8+
firewalls:
9+
main:
10+
anonymous: true

0 commit comments

Comments
 (0)