Skip to content

Commit

Permalink
Merge pull request #46 from crealoz/dev
Browse files Browse the repository at this point in the history
Improve code quality - add sort for processors
  • Loading branch information
ChristopheFerreboeuf authored Jan 24, 2025
2 parents 548b5c5 + c208650 commit 50ee282
Show file tree
Hide file tree
Showing 38 changed files with 713 additions and 631 deletions.
2 changes: 2 additions & 0 deletions Api/Processor/AuditProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public function getErroneousFiles(): array;
public function prepopulateResults(): void;

public function hasErrors(): bool;

public function getOrder(): int;
}
2 changes: 1 addition & 1 deletion Console/RunAuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$ignoredModules = $input->getOption('ignored-modules');
if (!empty($ignoredModules)) {
$this->auditStorage->setIgnoredModules(explode(',',$ignoredModules));
$this->auditStorage->setIgnoredModules(explode(',',(string) $ignoredModules));
}

$this->auditService->run($output, $language);
Expand Down
1 change: 1 addition & 0 deletions Controller/Adminhtml/Request/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function execute()
$this->messageManager->addErrorMessage(__('An error occurred while downloading the file.'));
return $this->_redirect('*/*/index');
}
return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Exception/Processor/AuditProcessorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class AuditProcessorException extends LocalizedException
{
private string $erroneousFile;
private readonly string $erroneousFile;

public function __construct(Phrase $phrase, string $erroneousFile, \Exception $cause = null, $code = 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class MagentoFrameworkPluginExtension extends AuditProcessorException
{
private string $pluggedFile;
private readonly string $pluggedFile;

public function __construct(Phrase $phrase, string $erroneousFile, string $pluggedFile, \Exception $cause = null, $code = 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Model/ResourceModel/AuditRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
return $this;
}
$files = $object->getFiles();
if (count($files) > 0) {
if ($files !== []) {
$connection = $this->getConnection();
$connection->delete(
$this->getTable('crealoz_easyaudit_request_file'),
Expand Down
5 changes: 5 additions & 0 deletions Processor/Files/AbstractAuditProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

abstract class AbstractAuditProcessor implements AuditProcessorInterface
{
public const ORDER = 0;

protected array $results = [];

Expand Down Expand Up @@ -70,6 +71,10 @@ protected function addErroneousFile(string $file, int $score): void
} else {
$this->erroneousFiles[$file] += $score;
}
}

public function getOrder(): int
{
return static::ORDER;
}
}
1 change: 1 addition & 0 deletions Processor/Files/Code/BlockViewModelRatio.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class BlockViewModelRatio extends AbstractArrayProcessor implements ArrayProcessorInterface
{
public const ORDER = 40;

public function getProcessorName(): string
{
Expand Down
11 changes: 6 additions & 5 deletions Processor/Files/Code/HardWrittenSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class HardWrittenSQL extends AbstractFileProcessor implements FileProcessorInterface
{
public const ORDER = 30;

public function getProcessorName(): string
{
Expand Down Expand Up @@ -117,7 +118,7 @@ public function run(): void
* Tries to find a FROM clause in the SQL query
*/
preg_match('/SELECT.*FROM/', $code, $matches);
if (!empty($matches)) {
if ($matches !== []) {
$this->results['hasErrors'] = true;
$this->results['errors']['hardWrittenSQLSelect']['files'][] = $input;
$this->addErroneousFile($input, Audit::PRIORITY_AVERAGE);
Expand All @@ -128,7 +129,7 @@ public function run(): void
* Tries to find an INTO clause in the SQL query
*/
preg_match('/INSERT.*INTO/', $code, $matches);
if (!empty($matches)) {
if ($matches !== []) {
$this->results['hasErrors'] = true;
$this->results['warnings']['hardWrittenSQLInsert']['files'][] = $input;
$this->addErroneousFile($input, Audit::PRIORITY_AVERAGE);
Expand All @@ -139,7 +140,7 @@ public function run(): void
* Tries to find a SET clause in the SQL query
*/
preg_match('/UPDATE.*SET/', $code, $matches);
if (!empty($matches)) {
if ($matches !== []) {
$this->results['hasErrors'] = true;
$this->results['warnings']['hardWrittenSQLUpdate']['files'][] = $input;
$this->addErroneousFile($input, Audit::PRIORITY_AVERAGE);
Expand All @@ -150,7 +151,7 @@ public function run(): void
* Tries to find a FROM clause in the SQL query
*/
preg_match('/DELETE.*FROM/', $code, $matches);
if (!empty($matches)) {
if ($matches !== []) {
$this->results['hasErrors'] = true;
$this->results['errors']['hardWrittenSQLDelete']['files'][] = $input;
$this->addErroneousFile($input, Audit::PRIORITY_AVERAGE);
Expand All @@ -161,7 +162,7 @@ public function run(): void
* Tries to find a JOIN sth ON clause in the SQL query
*/
preg_match('/JOIN.*ON/', $code, $matches);
if (!empty($matches)) {
if ($matches !== []) {
$this->results['hasErrors'] = true;
$this->results['suggestions']['hardWrittenSQLJoin']['files'][] = $input;
}
Expand Down
4 changes: 3 additions & 1 deletion Processor/Files/Code/SpecificClassInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class SpecificClassInjection extends AbstractFileProcessor implements FileProcessorInterface
{
public const ORDER = 20;

private array $ignoredClass = [
'Magento\Framework\Escaper',
'Magento\Framework\Data\Collection\AbstractDb',
Expand Down Expand Up @@ -216,7 +218,7 @@ private function isArgumentIgnored($argumentName)

private function isClassRepository($argumentName): bool
{
return str_contains($argumentName, 'Repository');
return str_contains((string) $argumentName, 'Repository');
}

public function getProcessorTag(): string
Expand Down
2 changes: 1 addition & 1 deletion Processor/Files/Code/UseOfRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class UseOfRegistry extends AbstractFileProcessor implements FileProcessorInterface
{

public const ORDER = 10;

public function __construct(
AuditStorage $auditStorage,
Expand Down
21 changes: 11 additions & 10 deletions Processor/Files/Di/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@

class Plugins extends AbstractXmlProcessor implements FileProcessorInterface
{

public function getProcessorName(): string
{
return __('Plugins');
}

public function getAuditSection(): string
{
return __('Dependency Injection (DI)');
}
public const ORDER = 10;

public function __construct(
AuditStorage $auditStorage,
Expand All @@ -43,6 +34,16 @@ public function __construct(
parent::__construct($auditStorage);
}

public function getProcessorName(): string
{
return __('Plugins');
}

public function getAuditSection(): string
{
return __('Dependency Injection (DI)');
}

public function prepopulateResults(): void
{
parent::prepopulateResults();
Expand Down
4 changes: 3 additions & 1 deletion Processor/Files/Logic/UnusedModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class UnusedModules extends AbstractArrayProcessor implements ArrayProcessorInterface
{
public const ORDER = 10;

public function getAuditSection(): string
{
return __('Logic');
Expand Down Expand Up @@ -59,7 +61,7 @@ private function getUnusedModulesEntry(): array
public function run(): void
{
$unusedModules = $this->getModuleConfig->process($this->getArray());
if (!empty($unusedModules)) {
if ($unusedModules !== []) {
foreach ($unusedModules as $module) {
$this->results['hasErrors'] = true;
$this->results['suggestions']['unusedModules']['files'][] = $module;
Expand Down
4 changes: 3 additions & 1 deletion Processor/Files/View/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Cacheable extends AbstractXmlProcessor implements FileProcessorInterface
{
public const ORDER = 10;

protected array $allowedAreas = ['sales', 'customer', 'gift', 'message'];

public function getProcessorName(): string
Expand Down Expand Up @@ -51,7 +53,7 @@ public function run(): void
foreach ($blocksNotCached as $block) {
$blockName = (string) $block->attributes()->name;
foreach ($this->allowedAreas as $area) {
if (str_contains($blockName, $area)) {
if (str_contains($blockName, (string) $area)) {
continue 2;
}
}
Expand Down
8 changes: 4 additions & 4 deletions Processor/Results/ErroneousFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function processResults(array $results): array
$fileList[$scope][$filename] = $score;
}

if (!empty($fileList)) {
if ($fileList !== []) {
$summary[] = __('%1 files have a score equal to or higher than 10. These files must have really bad design pattern and/or not follow coding standards. Please check them with high priority.', $countHigherThan10);
$summary[] = __('Beside that, %1 files have a score equal to or higher than 5. These files must be checked with medium priority.', $countHigherThan5);

Expand All @@ -60,13 +60,13 @@ public function processResults(array $results): array
*/
private function getScope($file) : string
{
if (str_contains($file, 'vendor')) {
if (str_contains((string) $file, 'vendor')) {
return 'vendor';
}
if (str_contains($file, 'code')) {
if (str_contains((string) $file, 'code')) {
return 'code';
}
if (str_contains($file, 'design')) {
if (str_contains((string) $file, 'design')) {
return 'design';
}
return 'other';
Expand Down
14 changes: 10 additions & 4 deletions Processor/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

abstract class AbstractType implements TypeInterface
{
const ORDER = 0;
protected array $results = [];

protected array $fileGetters = [];
Expand All @@ -25,6 +26,11 @@ public function __construct(
) {
}

public function getOrder(): int
{
return static::ORDER;
}

public function hasErrors(): bool
{
return $this->hasErrors;
Expand All @@ -46,9 +52,9 @@ public function process(array $subTypes, string $type, OutputInterface $output =
foreach ($subTypes as $subType => $processors) {
$fileGetter = $this->getFileGetter($subType);
$files = $fileGetter->execute();
if (!empty($files)) {
if ($files !== []) {
$progressBar = null;
if ($output) {
if ($output instanceof \Symfony\Component\Console\Output\OutputInterface) {
$output->writeln("\r\nProcessing $subType files...");
/** if we are in command line, we display a bar */
$progressBar = new ProgressBar($output, $this->getProgressBarCount($processors, $files));
Expand All @@ -59,7 +65,7 @@ public function process(array $subTypes, string $type, OutputInterface $output =
$this->hasErrors = true;
}
$this->manageResults($processors);
if ($output) {
if ($output instanceof \Symfony\Component\Console\Output\OutputInterface) {
$progressBar->finish();
}
}
Expand Down Expand Up @@ -121,7 +127,7 @@ protected function manageResults(array $processors) : void
foreach ($processors as $processor) {
$results = $processor->getResults();
$results['title'] = $processor->getProcessorName();
$this->results[$processor->getAuditSection()][$processor->getProcessorTag()] = $results;
$this->results[$processor->getAuditSection()][$processor->getOrder()] = $results;
foreach ($processor->getErroneousFiles() as $file => $score) {
if (isset($this->erroneousFiles[$file])) {
$this->erroneousFiles[$file] += $score;
Expand Down
1 change: 1 addition & 0 deletions Processor/Type/Logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Logic extends AbstractType implements TypeInterface
{
const ORDER = 20;

/**
* @inheritDoc
Expand Down
5 changes: 4 additions & 1 deletion Processor/Type/PHPCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class PHPCode extends AbstractType implements TypeInterface
{

const ORDER = 10;

/**
* @inheritDoc
*/
Expand All @@ -16,7 +19,7 @@ protected function doProcess(array $processors, array $files, ProgressBar $progr
$hasErrors = false;
foreach ($files as $codeFile) {
// ignores autoload.php and registration.php
if (str_contains($codeFile, 'autoload.php') || str_contains($codeFile, 'registration.php')) {
if (str_contains((string) $codeFile, 'autoload.php') || str_contains((string) $codeFile, 'registration.php')) {
continue;
}
foreach ($processors as $processor) {
Expand Down
2 changes: 2 additions & 0 deletions Processor/Type/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public function getErroneousFiles(): array;
public function initResults(array $subTypes): void;

public function hasErrors(): bool;

public function getOrder(): int;
}
1 change: 1 addition & 0 deletions Processor/Type/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Xml extends AbstractType implements TypeInterface
{
const ORDER = 30;

/**
* @inheritDoc
Expand Down
16 changes: 14 additions & 2 deletions Service/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function run(OutputInterface $output = null, string $language = null, str
$typeResults = $type->process($subTypes, $typeName, $output);
if ($type->hasErrors()) {
$hasErrors = true;
$this->results = array_merge_recursive($typeResults, $this->results);
$this->results = $this->mergeResultsWithPreservedIndexes($this->results, $typeResults);
$erroneousFiles[$typeName] = $type->getErroneousFiles();
}
}
Expand Down Expand Up @@ -127,6 +127,18 @@ public function run(OutputInterface $output = null, string $language = null, str
return '';
}

private function mergeResultsWithPreservedIndexes(array $oldResults, array $newResults): array
{
foreach ($newResults as $key => $value) {
if (array_key_exists($key, $oldResults)) {
$oldResults[$key] = $this->mergeResultsWithPreservedIndexes($oldResults[$key], $value);
} else {
$oldResults[$key] = $value;
}
}
return $oldResults;
}

/**
* Initialize the results of the processors
* @return void
Expand All @@ -150,7 +162,7 @@ private function consolidateResults($erroneousFiles): array
foreach ($erroneousFiles as $files) {
foreach ($files as $file => $score) {
// if file consists only in empty spaces and line feed, we skip it
if (trim($file) === '') {
if (trim((string) $file) === '') {
continue;
}

Expand Down
Loading

0 comments on commit 50ee282

Please sign in to comment.