Skip to content

Conversation

@AdityaS62
Copy link

@AdityaS62 AdityaS62 commented Jan 11, 2026

Summary

When formatting output as YAML, falsey scalar values such as 0 or "0" could be incorrectly handled, resulting in missing or invalid output. In some cases, this caused YAML formatting to fail instead of displaying the expected value.

What This Change Does

This update ensures scalar values are normalized before being passed to the YAML serializer so that valid zero values are preserved correctly.

Specifically:

Detects scalar zero values (0 and "0") before YAML serialization

Outputs zero values directly instead of passing them to the YAML serializer

Prevents exceptions caused by YAML serialization of scalar zero values

Scope

Applies only to YAML output

Does not affect JSON, CSV, table, or other formats

Preserves existing behavior for non-scalar values

Rationale

Zero (0) is a meaningful value in CLI output and should not be treated as empty or omitted.
This change ensures correctness and consistency in YAML output without altering broader formatting behavior or modifying the underlying YAML library.

Testing

Verified locally using Behat formatter tests

Confirmed that YAML output preserves scalar zero values correctly

No regressions observed in other output formats

Example

Before

$ wp option get test_zero --format=yaml

(Exception)

After

$ wp option get test_zero --format=yaml
0

@AdityaS62 AdityaS62 requested a review from a team as a code owner January 11, 2026 16:47
@github-actions

This comment was marked as resolved.

@gemini-code-assist

This comment was marked as resolved.

@codecov
Copy link

codecov bot commented Jan 11, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/Formatter.php 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@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 addresses the issue of preserving '0' values in YAML output by explicitly casting them to strings. The logic is sound and well-contained. My main feedback concerns coding style, specifically inconsistent indentation in the new code block. I've left a couple of specific comments with suggestions to fix it. Additionally, to prevent future regressions, I recommend adding a new test case to features/formatter.feature that verifies this fix for zero values in YAML output.

@swissspidy
Copy link
Member

Hi there,

Thanks for your PR! Could you perhaps add a test to demonstrate the issue this aims to solve?

@AdityaS62
Copy link
Author

Hi! Thanks for the review 🙂
Yes, that makes sense — I’ll add a test to demonstrate the YAML output behavior for "0" values and update the PR shortly.

@swissspidy
Copy link
Member

@AdityaS62 I see you're doing lots of experimenting. Are you running tests locally before pushing changes? That is a much faster feedback loop.

@AdityaS62
Copy link
Author

@AdityaS62 I see you're doing lots of experimenting. Are you running tests locally before pushing changes? That is a much faster feedback loop.

Thanks for the feedback! You’re right — I did a fair bit of experimentation while narrowing down the correct fix.

I’m setting up and running the relevant tests locally now to tighten the feedback loop before pushing further changes. Appreciate the guidance.

@AdityaS62
Copy link
Author

@swissspidy Thanks for the suggestion! You’re right — running tests locally first would have saved some iteration time. I’ve since been running PHPCS and the relevant Behat scenarios locally before pushing, which helped stabilize the changes quickly.

All unit tests, functional tests, and Behat scenarios now pass both locally and in CI.

You may notice the Codecov check reporting 0% of diff hit, this appears to be due to coverage not being uploaded for this test matrix rather than missing test coverage. The behavior is fully covered by the existing Behat scenario and passes consistently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants