Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #14

Merged
merged 2 commits into from
Nov 13, 2024
Merged

Dev #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Api/Processor/Audit/ArrayProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Crealoz\EasyAudit\Api\Processor\Audit;

use Crealoz\EasyAudit\Api\Processor\AuditProcessorInterface;

interface ArrayProcessorInterface extends AuditProcessorInterface
{
public function setArray(array $array): void;

public function getArray(): array;
}
12 changes: 12 additions & 0 deletions Api/Processor/Audit/FileProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Crealoz\EasyAudit\Api\Processor\Audit;

use Crealoz\EasyAudit\Api\Processor\AuditProcessorInterface;

interface FileProcessorInterface extends AuditProcessorInterface
{
public function setFile(string $file): void;

public function getFile(): string;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Crealoz\EasyAudit\Processor\Files;
namespace Crealoz\EasyAudit\Api\Processor;

/**
* @author Christophe Ferreboeuf <[email protected]>
*/
interface ProcessorInterface
interface AuditProcessorInterface
{
/**
* @param $input
*/
public function run($input);
public function run(): void;

/**
* @return string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Crealoz\EasyAudit\Processor\Results;
namespace Crealoz\EasyAudit\Api\Processor;

interface ResultProcessorInterface
{
Expand Down
20 changes: 20 additions & 0 deletions Processor/Files/AbstractArrayProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Crealoz\EasyAudit\Processor\Files;

use Crealoz\EasyAudit\Api\Processor\Audit\ArrayProcessorInterface;

abstract class AbstractArrayProcessor extends AbstractAuditProcessor implements ArrayProcessorInterface
{
protected array $array = [];

public function setArray(array $array): void
{
$this->array = $array;
}

public function getArray(): array
{
return $this->array;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Crealoz\EasyAudit\Processor\Files;

use Crealoz\EasyAudit\Api\Processor\AuditProcessorInterface;
use Crealoz\EasyAudit\Exception\Processor\GeneralAuditException;

abstract class AbstractProcessor implements ProcessorInterface
abstract class AbstractAuditProcessor implements AuditProcessorInterface
{

protected array $results = [];
Expand All @@ -16,7 +17,7 @@ public function hasErrors(): bool
return array_key_exists('hasErrors', $this->results) && $this->results['hasErrors'];
}

abstract public function run($input);
abstract public function run(): void;

public function prepopulateResults(): void {
$this->erroneousFiles = [];
Expand Down
21 changes: 21 additions & 0 deletions Processor/Files/AbstractFileProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Crealoz\EasyAudit\Processor\Files;

use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;

abstract class AbstractFileProcessor extends AbstractAuditProcessor implements FileProcessorInterface
{

protected string $file;

public function setFile(string $file): void
{
$this->file = $file;
}

public function getFile(): string
{
return $this->file;
}
}
14 changes: 14 additions & 0 deletions Processor/Files/AbstractXmlProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Crealoz\EasyAudit\Processor\Files;

use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;

abstract class AbstractXmlProcessor extends AbstractFileProcessor implements FileProcessorInterface
{

protected function getContent(): \SimpleXMLElement
{
return simplexml_load_file($this->getFile());
}
}
14 changes: 6 additions & 8 deletions Processor/Files/Code/BlockViewModelRatio.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Crealoz\EasyAudit\Processor\Files\Code;

use Crealoz\EasyAudit\Processor\Files\AbstractProcessor;
use Crealoz\EasyAudit\Processor\Files\ProcessorInterface;
use Crealoz\EasyAudit\Api\Processor\Audit\ArrayProcessorInterface;
use Crealoz\EasyAudit\Processor\Files\AbstractArrayProcessor;
use Crealoz\EasyAudit\Processor\Files\AbstractAuditProcessor;

class BlockViewModelRatio extends AbstractProcessor implements ProcessorInterface
class BlockViewModelRatio extends AbstractArrayProcessor implements ArrayProcessorInterface
{

public function getProcessorName(): string
Expand Down Expand Up @@ -44,12 +45,9 @@ private function getBVMWarningEntry(): array
];
}

public function run($input)
public function run(): void
{
if (!is_array($input)) {
throw new \InvalidArgumentException('Input must be an array');
}
$files = $this->segregateFilesByModule($input);
$files = $this->segregateFilesByModule($this->getArray());
foreach ($files as $module => $moduleFiles) {
$blockViewModelRatio = $this->getBlockViewModelRatio($moduleFiles);
if ($blockViewModelRatio > 0.5) {
Expand Down
9 changes: 5 additions & 4 deletions Processor/Files/Code/HardWrittenSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Crealoz\EasyAudit\Processor\Files\Code;

use Crealoz\EasyAudit\Processor\Files\AbstractProcessor;
use Crealoz\EasyAudit\Processor\Files\ProcessorInterface;
use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;
use Crealoz\EasyAudit\Processor\Files\AbstractFileProcessor;
use Crealoz\EasyAudit\Service\Audit;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\DriverInterface;

class HardWrittenSQL extends AbstractProcessor implements ProcessorInterface
class HardWrittenSQL extends AbstractFileProcessor implements FileProcessorInterface
{

public function getProcessorName(): string
Expand Down Expand Up @@ -104,8 +104,9 @@ private function getHardWrittenSQLJoinEntry(): array
/**
* @throws FileSystemException
*/
public function run($input)
public function run(): void
{
$input = $this->getFile();
$code = $this->driver->fileGetContents($input);
if (str_contains($code, 'SELECT')) {
/**
Expand Down
14 changes: 8 additions & 6 deletions Processor/Files/Code/SpecificClassInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Crealoz\EasyAudit\Processor\Files\Code;

use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;
use Crealoz\EasyAudit\Api\Processor\AuditProcessorInterface;
use Crealoz\EasyAudit\Exception\Processor\Getters\NotAClassException;
use Crealoz\EasyAudit\Processor\Files\AbstractProcessor;
use Crealoz\EasyAudit\Processor\Files\ProcessorInterface;
use Crealoz\EasyAudit\Processor\Files\AbstractAuditProcessor;
use Crealoz\EasyAudit\Processor\Files\AbstractFileProcessor;
use Crealoz\EasyAudit\Service\Audit;
use Crealoz\EasyAudit\Service\Classes\ArgumentTypeChecker;
use Crealoz\EasyAudit\Service\Classes\ConstructorService;
Expand All @@ -16,7 +18,7 @@
* A class must not be injected in controller as a specific class. In all the cases, a factory or an interface should be used.
* @author Christophe Ferreboeuf <[email protected]>
*/
class SpecificClassInjection extends AbstractProcessor implements ProcessorInterface
class SpecificClassInjection extends AbstractFileProcessor implements FileProcessorInterface
{
private array $ignoredClass = [
'Magento\Framework\Escaper',
Expand Down Expand Up @@ -118,11 +120,11 @@ public function getAuditSection(): string
{
return __('PHP');
}
public function run($input)
public function run(): void
{
// First we get class name from the input that represents the file's path
try {
$className = $this->classNameGetter->getClassFullNameFromFile($input);
$className = $this->classNameGetter->getClassFullNameFromFile($this->getFile());
} catch (NotAClassException|FileSystemException $e) {
return;
}
Expand All @@ -141,7 +143,7 @@ public function run($input)
}
$fileErrorLevel = 0;
foreach ($arguments as $argument) {
if ($argument === null || !is_array($argument) || count($argument) < 2 || !is_string($argument[1])) {
if (!is_array($argument) || count($argument) < 2 || !is_string($argument[1])) {
continue;
}
$argumentName = $argument[1];
Expand Down
13 changes: 7 additions & 6 deletions Processor/Files/Code/UseOfRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

namespace Crealoz\EasyAudit\Processor\Files\Code;

use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;
use Crealoz\EasyAudit\Exception\Processor\Getters\NotAClassException;
use Crealoz\EasyAudit\Processor\Files\AbstractProcessor;
use Crealoz\EasyAudit\Processor\Files\ProcessorInterface;
use Crealoz\EasyAudit\Processor\Files\AbstractFileProcessor;
use Crealoz\EasyAudit\Service\Classes\ConstructorService;
use Crealoz\EasyAudit\Service\FileSystem\ClassNameGetter;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\ObjectManager\DefinitionInterface;

class UseOfRegistry extends AbstractProcessor implements ProcessorInterface
class UseOfRegistry extends AbstractFileProcessor implements FileProcessorInterface
{


public function __construct(
private readonly ClassNameGetter $classNameGetter,
private readonly \Magento\Framework\ObjectManager\DefinitionInterface $definitions,
private readonly DefinitionInterface $definitions,
private readonly ConstructorService $constructorService
)
{
Expand Down Expand Up @@ -55,10 +56,10 @@ public function getAuditSection(): string
return __('PHP');
}

public function run($input): void
public function run(): void
{
try {
$className = $this->classNameGetter->getClassFullNameFromFile($input);
$className = $this->classNameGetter->getClassFullNameFromFile($this->getFile());
} catch (NotAClassException|FileSystemException $e) {
return;
}
Expand Down
37 changes: 14 additions & 23 deletions Processor/Files/Di/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

namespace Crealoz\EasyAudit\Processor\Files\Di;

use Crealoz\EasyAudit\Api\Processor\Audit\FileProcessorInterface;
use Crealoz\EasyAudit\Exception\Processor\Plugins\AroundToAfterPluginException;
use Crealoz\EasyAudit\Exception\Processor\Plugins\AroundToBeforePluginException;
use Crealoz\EasyAudit\Exception\Processor\Plugins\ConfigProviderPluginException;
use Crealoz\EasyAudit\Exception\Processor\Plugins\MagentoFrameworkPluginExtension;
use Crealoz\EasyAudit\Exception\Processor\Plugins\SameModulePluginException;
use Crealoz\EasyAudit\Processor\Files\AbstractProcessor;
use Crealoz\EasyAudit\Processor\Files\AbstractXmlProcessor;
use Crealoz\EasyAudit\Processor\Files\Di\Plugins\AroundToAfter;
use Crealoz\EasyAudit\Processor\Files\Di\Plugins\AroundToBefore;
use Crealoz\EasyAudit\Processor\Files\Di\Plugins\CheckConfigProvider;
use Crealoz\EasyAudit\Processor\Files\ProcessorInterface;
use Crealoz\EasyAudit\Service\Audit;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Exception\FileSystemException;
use Psr\Log\LoggerInterface;

class Plugins extends AbstractProcessor implements ProcessorInterface
class Plugins extends AbstractXmlProcessor implements FileProcessorInterface
{

public function getProcessorName(): string
Expand Down Expand Up @@ -123,31 +123,22 @@ private function getAroundToAfterPluginEntry(): array
];
}

public function run($input)
public function run(): void
{
if (!($input instanceof \SimpleXMLElement)) {
throw new \InvalidArgumentException("Input must be an instance of SimpleXMLElement");
}

$typeNodes = $input->xpath('//type[plugin]');

try {
foreach ($typeNodes as $typeNode) {
$pluginNodes = $typeNode->xpath('plugin');
$pluggedClassName = (string)$typeNode['name'];
$typeNodes = $this->getContent()->xpath('//type[plugin]');
foreach ($typeNodes as $typeNode) {
$pluginNodes = $typeNode->xpath('plugin');
$pluggedClassName = (string)$typeNode['name'];

foreach ($pluginNodes as $pluginNode) {
$pluggingClassName = (string)$pluginNode['type'];
$pluginDisabled = (string)$pluginNode['disabled'] ?? 'false';
if ($pluginDisabled === 'true') {
continue;
}
$this->processPlugin($pluggingClassName, $pluggedClassName);
foreach ($pluginNodes as $pluginNode) {
$pluggingClassName = (string)$pluginNode['type'];
$pluginDisabled = (string)$pluginNode['disabled'] ?? 'false';
if ($pluginDisabled === 'true') {
continue;
}
$this->processPlugin($pluggingClassName, $pluggedClassName);
}
} catch (FileSystemException $e) {
$this->results['warnings']['insufficientPermissions']['files'][] = $e->getMessage();
$this->addErroneousFile($input, Audit::PRIORITY_HIGH);
}
}

Expand Down
13 changes: 12 additions & 1 deletion Processor/Files/Logic/Modules/GetModuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Crealoz\EasyAudit\Processor\Files\Logic\Modules;

use Crealoz\EasyAudit\Service\FileSystem\ModulePaths;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Module\FullModuleList;
Expand All @@ -18,7 +19,8 @@ class GetModuleConfig
public function __construct(
protected readonly DriverInterface $driver,
private readonly FullModuleList $fullModuleList,
private readonly ModuleList $moduleList
private readonly ModuleList $moduleList,
private readonly ModulePaths $modulePath
)
{
}
Expand Down Expand Up @@ -55,6 +57,15 @@ public function getModuleName(string $input): string
return (string)$xml->module['name'];
}

/**
* @throws FileSystemException
*/
public function getModuleNameByAnyFile(string $filePath, bool $isVendor = false): string
{
$input = $this->modulePath->getDeclarationXml($filePath, $isVendor);
return $this->getModuleName($input);
}

/**
* Returns all modules
*
Expand Down
Loading