diff --git a/src/Console/Command/ComposerBasedCommand.php b/src/Console/Command/ComposerBasedCommand.php index 0418cc8030f..5ecb8c52153 100644 --- a/src/Console/Command/ComposerBasedCommand.php +++ b/src/Console/Command/ComposerBasedCommand.php @@ -14,6 +14,8 @@ use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration; use ReflectionObject; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\TableCell; +use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -58,10 +60,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($configurationTableRows !== []) { $this->symfonyStyle->title('Composer package bound rule configuration'); - $this->symfonyStyle->table( - ['Rule', 'Package', 'Requires', 'Installed', 'Active', 'Configuration'], - $configurationTableRows - ); + + $headers = ['Rule', 'Package', 'Requires', 'Installed', 'Active']; + + $table = $this->symfonyStyle->createTable(); + $table->setHeaders($headers); + $table->setRows($this->createConfigurationDisplayRows($configurationTableRows)); + + // keep columns as narrow as their content, so the full-width configuration line does not stretch them + foreach ($headers as $columnPosition => $header) { + $columnWidth = max(array_map( + static fn (array $configurationTableRow): int => strlen($configurationTableRow[$columnPosition]), + $configurationTableRows + )); + + $table->setColumnMaxWidth($columnPosition, max($columnWidth, strlen($header))); + } + + $table->render(); } $allTableRows = [...$tableRows, ...$configurationTableRows]; @@ -143,6 +159,30 @@ private function createConfigurationTableRows(): array return $tableRows; } + /** + * The configuration is printed on a separated full-width line, to avoid breaking the table layout + * + * @param array $configurationTableRows + * @return array|TableSeparator> + */ + private function createConfigurationDisplayRows(array $configurationTableRows): array + { + $displayRows = []; + + foreach ($configurationTableRows as $configurationTableRow) { + if ($displayRows !== []) { + $displayRows[] = new TableSeparator(); + } + + $displayRows[] = array_slice($configurationTableRow, 0, 5); + $displayRows[] = [new TableCell($configurationTableRow[5], [ + 'colspan' => 5, + ])]; + } + + return $displayRows; + } + /** * @param mixed[] $configuration */