Skip to content

Commit

Permalink
remov empty row
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 19, 2023
1 parent 8f3026f commit 65d144b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
36 changes: 19 additions & 17 deletions src/Console/OutputFormatter/TextOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use TomasVotruba\Lines\Contract\OutputFormatterInterface;
use TomasVotruba\Lines\Helpers\NumberFormat;
use TomasVotruba\Lines\Measurements;
use TomasVotruba\Lines\ValueObject\TableRow;
use TomasVotruba\Lines\ValueObject\TableView;
use Webmozart\Assert\Assert;

final class TextOutputFormatter implements OutputFormatterInterface
{
Expand Down Expand Up @@ -67,16 +69,21 @@ private function printMethods(Measurements $measurements): void
return;
}

$rows = $this->formatRows([
$tableRows = $this->formatRows([
['Non-static', $measurements->getNonStaticMethods(), $measurements->getNonStaticMethodsRelative()],
['Static', $measurements->getStaticMethods(), $measurements->getStaticMethodsRelative()],
[],
]);

$tableView = new TableView('Method access', 'Count', $tableRows, true);
$this->viewRenderer->renderTableView($tableView);

$tableRows = $this->formatRows([
['Public', $measurements->getPublicMethods(), $measurements->getPublicMethodsRelative()],
['Protected', $measurements->getProtectedMethods(), $measurements->getProtectedMethodsRelative()],
['Private', $measurements->getPrivateMethods(), $measurements->getPrivateMethodsRelative()],
]);

$tableView = new TableView('Methods', 'Count', $rows, true);
$tableView = new TableView('Method visibility', 'Count', $tableRows, true);
$this->viewRenderer->renderTableView($tableView);
}

Expand All @@ -100,23 +107,18 @@ private function printStructure(Measurements $measurements): void

/**
* @param array<array{0?: string, 1?: int|float, 2?: float|null, 3?: bool}> $rows
* @return array<array{}|array{name: string, count: int|float|string, percent: float|string|null, isChild: bool}>>
* @return TableRow[]
*/
private function formatRows(array $rows): array
{
return array_map(static function (array $row): array {
if ($row === []) {
return [];
}

return [
'name' => $row[0],
// make big numbers separated with space, e.g. "1 234"
'count' => NumberFormat::pretty($row[1]),
// optional float relatives
'percent' => isset($row[2]) ? NumberFormat::percent($row[2]) : null,
'isChild' => $row[3] ?? false,
];
return array_map(static function (array $row): TableRow {
Assert::notEmpty($row);

$prettyNumber = NumberFormat::pretty($row[1]);

return new TableRow($row[0], $prettyNumber, isset($row[2]) ? NumberFormat::percent(
$row[2]
) : null, $row[3] ?? false);
}, $rows);
}
}
36 changes: 36 additions & 0 deletions src/ValueObject/TableRow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\Lines\ValueObject;

final class TableRow
{
public function __construct(
private readonly string $name,
private readonly string $count,
private readonly ?string $percent,
private readonly bool $isChild,
) {
}

public function getName(): string

Check failure on line 17 in src/ValueObject/TableRow.php

View workflow job for this annotation

GitHub Actions / PHPStan

Public method "TomasVotruba\Lines\ValueObject\TableRow::getName()" is never used
{
return $this->name;
}

public function getCount(): string

Check failure on line 22 in src/ValueObject/TableRow.php

View workflow job for this annotation

GitHub Actions / PHPStan

Public method "TomasVotruba\Lines\ValueObject\TableRow::getCount()" is never used
{
return $this->count;
}

public function getPercent(): ?string

Check failure on line 27 in src/ValueObject/TableRow.php

View workflow job for this annotation

GitHub Actions / PHPStan

Public method "TomasVotruba\Lines\ValueObject\TableRow::getPercent()" is never used
{
return $this->percent;
}

public function isChild(): bool

Check failure on line 32 in src/ValueObject/TableRow.php

View workflow job for this annotation

GitHub Actions / PHPStan

Public method "TomasVotruba\Lines\ValueObject\TableRow::isChild()" is never used
{
return $this->isChild;
}
}
11 changes: 7 additions & 4 deletions src/ValueObject/TableView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

namespace TomasVotruba\Lines\ValueObject;

use Webmozart\Assert\Assert;

/**
* @api used in templates
*/
final class TableView
{
/**
* @param array<array{}|array{name: string, count: int|float|string, percent: float|string|null, isChild: bool}> $rows
* @param TableRow[] $tableRows
*/
public function __construct(
private readonly string $title,
private readonly string $label,
private readonly array $rows,
private readonly array $tableRows,
private readonly bool $shouldIncludeRelative = false,
) {
Assert::allIsInstanceOf($tableRows, TableRow::class);
}

public function getTitle(): string
Expand All @@ -36,11 +39,11 @@ public function isShouldIncludeRelative(): bool
}

/**
* @return array<array{}|array{name: string, count: int|float|string, percent: float|string|null, isChild: bool}>
* @return TableRow[]
*/
public function getRows(): array
{
return $this->rows;
return $this->tableRows;
}

public function getTemplateFilePath(): string
Expand Down
18 changes: 7 additions & 11 deletions views/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,24 @@
?>
</span>
</div>
<?php foreach ($tableView->getRows() as $row) { ?>
<?php if ($row === []) { ?>
<div />
<?php } else { ?>
<?php foreach ($tableView->getRows() as $tableRow) { ?>
<div class="flex space-x-1">
<span class="<?php echo $row['isChild'] ? 'ml-1' : '' ?>">
<?php echo $row['name']; ?>
<span class="<?php echo $tableRow->isChild() ? 'ml-1' : '' ?>">
<?php echo $tableRow->getName(); ?>
</span>
<span class="flex-1 content-repeat-[.] text-gray" />
<span><?php echo $row['count']; ?></span>
<?php if ($row['percent']) { ?>
<span><?php echo $tableRow->getCount(); ?></span>
<?php if ($tableRow->getPercent()) { ?>
<span>
<span class="text-gray mr-1">/</span>
<span class="text-gray w-6 text-right">
<?php echo $row['percent']; ?>
<?php echo $tableRow->getPercent(); ?>
</span>
</span>
<?php }
?>
</div>
<?php }
?>

<?php }
?>
</div>

0 comments on commit 65d144b

Please sign in to comment.