Skip to content

Commit

Permalink
Added dependency detection inside *.js, *.xml files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Slepnev committed Nov 4, 2022
1 parent 227fc7f commit 9760ce5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Package allows to run static analysis on Magento 2 Module Packages to provide an

### Supported tools: ###

- **Composer.json package dependencies checker** - check *.php and *.phtml on a subject if other packages used inside
- **Composer.json package dependencies checker** - check *.xml, *.js, *.php and *.phtml on a subject if other packages used inside
and check if corresponding module/package is declared as required in composer.json.
- **Module.xml dependencies checker** - analyse if packages' etc/module.xml file contains in 'sequence' section all
Magento 2 modules which classes are used in *.php and *.phtml files of the package.
Magento 2 modules which classes are used in *.xml, *.js, *.php and *.phtml files of the package.
- **Package structure checker** - verify if all newly added Magento 2 modules has a proper structure with all required
files.

Expand Down
19 changes: 15 additions & 4 deletions src/Analysis/Service/Dependencies/Scanner/PhpFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

class PhpFiles implements DependenciesScannerInterface
{
private const FILE_MASKS = ['php', 'phtml'];
private const FILE_MASKS = ['php', 'phtml', 'xml', 'js'];
private const SKIP_FILES = [
'module.xml',
'db_schema.xml',
'config.xml',
'sections.xml',
'page_types.xml',
'fieldset.xml',
'csp_whitelist.xml'
];

private string $regexp;

Expand Down Expand Up @@ -69,6 +78,9 @@ public function lookupDependencies(Package $package): array
*/
private function analyzeFile(\SplFileInfo $file, array $currentModuleNamespaces): array
{
if (in_array($file->getFilename(), self::SKIP_FILES)) {
return [];
}
$contents = \php_strip_whitespace($file->getPathname());

if ($file->getExtension() === 'phtml') {
Expand All @@ -91,7 +103,7 @@ private function analyzeFile(\SplFileInfo $file, array $currentModuleNamespaces)
$dependenciesInfo[] = $referenceModule;
}

return $dependenciesInfo;
return array_unique($dependenciesInfo);
}

/**
Expand All @@ -105,8 +117,7 @@ private function stripeHtml(string $contents): string
{
return (string)preg_replace_callback(
'~(<\?(php|=)\s+.*\?>)~sU',
function (array $matches) use ($contents, &$contentsWithoutHtml)
{
function (array $matches) use ($contents, &$contentsWithoutHtml) {
$contentsWithoutHtml .= $matches[1];

return $contents;
Expand Down

0 comments on commit 9760ce5

Please sign in to comment.