Skip to content

Fix Formatter displaying empty string for boolean false values#6162

Merged
swissspidy merged 11 commits intomainfrom
copilot/fix-formatter-empty-string-bug
Mar 12, 2026
Merged

Fix Formatter displaying empty string for boolean false values#6162
swissspidy merged 11 commits intomainfrom
copilot/fix-formatter-empty-string-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 9, 2025

The Formatter class renders boolean false as empty string in table/CSV output while true displays as "1".

Changes

Core fix: Modified transform_item_values_to_json() to explicitly convert booleans:

  • true"true" (human-readable string representation)
  • false"false" (fixes empty string bug)

Format preservation: Conversion only applies to table/CSV. JSON/YAML continue outputting native boolean types.

Documentation: Updated method docstring to reflect boolean handling.

Example

$formatter = new \WP_CLI\Formatter($assocArgs, ['id', 'status']);
$entries = [
    ['id' => 1, 'status' => true],
    ['id' => 2, 'status' => false],
];
$formatter->display_items($entries);

Before:

+----+--------+
| id | status |
+----+--------+
| 1  | 1      |
| 2  |        |
+----+--------+

After:

+----+--------+
| id | status |
+----+--------+
| 1  | true   |
| 2  | false  |
+----+--------+

JSON output unchanged: [{"id":1,"status":true},{"id":2,"status":false}]

Tests

Added Behat scenarios validating:

  • Table format displays true/false for booleans
  • JSON format preserves native boolean types
Original prompt

This section details on the original issue you should resolve

<issue_title>Formatter prints empty string for boolean with false value</issue_title>
<issue_description>## Bug Report

The following code prints the following output:

$formatter = new \WP_CLI\Formatter($assocArgs, [
    'id',
    'status',
]);

$entries = [
    [
        'id' => 1,
        'status' => true,
    ],
    [
        'id' => 2,
        'status' => false,
    ]
];

$iterator = \WP_CLI\Utils\iterator_map(
    $entries,
    function (array $entry) {
        return $entry;
    }
);

$formatter->display_items($iterator);

Output:

+----+--------+
| id | status |
+----+--------+
| 1  | 1      |
| 2  |        |
+----+--------+

I would have expected something like:

+----+--------+
| id | status |
+----+--------+
| 1  | 1      |
| 2  | 0      |
+----+--------+

or even better:

+----+--------+
| id | status |
+----+--------+
| 1  | true   |
| 2  | false  |
+----+--------+

I tried something like this to change the output, but the problem is that I would still like a boolean if I output a json with --format=json.

$iterator = \WP_CLI\Utils\iterator_map(
    $entries,
    function (array $entry) {
        $entry['status'] = $entry['status'] ? 'true' : 'false';

        return $entry;
    }
);
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 9, 2025 14:09
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
… handling

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix formatter to correctly print boolean false value Fix Formatter displaying empty string for boolean false values Dec 9, 2025
Copilot AI requested a review from swissspidy December 9, 2025 14:13
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 9, 2025

Codecov Report

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

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

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review December 19, 2025 14:08
@swissspidy swissspidy requested a review from a team as a code owner December 19, 2025 14:08
Copilot AI and others added 3 commits January 11, 2026 17:12
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy added this to the 3.0.0 milestone Jan 20, 2026

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy merged commit d9a70b5 into main Mar 12, 2026
69 checks passed
@swissspidy swissspidy deleted the copilot/fix-formatter-empty-string-bug branch March 12, 2026 09:43
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.

Formatter prints empty string for boolean with false value

3 participants