Skip to content

Commit d9a70b5

Browse files
CopilotswissspidyCopilot
authored
Fix Formatter displaying empty string for boolean false values (#6162)
Co-authored-by: swissspidy <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Pascal Birchler <[email protected]>
1 parent b7a7a10 commit d9a70b5

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

features/formatter.feature

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,58 @@ Feature: Format output
190190
| | | mango |
191191
| 1 | bar | br |
192192

193+
Scenario: Format boolean values in table and JSON
194+
Given an empty directory
195+
And a file.php file:
196+
"""
197+
<?php
198+
$items = array(
199+
array(
200+
'id' => 1,
201+
'status' => true,
202+
),
203+
array(
204+
'id' => 2,
205+
'status' => false,
206+
),
207+
);
208+
$assoc_args = array();
209+
$formatter = new WP_CLI\Formatter( $assoc_args, array( 'id', 'status' ) );
210+
$formatter->display_items( $items );
211+
"""
212+
213+
When I run `wp eval-file file.php --skip-wordpress`
214+
Then STDOUT should be a table containing rows:
215+
| id | status |
216+
| 1 | true |
217+
| 2 | false |
218+
219+
Scenario: JSON format preserves boolean types
220+
Given an empty directory
221+
And a file.php file:
222+
"""
223+
<?php
224+
$items = array(
225+
array(
226+
'id' => 1,
227+
'status' => true,
228+
),
229+
array(
230+
'id' => 2,
231+
'status' => false,
232+
),
233+
);
234+
$assoc_args = array( 'format' => 'json' );
235+
$formatter = new WP_CLI\Formatter( $assoc_args, array( 'id', 'status' ) );
236+
$formatter->display_items( $items );
237+
"""
238+
239+
When I run `wp eval-file file.php --skip-wordpress`
240+
Then STDOUT should be JSON containing:
241+
"""
242+
[{"id":1,"status":true},{"id":2,"status":false}]
243+
"""
244+
193245
Scenario: Custom fields that exist in some items but not others
194246
Given an empty directory
195247
And a custom-fields.php file:

php/WP_CLI/Formatter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,11 @@ private function assoc_array_to_rows( $fields ) {
494494
}
495495

496496
/**
497-
* Transforms objects and arrays to JSON as necessary
497+
* Transforms item values for string-based output formats (table/CSV).
498+
*
499+
* Converts complex types to strings:
500+
* - Objects and arrays are converted to JSON strings
501+
* - Booleans are converted to "true" or "false"
498502
*
499503
* @param array|object $item
500504
* @return mixed
@@ -513,6 +517,13 @@ public function transform_item_values_to_json( $item ) {
513517
} elseif ( is_array( $item ) ) {
514518
$item[ $true_field ] = json_encode( $value );
515519
}
520+
} elseif ( is_bool( $value ) ) {
521+
// Convert boolean to string representation for table/CSV display
522+
if ( is_object( $item ) ) {
523+
$item->$true_field = $value ? 'true' : 'false';
524+
} elseif ( is_array( $item ) ) {
525+
$item[ $true_field ] = $value ? 'true' : 'false';
526+
}
516527
}
517528
}
518529
return $item;

0 commit comments

Comments
 (0)