php-extension-check/src/Command/CheckCommand.php

141 lines
4.6 KiB
PHP
Raw Normal View History

2021-05-08 21:09:48 +00:00
<?php
declare(strict_types=1);
namespace Fbrinker\ExtensionCheck\Command;
use Closure;
use Fbrinker\ExtensionCheck\Extension\ExtensionCheck;
use Fbrinker\ExtensionCheck\Extension\ExtensionDetails;
use Fbrinker\ExtensionCheck\Parser\FileParser;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class CheckCommand extends Command
{
protected static $defaultName = 'check';
private $fileParser;
private $extensionDetails;
private $extensionCheck;
public function __construct(
$symfonyStyleFactory,
FileParser $fileParser,
ExtensionDetails $extensionDetails,
ExtensionCheck $extensionCheck
) {
parent::__construct();
$this->symfonyStyleFactory = $symfonyStyleFactory;
$this->fileParser = $fileParser;
$this->extensionDetails = $extensionDetails;
$this->extensionCheck = $extensionCheck;
}
protected function configure(): void
{
$this
->setDescription('Checks extensions...')
->setHelp('This command allows you to check your extensions...')
->setDefinition(
new InputDefinition([
new InputArgument('directory', InputArgument::OPTIONAL, "The directory to scan", "./"),
])
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = ($this->symfonyStyleFactory)($input, $output);
$io->section("Analyzing Files");
$this->fileParser->scanForFiles($input->getArgument('directory'));
$fileCount = $this->fileParser->getFileCount();
$io->text(sprintf('Files: %d', $fileCount));
$io->newLine();
$progressBar = $this->getStyledProgressBar($output, $fileCount);
$progressBar->start();
[$classes, $functions, $constants] = $this->fileParser->parseFiles(
$this->getProgressBarClosure($progressBar)
);
$progressBar->finish();
$io->newLine(2);
$io->text(sprintf("Calls to check:", count($classes)));
$io->text(sprintf(
"%d Classes, %d Functions, %d Constants",
count($classes),
count($functions),
count($constants)
));
$io->section("Checking Extension Usages");
$io->text(sprintf('Loaded Extensions: %d', $this->extensionDetails->getLoadedExtensionsCount()));
$io->newLine();
$totalUsagesToCheck = count($classes) + count($functions);
$progressBar = $this->getStyledProgressBar($output, $totalUsagesToCheck);
$progressBar->start();
$usedExtensions = $this->extensionCheck->checkUsages(
$classes,
$functions,
$constants,
$this->getProgressBarClosure($progressBar)
);
$progressBar->finish();
$io->newLine(2);
$unusedExtensions = $this->extensionCheck->getUnused(array_keys($usedExtensions));
$io->text(sprintf(
"<fg=green>%d</> Used, <fg=red>%d</> Unused",
count($usedExtensions),
count($unusedExtensions),
));
$io->section("Result");
$io->text("Used Extensions:");
$tmp = array_keys($usedExtensions);
natcasesort($tmp);
foreach($tmp as $usedExtension) {
$reason = $usedExtensions[$usedExtension];
$io->text(sprintf(' <fg=green>%s</> %s <comment>[Usage: %s]</>',
"\u{2713}",
$usedExtension,
implode(", ", array_keys($reason))
));
}
$io->newLine();
$io->text("Unused Extensions:");
foreach($unusedExtensions as $unusedExtension) {
$io->text(sprintf(' <fg=red>%s</> %s',
"\u{2717}",
$unusedExtension
));
}
return Command::SUCCESS;
}
private function getProgressBarClosure(&$progressBar): Closure {
return function() use ($progressBar) {
$progressBar->advance();
};
}
private function getStyledProgressBar(OutputInterface $output, int $maxValue): ProgressBar {
$progressBar = new ProgressBar($output, $maxValue);
$progressBar->setBarCharacter('<info>=</info>');
$progressBar->setEmptyBarCharacter(' ');
$progressBar->setProgressCharacter('>');
return $progressBar;
}
}