Skip to content

Commit

Permalink
Add --longest option to spot longest files
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 12, 2024
1 parent 9922322 commit 5738d5a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Console/Command/MeasureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function configure(): void
$this->addOption('json', null, InputOption::VALUE_NONE, 'Output in JSON format');
$this->addOption('short', null, InputOption::VALUE_NONE, 'Print short metrics only');
$this->addOption('allow-vendor', null, InputOption::VALUE_NONE, 'Allow /vendor directory to be scanned');
$this->addOption('longest', null, InputOption::VALUE_NONE, 'Show top 10 longest files');
}

/**
Expand All @@ -56,6 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$isJson = (bool) $input->getOption('json');
$isShort = (bool) $input->getOption('short');
$allowVendor = (bool) $input->getOption('allow-vendor');
$showLongestFiles = (bool) $input->getOption('longest');

$filePaths = $this->phpFilesFinder->findInDirectories($paths, $excludes, $allowVendor);
if ($filePaths === []) {
Expand All @@ -68,9 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// print results
if ($isJson) {
$this->jsonOutputFormatter->printMeasurement($measurement, $isShort);
$this->jsonOutputFormatter->printMeasurement($measurement, $isShort, $showLongestFiles);
} else {
$this->textOutputFormatter->printMeasurement($measurement, $isShort);
$this->textOutputFormatter->printMeasurement($measurement, $isShort, $showLongestFiles);
}

return Command::SUCCESS;
Expand Down
6 changes: 5 additions & 1 deletion src/Console/OutputFormatter/JsonOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class JsonOutputFormatter implements OutputFormatterInterface
{
public function printMeasurement(Measurements $measurements, bool $isShort): void
public function printMeasurement(Measurements $measurements, bool $isShort, bool $showLongestFiles): void
{
$arrayData = [
'filesystem' => [
Expand Down Expand Up @@ -57,6 +57,10 @@ public function printMeasurement(Measurements $measurements, bool $isShort): voi
];
}

if ($showLongestFiles) {
$arrayData['longest_files'] = $measurements->getLongestFiles();
}

$jsonString = json_encode($arrayData, JSON_PRETTY_PRINT);
Assert::string($jsonString);

Expand Down
13 changes: 12 additions & 1 deletion src/Console/OutputFormatter/TextOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
) {
}

public function printMeasurement(Measurements $measurements, bool $isShort): void
public function printMeasurement(Measurements $measurements, bool $isShort, bool $showLongestFiles): void
{
$this->symfonyStyle->newLine();

Expand All @@ -37,6 +37,17 @@ public function printMeasurement(Measurements $measurements, bool $isShort): voi
$this->printStructure($measurements);
$this->printMethods($measurements);

if ($showLongestFiles) {
$rows = [];
foreach ($measurements->getLongestFiles() as $filePath => $linesCount) {
$rows[] = [$filePath, $linesCount];
}

$tableRows = $this->formatRows($rows);
$tableView = new TableView('Longest files', 'Line count', $tableRows);
$this->viewRenderer->renderTableView($tableView);
}

$this->symfonyStyle->newLine();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Contract/OutputFormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface OutputFormatterInterface
{
public function printMeasurement(Measurements $measurements, bool $isShort): void;
public function printMeasurement(Measurements $measurements, bool $isShort, bool $showLongestFiles): void;
}
21 changes: 21 additions & 0 deletions src/Measurements.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ final class Measurements
*/
private array $namespaceNames = [];

/**
* @var array<string, int>
*/
private array $filesToSize = [];

public function addFile(string $filename): void
{
$this->directoryNames[] = dirname($filename);

$relativeFilePath = str_replace(getcwd() . '/', '', $filename);
$this->filesToSize[$relativeFilePath] = substr_count(file_get_contents($filename), "\n") + 1;

++$this->fileCount;
}

Expand Down Expand Up @@ -134,6 +142,7 @@ public function getDirectoryCount(): int
{
$uniqueDirectoryNames = array_unique($this->directoryNames);
return count($uniqueDirectoryNames) - 1;

}

public function getFileCount(): int
Expand Down Expand Up @@ -290,6 +299,18 @@ public function getEnumCount(): int
return $this->enumCount;
}

/**
* @return array<string, int>
*/
public function getLongestFiles(): array
{
// longest files first
arsort($this->filesToSize);

// get top 10
return array_slice($this->filesToSize, 0, 10);
}

private function relative(int $partialNumber, int $totalNumber): float
{
$relative = ($partialNumber / $totalNumber) * 100;
Expand Down

0 comments on commit 5738d5a

Please sign in to comment.