Skip to content

Commit

Permalink
Add --longest option to spot longest files (#40)
Browse files Browse the repository at this point in the history
* Add --longest option to spot longest files

* docs

* bump deps

* bump ci
  • Loading branch information
TomasVotruba committed Jul 12, 2024
1 parent 9922322 commit 9626ae2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
run: bin/lines measure src --ansi

-
name: 'Run "measure" with json command'
run: bin/lines measure src --json --ansi
name: 'Run "measure" with json and logest command'
run: bin/lines measure src --json --longest --ansi

-
name: 'PHPStan'
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ Also, you can combine them (very handy for blog posts and tweets):
vendor/bin/lines measure src --short --json
```

<br>

Are you looking for top 10 longest files?

```bash
vendor/bin/lines measure src --longest
```

## The Measured Items

For the text output, you'll get data like these:
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
],
"require": {
"php": "^8.2",
"symfony/console": "^6.3",
"symfony/finder": "^6.3",
"illuminate/container": "^10.43",
"symfony/console": "^6.4",
"symfony/finder": "^6.4",
"illuminate/container": "^11.0",
"webmozart/assert": "^1.11",
"nikic/php-parser": "^4.18",
"sebastian/lines-of-code": "^2.0",
"nunomaduro/termwind": "^1.15"
},
"require-dev": {
"phpunit/phpunit": "^10.5",
"rector/rector": "^0.19.5",
"rector/rector": "^1.1",
"phpstan/phpstan": "^1.10.57",
"symplify/easy-coding-standard": "^12.1",
"tracy/tracy": "^2.10",
"tomasvotruba/class-leak": "^0.2",
"phpstan/extension-installer": "^1.3",
"phpstan/extension-installer": "^1.4",
"symplify/vendor-patches": "^11.3",
"symplify/phpstan-rules": "^12.4"
"symplify/phpstan-rules": "^13.0",
"rector/type-perfect": "^0.1.8"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 5 additions & 3 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 @@ -64,13 +66,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$progressBarClosure = $this->createProgressBarClosure($isJson, $filePaths);
$measurement = $this->analyser->measureFiles($filePaths, $progressBarClosure);
$measurements = $this->analyser->measureFiles($filePaths, $progressBarClosure);

// print results
if ($isJson) {
$this->jsonOutputFormatter->printMeasurement($measurement, $isShort);
$this->jsonOutputFormatter->printMeasurement($measurements, $isShort, $showLongestFiles);
} else {
$this->textOutputFormatter->printMeasurement($measurement, $isShort);
$this->textOutputFormatter->printMeasurement($measurements, $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((string) 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 9626ae2

Please sign in to comment.