Skip to content

Commit

Permalink
add ignored modules - change some behavior about paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopheFerreboeuf committed Nov 25, 2024
1 parent 2e8711c commit 146647c
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 35 deletions.
22 changes: 20 additions & 2 deletions Console/RunAuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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',
''
)
;
}

Expand All @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions Model/AuditStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Crealoz\EasyAudit\Model;

use Magento\Framework\Module\Dir\Reader;

class AuditStorage
{
private array $ignoredModules = [];

public function __construct(
private readonly Reader $moduleReader
)
{}

public function getIgnoredModules(): array
{
return $this->ignoredModules;
}

/**
* @param array $ignoredModules
*
* @throws \InvalidArgumentException
*/
public function setIgnoredModules(array $ignoredModules): void
{
foreach ($ignoredModules as $module) {
$this->ignoredModules[$module] = $this->moduleReader->getModuleDir('', $module);
}
}
}
12 changes: 11 additions & 1 deletion Processor/Results/ErroneousFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,14 +26,15 @@ 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) {
$countHigherThan5++;
} else {
continue;
}
$fileList[$scope][$file] = $score;
$fileList[$scope][$filename] = $score;
}

if (!empty($fileList)) {
Expand Down
2 changes: 2 additions & 0 deletions Processor/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions Service/FileSystem/ModulePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crealoz\EasyAudit\Service\FileSystem;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;

class ModulePaths
Expand Down Expand Up @@ -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);
}
}
35 changes: 7 additions & 28 deletions Service/PDFWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 4 additions & 2 deletions Service/PDFWriter/SpecificSection/SpecificClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
{
}
Expand All @@ -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) . ')');
}
}

Expand Down
6 changes: 4 additions & 2 deletions Service/PDFWriter/SpecificSection/SpecificModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
{
}
Expand All @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<preference for="Crealoz\EasyAudit\Api\Data\AuditRequestInterface" type="Crealoz\EasyAudit\Model\AuditRequest"/>
<preference for="Crealoz\EasyAudit\Api\AuditRequestRepositoryInterface" type="Crealoz\EasyAudit\Model\AuditRequestRepository"/>
<!-- End of Preferences -->
<type name="Crealoz\EasyAudit\Model\AuditStorage" shared="true">
<arguments>
<argument name="moduleReader" xsi:type="object">Magento\Framework\Module\Dir\Reader\Proxy</argument>
</arguments>
</type>
<!-- Command -->
<type name="Magento\Framework\Console\CommandList">
<arguments>
Expand Down

0 comments on commit 146647c

Please sign in to comment.