Skip to content

Conversation

@AdityaS62
Copy link

Summary

This PR fixes an issue where YAML serialization incorrectly drops or misrepresents zero (0 or "0") values.
Because PHP treats 0 and "0" as empty in certain contexts, Spyc was mistakenly omitting valid scalar zero values during YAML output.

Root Cause

In PHP:

empty( 0 ) === true
empty( "0" ) === true

Spyc relied on empty() checks when determining whether a value or array should be serialized.
As a result:

Scalar zero values were treated as empty

Arrays containing zero values could be serialized without those values

YAML output became ambiguous or incomplete

Changes Made

Logic fixes

Replaced empty() checks with explicit, value-safe conditions

Ensured zero (0 / "0") is treated as a valid scalar, not as empty

Restricted “empty” handling to:

null

truly empty arrays (count($value) === 0)

Affected methods

dump(): avoids treating arrays containing zero values as empty

_yamlize(): distinguishes zero values from empty values during serialization

Tests Added

New tests cover zero handling in multiple scenarios:

Integer zero (0)

String zero ("0")

Associative arrays containing zero values

Mixed arrays containing zero values

These tests ensure zero values are preserved correctly across YAML output.

Why This Approach

Fixes the issue at the serialization layer, where the behavior originates

Avoids altering consumer code (e.g. wp-cli formatters)

Preserves backward compatibility for non-zero values

Aligns YAML output with user expectations and CLI correctness

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
@gemini-code-assist
Copy link

Summary of Changes

Hello @AdityaS62, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the Spyc YAML serialization library where valid zero values, both integer '0' and string '0', were erroneously dropped from the YAML output. The fix involves refining the internal logic to distinguish actual empty states from legitimate zero values, ensuring that all data is accurately represented in the generated YAML. This change improves data integrity and aligns the library's behavior with expected YAML standards.

Highlights

  • Corrected YAML Serialization of Zero Values: Fixed an issue where the Spyc library incorrectly omitted or misrepresented integer '0' and string '0' values during YAML serialization due to PHP's 'empty()' function treating them as empty.
  • Logic Refinements in dump() and _yamlize(): Replaced empty() checks with explicit, value-safe conditions in the dump() and _yamlize() methods to ensure zero values are treated as valid scalars and not dropped.
  • Enhanced Test Coverage: Added new unit tests to specifically cover various scenarios involving zero values, including integer '0', string '0', and their presence in associative and mixed arrays, to prevent future regressions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves an issue where zero values (0 and "0") were incorrectly handled during YAML serialization due to PHP's empty() behavior. The changes in the dump and _yamlize methods correctly ensure that zero is treated as a valid scalar value. The addition of specific regression tests for various zero-value scenarios is excellent and provides strong verification for the fix. I have one minor suggestion regarding a comment in _yamlize for improved clarity.

This comment was marked as resolved.

- dump() method: Updated comment to clarify the fix handles both:
  - Scalar zero values passed directly (dump(0) or dump("0"))
  - Arrays containing zero values (dump([0]))

- _yamlize() method: Updated comment to clarify this is a stylistic
  consistency change, since at this point  is already verified
  to be an array (not a scalar zero)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant