Skip to content

Commit 03ef6cf

Browse files
authored
[Console] Print composer-based rule configuration on separate full-width line (#8256)
1 parent 104c4d6 commit 03ef6cf

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

‎src/Console/Command/ComposerBasedCommand.php‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration;
1515
use ReflectionObject;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Helper\TableCell;
18+
use Symfony\Component\Console\Helper\TableSeparator;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Output\OutputInterface;
1921
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -58,10 +60,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5860

5961
if ($configurationTableRows !== []) {
6062
$this->symfonyStyle->title('Composer package bound rule configuration');
61-
$this->symfonyStyle->table(
62-
['Rule', 'Package', 'Requires', 'Installed', 'Active', 'Configuration'],
63-
$configurationTableRows
64-
);
63+
64+
$headers = ['Rule', 'Package', 'Requires', 'Installed', 'Active'];
65+
66+
$table = $this->symfonyStyle->createTable();
67+
$table->setHeaders($headers);
68+
$table->setRows($this->createConfigurationDisplayRows($configurationTableRows));
69+
70+
// keep columns as narrow as their content, so the full-width configuration line does not stretch them
71+
foreach ($headers as $columnPosition => $header) {
72+
$columnWidth = max(array_map(
73+
static fn (array $configurationTableRow): int => strlen($configurationTableRow[$columnPosition]),
74+
$configurationTableRows
75+
));
76+
77+
$table->setColumnMaxWidth($columnPosition, max($columnWidth, strlen($header)));
78+
}
79+
80+
$table->render();
6581
}
6682

6783
$allTableRows = [...$tableRows, ...$configurationTableRows];
@@ -143,6 +159,30 @@ private function createConfigurationTableRows(): array
143159
return $tableRows;
144160
}
145161

162+
/**
163+
* The configuration is printed on a separated full-width line, to avoid breaking the table layout
164+
*
165+
* @param array<array{string, string, string, string, string, string}> $configurationTableRows
166+
* @return array<array<TableCell|string>|TableSeparator>
167+
*/
168+
private function createConfigurationDisplayRows(array $configurationTableRows): array
169+
{
170+
$displayRows = [];
171+
172+
foreach ($configurationTableRows as $configurationTableRow) {
173+
if ($displayRows !== []) {
174+
$displayRows[] = new TableSeparator();
175+
}
176+
177+
$displayRows[] = array_slice($configurationTableRow, 0, 5);
178+
$displayRows[] = [new TableCell($configurationTableRow[5], [
179+
'colspan' => 5,
180+
])];
181+
}
182+
183+
return $displayRows;
184+
}
185+
146186
/**
147187
* @param mixed[] $configuration
148188
*/

0 commit comments

Comments
 (0)