Skip to content

Commit 14a4c24

Browse files
authored
Merge pull request #4755 from magento-trigger/MC-17627
- Fixed Dependency static test does not analyze content of phtml files
2 parents b298a37 + e781520 commit 14a4c24

File tree

10 files changed

+52
-55
lines changed

10 files changed

+52
-55
lines changed

app/code/Magento/Braintree/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
"magento/module-sales": "*",
2323
"magento/module-ui": "*",
2424
"magento/module-vault": "*",
25-
"magento/module-multishipping": "*"
25+
"magento/module-multishipping": "*",
26+
"magento/module-theme": "*"
2627
},
2728
"suggest": {
28-
"magento/module-checkout-agreements": "*",
29-
"magento/module-theme": "*"
29+
"magento/module-checkout-agreements": "*"
3030
},
3131
"type": "magento2-module",
3232
"license": [

app/code/Magento/Catalog/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"magento/module-ui": "*",
3232
"magento/module-url-rewrite": "*",
3333
"magento/module-widget": "*",
34-
"magento/module-wishlist": "*",
35-
"magento/module-authorization": "*"
34+
"magento/module-wishlist": "*"
3635
},
3736
"suggest": {
3837
"magento/module-cookie": "*",

app/code/Magento/CatalogWidget/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"magento/module-rule": "*",
1515
"magento/module-store": "*",
1616
"magento/module-widget": "*",
17-
"magento/module-wishlist": "*"
17+
"magento/module-wishlist": "*",
18+
"magento/module-theme": "*"
1819
},
1920
"type": "magento2-module",
2021
"license": [

app/code/Magento/ConfigurableProduct/view/adminhtml/templates/catalog/product/edit/super/config.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"productsProvider": "configurable_associated_product_listing.data_source",
6565
"productsMassAction": "configurable_associated_product_listing.configurable_associated_product_listing.product_columns.ids",
6666
"productsColumns": "configurable_associated_product_listing.configurable_associated_product_listing.product_columns",
67-
"productsGridUrl": "<?= /* @noEscape */ $block->getUrl('catalog/product/associated_grid', ['componentJson' => true]) ?>",
67+
"productsGridUrl": "<?= /* @noEscape */ $block->getUrl('catalog/product_associated/grid', ['componentJson' => true]) ?>",
6868
"configurableVariations": "configurableVariations"
6969
}
7070
}

app/code/Magento/SendFriend/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"magento/module-customer": "*",
1212
"magento/module-store": "*",
1313
"magento/module-captcha": "*",
14-
"magento/module-authorization": "*"
14+
"magento/module-authorization": "*",
15+
"magento/module-theme": "*"
1516
},
1617
"type": "magento2-module",
1718
"license": [

app/code/Magento/Translation/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"magento/framework": "*",
1010
"magento/module-backend": "*",
1111
"magento/module-developer": "*",
12-
"magento/module-store": "*"
12+
"magento/module-store": "*",
13+
"magento/module-theme": "*"
1314
},
1415
"suggest": {
1516
"magento/module-deploy": "*"

app/code/Magento/Vault/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"magento/module-payment": "*",
1313
"magento/module-quote": "*",
1414
"magento/module-sales": "*",
15-
"magento/module-store": "*"
15+
"magento/module-store": "*",
16+
"magento/module-theme": "*"
1617
},
1718
"type": "magento2-module",
1819
"license": [

dev/tests/static/framework/Magento/TestFramework/Dependency/PhpRule.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,12 @@ private function isPluginDependency($dependent, $dependency)
285285
* @return array
286286
* @throws LocalizedException
287287
* @throws \Exception
288+
* @SuppressWarnings(PMD.CyclomaticComplexity)
288289
*/
289290
protected function _caseGetUrl(string $currentModule, string &$contents): array
290291
{
291-
$pattern = '#(\->|:)(?<source>getUrl\(([\'"])(?<route_id>[a-z0-9\-_]{3,})'
292-
.'(/(?<controller_name>[a-z0-9\-_]+))?(/(?<action_name>[a-z0-9\-_]+))?\3)#i';
292+
$pattern = '#(\->|:)(?<source>getUrl\(([\'"])(?<route_id>[a-z0-9\-_]{3,}|\*)'
293+
.'(/(?<controller_name>[a-z0-9\-_]+|\*))?(/(?<action_name>[a-z0-9\-_]+|\*))?\3)#i';
293294

294295
$dependencies = [];
295296
if (!preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
@@ -298,10 +299,22 @@ protected function _caseGetUrl(string $currentModule, string &$contents): array
298299

299300
try {
300301
foreach ($matches as $item) {
302+
$routeId = $item['route_id'];
303+
$controllerName = $item['controller_name'] ?? UrlInterface::DEFAULT_CONTROLLER_NAME;
304+
$actionName = $item['action_name'] ?? UrlInterface::DEFAULT_ACTION_NAME;
305+
306+
// skip rest
307+
if ($routeId === "rest") { //MC-19890
308+
continue;
309+
}
310+
// skip wildcards
311+
if ($routeId === "*" || $controllerName === "*" || $actionName === "*") { //MC-19890
312+
continue;
313+
}
301314
$modules = $this->routeMapper->getDependencyByRoutePath(
302-
$item['route_id'],
303-
$item['controller_name'] ?? UrlInterface::DEFAULT_CONTROLLER_NAME,
304-
$item['action_name'] ?? UrlInterface::DEFAULT_ACTION_NAME
315+
$routeId,
316+
$controllerName,
317+
$actionName
305318
);
306319
if (!in_array($currentModule, $modules)) {
307320
if (count($modules) === 1) {

dev/tests/static/framework/Magento/TestFramework/Dependency/Route/RouteMapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public function getDependencyByRoutePath(
174174
$dependencies = [];
175175
foreach ($this->getRouterTypes() as $routerId) {
176176
if (isset($this->getActionsMap()[$routerId][$routeId][$controllerName][$actionName])) {
177+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
177178
$dependencies = array_merge(
178179
$dependencies,
179180
$this->getActionsMap()[$routerId][$routeId][$controllerName][$actionName]

dev/tests/static/testsuite/Magento/Test/Integrity/DependencyTest.php

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ protected static function _initRules()
234234
. '/_files/dependency_test/tables_*.php';
235235
$dbRuleTables = [];
236236
foreach (glob($replaceFilePattern) as $fileName) {
237-
//phpcs:ignore Generic.PHP.NoSilencedErrors
238-
$dbRuleTables = array_merge($dbRuleTables, @include $fileName);
237+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
238+
$dbRuleTables = array_merge($dbRuleTables, include $fileName);
239239
}
240240
self::$_rulesInstances = [
241241
new PhpRule(
@@ -267,11 +267,11 @@ private static function getRoutesWhitelist(): array
267267
$routesWhitelistFilePattern = realpath(__DIR__) . '/_files/dependency_test/whitelist/routes_*.php';
268268
$routesWhitelist = [];
269269
foreach (glob($routesWhitelistFilePattern) as $fileName) {
270+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
270271
$routesWhitelist = array_merge($routesWhitelist, include $fileName);
271272
}
272273
self::$routesWhitelist = $routesWhitelist;
273274
}
274-
275275
return self::$routesWhitelist;
276276
}
277277

@@ -284,35 +284,36 @@ private static function getRoutesWhitelist(): array
284284
*/
285285
protected function _getCleanedFileContents($fileType, $file)
286286
{
287-
$contents = (string)file_get_contents($file);
287+
$contents = null;
288288
switch ($fileType) {
289289
case 'php':
290-
//Removing php comments
291-
$contents = preg_replace('~/\*.*?\*/~m', '', $contents);
292-
$contents = preg_replace('~^\s*/\*.*?\*/~sm', '', $contents);
293-
$contents = preg_replace('~^\s*//.*$~m', '', $contents);
290+
$contents = php_strip_whitespace($file);
294291
break;
295292
case 'layout':
296293
case 'config':
297294
//Removing xml comments
298-
$contents = preg_replace('~\<!\-\-/.*?\-\-\>~s', '', $contents);
295+
$contents = preg_replace(
296+
'~\<!\-\-/.*?\-\-\>~s',
297+
'',
298+
file_get_contents($file)
299+
);
299300
break;
300301
case 'template':
302+
$contents = php_strip_whitespace($file);
301303
//Removing html
302304
$contentsWithoutHtml = '';
303305
preg_replace_callback(
304-
'~(<\?php\s+.*\?>)~sU',
306+
'~(<\?(php|=)\s+.*\?>)~sU',
305307
function ($matches) use ($contents, &$contentsWithoutHtml) {
306308
$contentsWithoutHtml .= $matches[1];
307309
return $contents;
308310
},
309311
$contents
310312
);
311313
$contents = $contentsWithoutHtml;
312-
//Removing php comments
313-
$contents = preg_replace('~/\*.*?\*/~s', '', $contents);
314-
$contents = preg_replace('~^\s*//.*$~s', '', $contents);
315314
break;
315+
default:
316+
$contents = file_get_contents($file);
316317
}
317318
return $contents;
318319
}
@@ -393,9 +394,9 @@ protected function getDependenciesFromFiles($module, $fileType, $file, $contents
393394
foreach (self::$_rulesInstances as $rule) {
394395
/** @var \Magento\TestFramework\Dependency\RuleInterface $rule */
395396
$newDependencies = $rule->getDependencyInfo($module, $fileType, $file, $contents);
397+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
396398
$dependencies = array_merge($dependencies, $newDependencies);
397399
}
398-
399400
foreach ($dependencies as $key => $dependency) {
400401
foreach (self::$whiteList as $namespace) {
401402
if (strpos($dependency['source'], $namespace) !== false) {
@@ -509,12 +510,12 @@ public function collectRedundant()
509510

510511
foreach (array_keys(self::$mapDependencies) as $module) {
511512
$declared = $this->_getDependencies($module, self::TYPE_HARD, self::MAP_TYPE_DECLARED);
513+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
512514
$found = array_merge(
513515
$this->_getDependencies($module, self::TYPE_HARD, self::MAP_TYPE_FOUND),
514516
$this->_getDependencies($module, self::TYPE_SOFT, self::MAP_TYPE_FOUND),
515517
$schemaDependencyProvider->getDeclaredExistingModuleDependencies($module)
516518
);
517-
518519
$found['Magento\Framework'] = 'Magento\Framework';
519520
$this->_setDependencies($module, self::TYPE_HARD, self::MAP_TYPE_REDUNDANT, array_diff($declared, $found));
520521
}
@@ -578,37 +579,16 @@ protected function _prepareFiles($fileType, $files, $skip = null)
578579
*/
579580
public function getAllFiles()
580581
{
581-
$files = [];
582-
583-
// Get all php files
584-
$files = array_merge(
585-
$files,
582+
return array_merge(
586583
$this->_prepareFiles(
587584
'php',
588585
Files::init()->getPhpFiles(Files::INCLUDE_APP_CODE | Files::AS_DATA_SET | Files::INCLUDE_NON_CLASSES),
589586
true
590-
)
591-
);
592-
593-
// Get all configuration files
594-
$files = array_merge(
595-
$files,
596-
$this->_prepareFiles('config', Files::init()->getConfigFiles())
597-
);
598-
599-
//Get all layout updates files
600-
$files = array_merge(
601-
$files,
602-
$this->_prepareFiles('layout', Files::init()->getLayoutFiles())
603-
);
604-
605-
// Get all template files
606-
$files = array_merge(
607-
$files,
587+
),
588+
$this->_prepareFiles('config', Files::init()->getConfigFiles()),
589+
$this->_prepareFiles('layout', Files::init()->getLayoutFiles()),
608590
$this->_prepareFiles('template', Files::init()->getPhtmlFiles())
609591
);
610-
611-
return $files;
612592
}
613593

614594
/**

0 commit comments

Comments
 (0)