Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Convert null values to empty strings
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
```
  • Loading branch information
mrsdizzie committed Mar 26, 2025
commit 854de46db7658a4c094b85c603a54aab2fe0cc80
1 change: 1 addition & 0 deletions lib/cli/table/Tabular.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function row( array $row ) {
$output = '';

foreach ( $row as $col => $value ) {
$value = isset( $value ) ? (string) $value : '';
$value = str_replace( "\t", ' ', $value );
$split_lines = preg_split( '/\r\n|\n/', $value );
// Keep anything before the first line break on the original line
Expand Down
22 changes: 22 additions & 0 deletions tests/Test_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,26 @@ public function test_preserve_trailing_tabs() {
$this->assertSame( $expected, $out, 'Trailing tabs should be preserved in table output.' );
}

public function test_null_values_are_handled() {
$table = new cli\Table();
$renderer = new cli\Table\Tabular();
$table->setRenderer( $renderer );

$table->setHeaders( array( 'Field', 'Type', 'Null', 'Key', 'Default', 'Extra' ) );

// Add row with a null value in the middle
$table->addRow( array( 'id', 'int', 'NO', 'PRI', null, 'auto_increment' ) );

// Add row with a null value at the end
$table->addRow( array( 'name', 'varchar(255)', 'YES', '', 'NULL', null ) );

$out = $table->getDisplayLines();

$expected = [
"Field\tType\tNull\tKey\tDefault\tExtra",
"id\tint\tNO\tPRI\t\tauto_increment",
"name\tvarchar(255)\tYES\t\tNULL\t",
];
$this->assertSame( $expected, $out, 'Null values should be safely converted to empty strings in table output.' );
}
}