fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -36,69 +36,27 @@ class Table
private const BORDER_OUTSIDE = 0;
private const BORDER_INSIDE = 1;
private $headerTitle;
private $footerTitle;
/**
* Table headers.
*/
private $headers = [];
/**
* Table rows.
*/
private $rows = [];
private $horizontal = false;
/**
* Column widths cache.
*/
private $effectiveColumnWidths = [];
/**
* Number of columns cache.
*
* @var int
*/
private $numberOfColumns;
/**
* @var OutputInterface
*/
private ?string $headerTitle = null;
private ?string $footerTitle = null;
private array $headers = [];
private array $rows = [];
private bool $horizontal = false;
private array $effectiveColumnWidths = [];
private int $numberOfColumns;
private $output;
/**
* @var TableStyle
*/
private $style;
private array $columnStyles = [];
private array $columnWidths = [];
private array $columnMaxWidths = [];
private bool $rendered = false;
/**
* @var array
*/
private $columnStyles = [];
/**
* User set column widths.
*
* @var array
*/
private $columnWidths = [];
private $columnMaxWidths = [];
/**
* @var array<string, TableStyle>|null
*/
private static $styles;
private $rendered = false;
private static array $styles;
public function __construct(OutputInterface $output)
{
$this->output = $output;
if (!self::$styles) {
self::$styles = self::initStyles();
}
self::$styles ??= self::initStyles();
$this->setStyle('default');
}
@@ -108,39 +66,27 @@ class Table
*/
public static function setStyleDefinition(string $name, TableStyle $style)
{
if (!self::$styles) {
self::$styles = self::initStyles();
}
self::$styles ??= self::initStyles();
self::$styles[$name] = $style;
}
/**
* Gets a style definition by name.
*
* @return TableStyle
*/
public static function getStyleDefinition(string $name)
public static function getStyleDefinition(string $name): TableStyle
{
if (!self::$styles) {
self::$styles = self::initStyles();
}
self::$styles ??= self::initStyles();
if (isset(self::$styles[$name])) {
return self::$styles[$name];
}
throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
return self::$styles[$name] ?? throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
}
/**
* Sets table style.
*
* @param TableStyle|string $name The style name or a TableStyle instance
*
* @return $this
*/
public function setStyle($name)
public function setStyle(TableStyle|string $name): static
{
$this->style = $this->resolveStyle($name);
@@ -149,10 +95,8 @@ class Table
/**
* Gets the current table style.
*
* @return TableStyle
*/
public function getStyle()
public function getStyle(): TableStyle
{
return $this->style;
}
@@ -164,7 +108,7 @@ class Table
*
* @return $this
*/
public function setColumnStyle(int $columnIndex, $name)
public function setColumnStyle(int $columnIndex, TableStyle|string $name): static
{
$this->columnStyles[$columnIndex] = $this->resolveStyle($name);
@@ -175,10 +119,8 @@ class Table
* Gets the current style for a column.
*
* If style was not set, it returns the global table style.
*
* @return TableStyle
*/
public function getColumnStyle(int $columnIndex)
public function getColumnStyle(int $columnIndex): TableStyle
{
return $this->columnStyles[$columnIndex] ?? $this->getStyle();
}
@@ -188,7 +130,7 @@ class Table
*
* @return $this
*/
public function setColumnWidth(int $columnIndex, int $width)
public function setColumnWidth(int $columnIndex, int $width): static
{
$this->columnWidths[$columnIndex] = $width;
@@ -200,7 +142,7 @@ class Table
*
* @return $this
*/
public function setColumnWidths(array $widths)
public function setColumnWidths(array $widths): static
{
$this->columnWidths = [];
foreach ($widths as $index => $width) {
@@ -218,7 +160,7 @@ class Table
*
* @return $this
*/
public function setColumnMaxWidth(int $columnIndex, int $width): self
public function setColumnMaxWidth(int $columnIndex, int $width): static
{
if (!$this->output->getFormatter() instanceof WrappableOutputFormatterInterface) {
throw new \LogicException(sprintf('Setting a maximum column width is only supported when using a "%s" formatter, got "%s".', WrappableOutputFormatterInterface::class, get_debug_type($this->output->getFormatter())));
@@ -232,7 +174,7 @@ class Table
/**
* @return $this
*/
public function setHeaders(array $headers)
public function setHeaders(array $headers): static
{
$headers = array_values($headers);
if (!empty($headers) && !\is_array($headers[0])) {
@@ -254,7 +196,7 @@ class Table
/**
* @return $this
*/
public function addRows(array $rows)
public function addRows(array $rows): static
{
foreach ($rows as $row) {
$this->addRow($row);
@@ -266,7 +208,7 @@ class Table
/**
* @return $this
*/
public function addRow($row)
public function addRow(TableSeparator|array $row): static
{
if ($row instanceof TableSeparator) {
$this->rows[] = $row;
@@ -274,10 +216,6 @@ class Table
return $this;
}
if (!\is_array($row)) {
throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.');
}
$this->rows[] = array_values($row);
return $this;
@@ -288,7 +226,7 @@ class Table
*
* @return $this
*/
public function appendRow($row): self
public function appendRow(TableSeparator|array $row): static
{
if (!$this->output instanceof ConsoleSectionOutput) {
throw new RuntimeException(sprintf('Output should be an instance of "%s" when calling "%s".', ConsoleSectionOutput::class, __METHOD__));
@@ -307,7 +245,7 @@ class Table
/**
* @return $this
*/
public function setRow($column, array $row)
public function setRow(int|string $column, array $row): static
{
$this->rows[$column] = $row;
@@ -317,7 +255,7 @@ class Table
/**
* @return $this
*/
public function setHeaderTitle(?string $title): self
public function setHeaderTitle(?string $title): static
{
$this->headerTitle = $title;
@@ -327,7 +265,7 @@ class Table
/**
* @return $this
*/
public function setFooterTitle(?string $title): self
public function setFooterTitle(?string $title): static
{
$this->footerTitle = $title;
@@ -337,7 +275,7 @@ class Table
/**
* @return $this
*/
public function setHorizontal(bool $horizontal = true): self
public function setHorizontal(bool $horizontal = true): static
{
$this->horizontal = $horizontal;
@@ -678,7 +616,7 @@ class Table
{
$unmergedRows = [];
foreach ($rows[$line] as $column => $cell) {
if (null !== $cell && !$cell instanceof TableCell && !\is_scalar($cell) && !(\is_object($cell) && method_exists($cell, '__toString'))) {
if (null !== $cell && !$cell instanceof TableCell && !\is_scalar($cell) && !$cell instanceof \Stringable) {
throw new InvalidArgumentException(sprintf('A cell must be a TableCell, a scalar or an object implementing "__toString()", "%s" given.', get_debug_type($cell)));
}
if ($cell instanceof TableCell && $cell->getRowspan() > 1) {
@@ -847,7 +785,7 @@ class Table
private function cleanup()
{
$this->effectiveColumnWidths = [];
$this->numberOfColumns = null;
unset($this->numberOfColumns);
}
/**
@@ -900,16 +838,12 @@ class Table
];
}
private function resolveStyle($name): TableStyle
private function resolveStyle(TableStyle|string $name): TableStyle
{
if ($name instanceof TableStyle) {
return $name;
}
if (isset(self::$styles[$name])) {
return self::$styles[$name];
}
throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
return self::$styles[$name] ?? throw new InvalidArgumentException(sprintf('Style "%s" is not defined.', $name));
}
}