Skip to content

Commit 34b83b4

Browse files
authored
Convert null values to empty strings (#185)
To avoid deprecation warnings in newer PHP: ``` PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /Users/isla/source/wp-cli-dev/php-cli-tools/lib/cli/table/Tabular.php on line 30 ```
1 parent fbb9c6e commit 34b83b4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/cli/table/Tabular.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function row( array $row ) {
2727
$output = '';
2828

2929
foreach ( $row as $col => $value ) {
30+
$value = isset( $value ) ? (string) $value : '';
3031
$value = str_replace( "\t", ' ', $value );
3132
$split_lines = preg_split( '/\r\n|\n/', $value );
3233
// Keep anything before the first line break on the original line

tests/Test_Table.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,26 @@ public function test_preserve_trailing_tabs() {
267267
$this->assertSame( $expected, $out, 'Trailing tabs should be preserved in table output.' );
268268
}
269269

270+
public function test_null_values_are_handled() {
271+
$table = new cli\Table();
272+
$renderer = new cli\Table\Tabular();
273+
$table->setRenderer( $renderer );
274+
275+
$table->setHeaders( array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' ) );
276+
277+
// Add row with a null value in the middle
278+
$table->addRow( array( 'id', 'int', 'NO', 'PRI', null, 'auto_increment' ) );
279+
280+
// Add row with a null value at the end
281+
$table->addRow( array( 'name', 'varchar(255)', 'YES', '', 'NULL', null ) );
282+
283+
$out = $table->getDisplayLines();
284+
285+
$expected = [
286+
"Field\tType\tNull\tKey\tDefault\tExtra",
287+
"id\tint\tNO\tPRI\t\tauto_increment",
288+
"name\tvarchar(255)\tYES\t\tNULL\t",
289+
];
290+
$this->assertSame( $expected, $out, 'Null values should be safely converted to empty strings in table output.' );
291+
}
270292
}

0 commit comments

Comments
 (0)