Skip to content

Table::getDisplayLines() can output excessive lines #164

Description

@slaFFik

Bug Report

Describe the current, buggy behavior

I'm registering a table with this (simplified) code:

$table  = new \cli\Table();
$ascii  = new \cli\table\Ascii();
$widths = [
	3, // this is the longest default date format: [24-Sep-2023 20:50:51 UTC]
	3, // this is the longest default type: PHP Fatal error
	5, // trigger the overflow and last column max width by defining a very long string length
];

$ascii->setWidths( $widths );
$table->setRenderer( $ascii );

$table->setHeaders( [ 'one', 'two', 'three' ] );

$table->display();

Method Table::() returns this data:

array:4 [▶
  0 => "+----------------------------+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+"
  1 => "| One                       | Two            | Three                                                                                                                                                                                                                                                             |"
  2 => "+----------------------------+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+"
  3 => "+----------------------------+-----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+"
]

As you can see, array values for 2 and 3 are the same - bottom border.

This is because I output just the table, without rows (I plan to use ->addRow() later). And because this code in this library:

foreach ($this->_rows as $row) {
	$row = $this->_renderer->row($row);
	$row = explode( PHP_EOL, $row );
	$out = array_merge( $out, $row );
}

if (isset($border)) {
	$out[] = $border;
}

Does not check whether rows is actually not empty.

Describe what you would expect as the correct outcome

If there is a header but no rows - we should display only 1 border.

Let us know what environment you are running this on

OS:     Darwin 22.6.0 Darwin Kernel Version 22.6.0: Fri Sep 15 13:41:28 PDT 2023; root:xnu-8796.141.3.700.8~1/RELEASE_ARM64_T6000 arm64
Shell:  /bin/zsh
PHP binary:     /opt/homebrew/Cellar/php/8.2.10/bin/php
PHP version:    8.2.10
php.ini used:   /opt/homebrew/etc/php/8.2/php.ini
MySQL binary:   
MySQL version:  
SQL modes:      
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/slaffik/Projects/[redacted]
WP-CLI packages dir:    /Users/slaffik/.wp-cli/packages/
WP-CLI cache dir:       /Users/slaffik/.wp-cli/cache
WP-CLI global config:   
WP-CLI project config:  
WP-CLI version: 2.8.1

Provide a possible solution

I will create a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions