Skip to content

Commit 77d0c0a

Browse files
committed
Fix YAML dump incorrectly handling zero values
In PHP, empty(0) and empty("0") return true, which caused the YAML dumper to incorrectly treat zero values as empty. This resulted in: - Arrays containing 0 being serialized without the zero value - Scalar zero values being omitted from YAML output Changes: - dump(): Replace 'if ($array)' with explicit null/empty checks to properly handle arrays that may contain zero values - _yamlize(): Replace 'empty($value)' with 'count($value) === 0' to only treat actually empty arrays as empty, not zero values Added test cases: - testDumpIntegerZero: Verify integer 0 is correctly dumped - testDumpStringZero: Verify string '0' is correctly dumped - testDumpAssociativeZero: Verify zero values in associative arrays - testDumpMixedWithZero: Verify mixed arrays containing zero Fixes wp-cli/wp-cli#6188
1 parent 17b6825 commit 77d0c0a

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Spyc.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
211211
if (!$no_opening_dashes) $string = "---\n";
212212

213213
// Start at the base of the array and move through it.
214-
if ($array) {
214+
// Note: We use !== null check to properly handle arrays containing zero values
215+
// since `if ($array)` would incorrectly skip processing for falsey values like 0.
216+
if ($array !== null && $array !== '' && (!is_array($array) || count($array) > 0)) {
215217
$array = (array)$array;
216218
$previous_key = -1;
217219
foreach ($array as $key => $value) {
@@ -234,7 +236,9 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
234236
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
235237
if(is_object($value)) $value = (array)$value;
236238
if (is_array($value)) {
237-
if (empty ($value))
239+
// Note: We use explicit count check instead of empty() because empty(0) and empty("0")
240+
// return true in PHP, which would incorrectly treat zero values as empty arrays.
241+
if (count($value) === 0)
238242
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
239243
// It has children. What to do?
240244
// Make it the right kind of item

tests/DumpTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,43 @@ public function testPerCentAndDoubleQuote() {
193193
$this->assertEquals ($awaiting, $dump);
194194
}
195195

196+
/**
197+
* Test that integer zero values are correctly dumped.
198+
* This is a regression test for the issue where empty(0) returns true in PHP,
199+
* causing zero values to be incorrectly treated as empty.
200+
*/
201+
public function testDumpIntegerZero() {
202+
$dump = Spyc::YAMLDump(array(0));
203+
$awaiting = "---\n- 0\n";
204+
$this->assertEquals($awaiting, $dump);
205+
}
206+
207+
/**
208+
* Test that string zero values are correctly dumped.
209+
* This is a regression test for the issue where empty("0") returns true in PHP.
210+
*/
211+
public function testDumpStringZero() {
212+
$dump = Spyc::YAMLDump(array('0'));
213+
$awaiting = "---\n- \"0\"\n";
214+
$this->assertEquals($awaiting, $dump);
215+
}
216+
217+
/**
218+
* Test that associative arrays with zero values are correctly dumped.
219+
*/
220+
public function testDumpAssociativeZero() {
221+
$dump = Spyc::YAMLDump(array('key' => 0));
222+
$awaiting = "---\nkey: 0\n";
223+
$this->assertEquals($awaiting, $dump);
224+
}
225+
226+
/**
227+
* Test that mixed arrays containing zero values are correctly dumped.
228+
*/
229+
public function testDumpMixedWithZero() {
230+
$dump = Spyc::YAMLDump(array(1, 0, 2));
231+
$awaiting = "---\n- 1\n- 0\n- 2\n";
232+
$this->assertEquals($awaiting, $dump);
233+
}
234+
196235
}

0 commit comments

Comments
 (0)