Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Spyc.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
if (!$no_opening_dashes) $string = "---\n";

// Start at the base of the array and move through it.
if ($array) {
// Note: We avoid a loose `if ($array)` check because it would treat falsey scalars
// (e.g., 0, "0", false) as empty input and skip dumping them, even though they are
// valid values. Non-empty arrays are always truthy in PHP regardless of contents,
// so this guard is about distinguishing "no data" (null, "", empty array) from real values.
if ($array !== null && $array !== '' && (!is_array($array) || count($array) > 0)) {
$array = (array)$array;
Comment on lines +218 to 219
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition still treats an empty string ('') as “empty” and skips dumping it. That contradicts the PR description (“restrict empty handling to null and truly empty arrays”), and it means top-level empty-string scalars can’t be serialized even though nested empty strings are supported. Consider removing the !== '' check (and add/adjust tests if you intentionally want '' to be skipped).

Copilot uses AI. Check for mistakes.
$previous_key = -1;
foreach ($array as $key => $value) {
Expand All @@ -234,7 +238,9 @@ public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashe
private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) {
if(is_object($value)) $value = (array)$value;
if (is_array($value)) {
if (empty ($value))
// Since $value is already an array, empty($value) and count($value) === 0 are equivalent.
// We use count($value) === 0 here for explicitness and consistency with the logic in dump().
if (count($value) === 0)
return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array);
// It has children. What to do?
// Make it the right kind of item
Expand Down
39 changes: 39 additions & 0 deletions tests/DumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,43 @@ public function testPerCentAndDoubleQuote() {
$this->assertEquals ($awaiting, $dump);
}

/**
* Test that integer zero values are correctly dumped.
* This is a regression test for the issue where empty(0) returns true in PHP,
* causing zero values to be incorrectly treated as empty.
*/
public function testDumpIntegerZero() {
$dump = Spyc::YAMLDump(array(0));
$awaiting = "---\n- 0\n";
$this->assertEquals($awaiting, $dump);
}

/**
* Test that string zero values are correctly dumped.
* This is a regression test for the issue where empty("0") returns true in PHP.
*/
public function testDumpStringZero() {
$dump = Spyc::YAMLDump(array('0'));
$awaiting = "---\n- \"0\"\n";
$this->assertEquals($awaiting, $dump);
}
Comment on lines +201 to +215
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new tests don’t exercise the behavior changed in Spyc::dump(): arrays like array(0) / array('0') were already truthy and would have been dumped before this PR. To validate the fix, add assertions for dumping falsey scalars at the top level (e.g. Spyc::YAMLDump(0) and Spyc::YAMLDump('0')), and/or include a case that demonstrably failed prior to the change.

Copilot uses AI. Check for mistakes.

/**
* Test that associative arrays with zero values are correctly dumped.
*/
public function testDumpAssociativeZero() {
$dump = Spyc::YAMLDump(array('key' => 0));
$awaiting = "---\nkey: 0\n";
$this->assertEquals($awaiting, $dump);
}

/**
* Test that mixed arrays containing zero values are correctly dumped.
*/
public function testDumpMixedWithZero() {
$dump = Spyc::YAMLDump(array(1, 0, 2));
$awaiting = "---\n- 1\n- 0\n- 2\n";
$this->assertEquals($awaiting, $dump);
}

}