Skip to content

Conversation

Copy link
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"1" (maintains existing behavior)
  • false"0" (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  | 1      |
| 2  | 0      |
+----+--------+

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

Tests

Added Behat scenarios validating:

  • Table format displays 1/0 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 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

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!

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.

Formatter prints empty string for boolean with false value

2 participants