Skip to content

Commit c161e05

Browse files
Merge branch '5.4' into 6.2
* 5.4: trim(): Argument #1 () must be of type string, bool given [Dumper] Trim leading newlines when checking if value begins with a space Fix the list of supported shells for completions in a phar
2 parents 7743777 + 176d1de commit c161e05

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

src/Symfony/Component/Console/Command/DumpCompletionCommand.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,18 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
143143
*/
144144
private function getSupportedShells(): array
145145
{
146-
return $this->supportedShells ??= array_map(function ($f) {
147-
return pathinfo($f, \PATHINFO_EXTENSION);
148-
}, glob(__DIR__.'/../Resources/completion.*'));
146+
if (null !== $this->supportedShells) {
147+
return $this->supportedShells;
148+
}
149+
150+
$shells = [];
151+
152+
foreach (new \DirectoryIterator(__DIR__.'/../Resources/') as $file) {
153+
if (str_starts_with($file->getBasename(), 'completion.') && $file->isFile()) {
154+
$shells[] = $file->getExtension();
155+
}
156+
}
157+
158+
return $this->supportedShells = $shells;
149159
}
150160
}

src/Symfony/Component/Console/Terminal.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public static function hasSttyAvailable(): bool
136136
private static function initDimensions()
137137
{
138138
if ('\\' === \DIRECTORY_SEPARATOR) {
139-
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
139+
$ansicon = getenv('ANSICON');
140+
if (false !== $ansicon && preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim($ansicon), $matches)) {
140141
// extract [w, H] from "wxh (WxH)"
141142
// or [w, h] from "wxh"
142143
self::$width = (int) $matches[1];

src/Symfony/Component/Yaml/Dumper.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
6767
}
6868

6969
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && str_contains($value, "\n") && !str_contains($value, "\r")) {
70-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
71-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
72-
$blockIndentationIndicator = str_starts_with($value, ' ') ? (string) $this->indentation : '';
70+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value);
7371

7472
if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
7573
$blockChompingIndicator = '+';
@@ -96,9 +94,7 @@ public function dump(mixed $input, int $inline = 0, int $indent = 0, int $flags
9694
$output .= sprintf('%s%s !%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', $value->getTag());
9795

9896
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
99-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
100-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
101-
$blockIndentationIndicator = str_starts_with($value->getValue(), ' ') ? (string) $this->indentation : '';
97+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
10298
$output .= sprintf(' |%s', $blockIndentationIndicator);
10399

104100
foreach (explode("\n", $value->getValue()) as $row) {
@@ -143,9 +139,7 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
143139
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
144140

145141
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && str_contains($value->getValue(), "\n") && !str_contains($value->getValue(), "\r\n")) {
146-
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
147-
// http://www.yaml.org/spec/1.2/spec.html#id2793979
148-
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
142+
$blockIndentationIndicator = $this->getBlockIndentationIndicator($value->getValue());
149143
$output .= sprintf(' |%s', $blockIndentationIndicator);
150144

151145
foreach (explode("\n", $value->getValue()) as $row) {
@@ -161,4 +155,20 @@ private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, i
161155

162156
return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
163157
}
158+
159+
private function getBlockIndentationIndicator(string $value): string
160+
{
161+
$lines = explode("\n", $value);
162+
163+
// If the first line (that is neither empty nor contains only spaces)
164+
// starts with a space character, the spec requires a block indentation indicator
165+
// http://www.yaml.org/spec/1.2/spec.html#id2793979
166+
foreach ($lines as $line) {
167+
if ('' !== trim($line, ' ')) {
168+
return (' ' === substr($line, 0, 1)) ? (string) $this->indentation : '';
169+
}
170+
}
171+
172+
return '';
173+
}
164174
}

src/Symfony/Component/Yaml/Tests/DumperTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,42 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
710710
$this->assertSame($data, $this->parser->parse($yml));
711711
}
712712

713+
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineIsEmptyAndSecondLineHasLeadingSpace()
714+
{
715+
$data = [
716+
'data' => [
717+
'multi_line' => "\n the second line has leading spaces\nThe third line does not.",
718+
],
719+
];
720+
721+
$expected = "data:\n multi_line: |4-\n\n the second line has leading spaces\n The third line does not.";
722+
723+
$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
724+
$this->assertSame($expected, $yml);
725+
$this->assertSame($data, $this->parser->parse($yml));
726+
}
727+
728+
public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasOnlySpaces()
729+
{
730+
$data = [
731+
'data' => [
732+
'multi_line' => " \nthe second line\nThe third line.",
733+
],
734+
];
735+
736+
$expectedData = [
737+
'data' => [
738+
'multi_line' => "\nthe second line\nThe third line.",
739+
],
740+
];
741+
742+
$expectedYml = "data:\n multi_line: |-\n \n the second line\n The third line.";
743+
744+
$yml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
745+
$this->assertSame($expectedYml, $yml);
746+
$this->assertSame($expectedData, $this->parser->parse($yml));
747+
}
748+
713749
public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
714750
{
715751
$data = ["a\r\nb\nc"];

0 commit comments

Comments
 (0)