From 146647c3a92e48163b9953fad6cfb55321ba3d63 Mon Sep 17 00:00:00 2001 From: Christophe Ferreboeuf Date: Mon, 25 Nov 2024 16:35:52 +0100 Subject: [PATCH] add ignored modules - change some behavior about paths --- Console/RunAuditCommand.php | 22 ++++++++++-- Model/AuditStorage.php | 32 +++++++++++++++++ Processor/Results/ErroneousFiles.php | 12 ++++++- Processor/Type/AbstractType.php | 2 ++ Service/FileSystem/ModulePaths.php | 24 +++++++++++++ Service/PDFWriter.php | 35 ++++--------------- .../SpecificSection/SpecificClass.php | 6 ++-- .../SpecificSection/SpecificModel.php | 6 ++-- etc/di.xml | 5 +++ 9 files changed, 109 insertions(+), 35 deletions(-) create mode 100644 Model/AuditStorage.php diff --git a/Console/RunAuditCommand.php b/Console/RunAuditCommand.php index 3bc91f8..9eb24bb 100644 --- a/Console/RunAuditCommand.php +++ b/Console/RunAuditCommand.php @@ -2,6 +2,7 @@ namespace Crealoz\EasyAudit\Console; use Composer\Console\Input\InputOption; +use Crealoz\EasyAudit\Model\AuditStorage; use Magento\Framework\Exception\FileSystemException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -13,7 +14,8 @@ class RunAuditCommand extends Command { public function __construct( - protected \Crealoz\EasyAudit\Service\Audit $auditService + protected \Crealoz\EasyAudit\Service\Audit $auditService, + protected readonly AuditStorage $auditStorage ) { parent::__construct(); @@ -23,7 +25,20 @@ protected function configure() { $this->setName('crealoz:run:audit') ->setDescription('Run the audit service on request') - ->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'Language to use for the audit service', 'en_US') + ->addOption( + 'language', + 'l', + InputOption::VALUE_OPTIONAL, + 'Language to use for the audit service', + 'en_US' + ) + ->addOption( + 'ignored-modules', + 'i', + InputOption::VALUE_OPTIONAL, + 'List of modules to ignore', + '' + ) ; } @@ -37,6 +52,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $language = $input->getOption('language'); + $ignoredModules = $input->getOption('ignored-modules'); + $this->auditStorage->setIgnoredModules(explode(',',$ignoredModules)); + $this->auditService->run($output, $language); $duration = microtime(true) - $start; diff --git a/Model/AuditStorage.php b/Model/AuditStorage.php new file mode 100644 index 0000000..1683a7d --- /dev/null +++ b/Model/AuditStorage.php @@ -0,0 +1,32 @@ +ignoredModules; + } + + /** + * @param array $ignoredModules + * + * @throws \InvalidArgumentException + */ + public function setIgnoredModules(array $ignoredModules): void + { + foreach ($ignoredModules as $module) { + $this->ignoredModules[$module] = $this->moduleReader->getModuleDir('', $module); + } + } +} \ No newline at end of file diff --git a/Processor/Results/ErroneousFiles.php b/Processor/Results/ErroneousFiles.php index 5cd4249..e469ab1 100644 --- a/Processor/Results/ErroneousFiles.php +++ b/Processor/Results/ErroneousFiles.php @@ -2,8 +2,17 @@ namespace Crealoz\EasyAudit\Processor\Results; +use Crealoz\EasyAudit\Service\FileSystem\ModulePaths; + class ErroneousFiles implements \Crealoz\EasyAudit\Api\Processor\ResultProcessorInterface { + + public function __construct( + private readonly ModulePaths $modulePaths + ) + { + } + /** * Checks results for erroneous files entries where score is superior to 5 then gets the ones where score is * superior to 10. @@ -17,6 +26,7 @@ public function processResults(array $results): array $fileList = []; foreach ($results['erroneousFiles'] as $file => $score) { $scope = $this->getScope($file); + $filename = $this->modulePaths->stripVendorOrApp($file); if ($score >= 10) { $countHigherThan10++; } elseif ($score >= 5) { @@ -24,7 +34,7 @@ public function processResults(array $results): array } else { continue; } - $fileList[$scope][$file] = $score; + $fileList[$scope][$filename] = $score; } if (!empty($fileList)) { diff --git a/Processor/Type/AbstractType.php b/Processor/Type/AbstractType.php index f95470e..174125b 100644 --- a/Processor/Type/AbstractType.php +++ b/Processor/Type/AbstractType.php @@ -3,6 +3,7 @@ namespace Crealoz\EasyAudit\Processor\Type; use Crealoz\EasyAudit\Api\FileSystem\FileGetterInterface; +use Crealoz\EasyAudit\Api\Processor\AuditProcessorInterface; use Crealoz\EasyAudit\Service\FileSystem\FileGetterFactory; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Helper\ProgressBar; @@ -116,6 +117,7 @@ protected function getFileGetter(string $type): FileGetterInterface */ protected function manageResults(array $processors) : void { + /** @var AuditProcessorInterface $processor */ foreach ($processors as $processor) { $this->results[$processor->getAuditSection()][$processor->getProcessorName()] = $processor->getResults(); foreach ($processor->getErroneousFiles() as $file => $score) { diff --git a/Service/FileSystem/ModulePaths.php b/Service/FileSystem/ModulePaths.php index a387bb4..d549b92 100644 --- a/Service/FileSystem/ModulePaths.php +++ b/Service/FileSystem/ModulePaths.php @@ -2,6 +2,7 @@ namespace Crealoz\EasyAudit\Service\FileSystem; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem; class ModulePaths @@ -57,4 +58,27 @@ public function getModuleBaseDir(string $filePath, bool $isVendor = false): stri } return $baseDir; } + + /** + * Remove the vendor or app part of a path + * + * @param string $path + * @return string + */ + public function stripVendorOrApp(string $path): string + { + $path = $this->removeBaseNameFromPath($path); + $pathParts = explode(DIRECTORY_SEPARATOR, $path); + if (isset($pathParts[0]) && in_array($pathParts[0], ['vendor', 'app'])) { + $offset = $pathParts[0] === 'vendor' ? 1 : 2; + return implode(DIRECTORY_SEPARATOR, array_slice($pathParts, $offset)); + } + return $path; + } + + public function removeBaseNameFromPath(string $path): string + { + $magentoInstallationPath = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath(); + return str_replace($magentoInstallationPath, '', $path); + } } \ No newline at end of file diff --git a/Service/PDFWriter.php b/Service/PDFWriter.php index 9a5a40f..4664c80 100644 --- a/Service/PDFWriter.php +++ b/Service/PDFWriter.php @@ -3,6 +3,7 @@ namespace Crealoz\EasyAudit\Service; use Crealoz\EasyAudit\Api\Result\SectionInterface; +use Crealoz\EasyAudit\Service\FileSystem\ModulePaths; use Crealoz\EasyAudit\Service\PDFWriter\CliTranslator; use Crealoz\EasyAudit\Service\PDFWriter\SizeCalculation; use Magento\Framework\App\Filesystem\DirectoryList; @@ -43,6 +44,7 @@ public function __construct( private readonly CliTranslator $cliTranslator, private readonly \Psr\Log\LoggerInterface $logger, private readonly Reader $moduleReader, + private readonly ModulePaths $modulePaths, private readonly array $specificSections = [], public int $x = 50, public int $columnCount = 1 @@ -157,7 +159,7 @@ private function manageIntroduction(array $introduction): void } $this->writeLine(ucfirst($scope) . ' files:'); foreach ($files as $file => $score) { - $file = $this->stripVendorOrApp($file); + $file = $this->modulePaths->stripVendorOrApp($file); if ($score >= 10) { $this->writeLine($file . ' (' . $score . ')', true,8, 0, 0.85); } elseif ($score >= 5) { @@ -169,29 +171,6 @@ private function manageIntroduction(array $introduction): void } } - /** - * Remove the vendor or app part of a path - * - * @param string $path - * @return string - */ - public function stripVendorOrApp(string $path): string - { - $path = $this->removeBaseNameFromPath($path); - $pathParts = explode(DIRECTORY_SEPARATOR, $path); - if (isset($pathParts[0]) && in_array($pathParts[0], ['vendor', 'app'])) { - $offset = $pathParts[0] === 'vendor' ? 1 : 2; - return implode(DIRECTORY_SEPARATOR, array_slice($pathParts, $offset)); - } - return $path; - } - - public function removeBaseNameFromPath(string $path): string - { - $magentoInstallationPath = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath(); - return str_replace($magentoInstallationPath, '', $path); - } - /** * Manage the subresult of a section * @@ -255,7 +234,7 @@ private function displaySection(string $title, array $section): void if ($entries['files'] === []) { continue; } - $numberOfPages = $this->specificSections[$sectionName]->calculateSize($entries) / 800; + $numberOfPages = (int) $this->specificSections[$sectionName]->calculateSize($entries) / 800; if ($numberOfPages > 10) { $this->delegateToAnnex($numberOfPages, $entries, $title, $sectionName); } else { @@ -291,7 +270,7 @@ private function manageSubsection(array $subResults): void return; } $this->writeSubSectionIntro($subResults); - $numberOfPages = $this->sizeCalculation->getNumberOfPagesForFiles($subResults['files']); + $numberOfPages = (int)$this->sizeCalculation->getNumberOfPagesForFiles($subResults['files']); if ($numberOfPages > 10 && is_array($subResults['files'])) { $this->delegateToAnnex($numberOfPages, $subResults['files'], $subResults['title'] ?? ''); } else { @@ -320,12 +299,12 @@ private function manageFiles(int $numberOfPages, array|string $resultFiles): voi if (is_array($files)) { $this->writeLine($key, true); foreach ($files as $file) { - $file = $this->stripVendorOrApp($file); + $file = $this->modulePaths->stripVendorOrApp($file); $this->writeLine('-' . $file, true, 8, 0, 0.2, 0.2, 0.2); } $this->y -= 5; } else { - $file = $this->stripVendorOrApp($files); + $file = $this->modulePaths->stripVendorOrApp($files); $this->writeLine('-' . $file, true); } } diff --git a/Service/PDFWriter/SpecificSection/SpecificClass.php b/Service/PDFWriter/SpecificSection/SpecificClass.php index c2d4947..dc33e1f 100644 --- a/Service/PDFWriter/SpecificSection/SpecificClass.php +++ b/Service/PDFWriter/SpecificSection/SpecificClass.php @@ -3,13 +3,15 @@ namespace Crealoz\EasyAudit\Service\PDFWriter\SpecificSection; use Crealoz\EasyAudit\Api\Result\SectionInterface; +use Crealoz\EasyAudit\Service\FileSystem\ModulePaths; use Crealoz\EasyAudit\Service\PDFWriter; class SpecificClass implements SectionInterface { public function __construct( - public readonly PDFWriter\SizeCalculation $sizeCalculation + public readonly PDFWriter\SizeCalculation $sizeCalculation, + private readonly ModulePaths $modulePaths ) { } @@ -27,7 +29,7 @@ public function writeSection(PDFWriter $pdfWriter, array $subresults, bool $isAn if ($pdfWriter->y < 9 * 1.3) { $pdfWriter->switchColumnOrAddPage(); } - $pdfWriter->writeLine('-' . $pdfWriter->stripVendorOrApp($file) . '(potential issues count : ' . count($arguments) . ')'); + $pdfWriter->writeLine('-' . $this->modulePaths->stripVendorOrApp($file) . '(potential issues count : ' . count($arguments) . ')'); } } diff --git a/Service/PDFWriter/SpecificSection/SpecificModel.php b/Service/PDFWriter/SpecificSection/SpecificModel.php index 351e076..dd42c99 100644 --- a/Service/PDFWriter/SpecificSection/SpecificModel.php +++ b/Service/PDFWriter/SpecificSection/SpecificModel.php @@ -3,13 +3,15 @@ namespace Crealoz\EasyAudit\Service\PDFWriter\SpecificSection; use Crealoz\EasyAudit\Api\Result\SectionInterface; +use Crealoz\EasyAudit\Service\FileSystem\ModulePaths; use Crealoz\EasyAudit\Service\PDFWriter; class SpecificModel implements SectionInterface { public function __construct( - public readonly PDFWriter\SizeCalculation $sizeCalculation + public readonly PDFWriter\SizeCalculation $sizeCalculation, + private readonly ModulePaths $modulePaths ) { } @@ -27,7 +29,7 @@ public function writeSection(PDFWriter $pdfWriter, array $subresults, bool $isAn if ($pdfWriter->y < 9 * 1.3 + count($arguments) * 12 + 10) { $pdfWriter->switchColumnOrAddPage(); } - $pdfWriter->writeLine($pdfWriter->stripVendorOrApp($file), true); + $pdfWriter->writeLine($this->modulePaths->stripVendorOrApp($file), true); foreach ($arguments as $argument) { $pdfWriter->writeLine('-' . $argument , true, 8); } diff --git a/etc/di.xml b/etc/di.xml index dcd825d..f40c31d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -4,6 +4,11 @@ + + + Magento\Framework\Module\Dir\Reader\Proxy + +