Skip to content

Commit 68b4a6b

Browse files
committed
MC-17627: Dependency static test does not analyze content of phtml files
- Fixed codestyle
1 parent 7eba404 commit 68b4a6b

File tree

4 files changed

+23
-34
lines changed

4 files changed

+23
-34
lines changed

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

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

295294
$dependencies = [];
296295
if (!preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
@@ -302,18 +301,13 @@ protected function _caseGetUrl(string $currentModule, string &$contents): array
302301
$routeId = $item['route_id'];
303302
$controllerName = $item['controller_name'] ?? UrlInterface::DEFAULT_CONTROLLER_NAME;
304303
$actionName = $item['action_name'] ?? UrlInterface::DEFAULT_ACTION_NAME;
305-
if (in_array(
306-
implode('/', [$routeId, $controllerName, $actionName]),
307-
$this->getRoutesWhitelist()
308-
)) {
309-
continue;
310-
}
304+
311305
// skip rest
312-
if ($routeId == "rest") { //MC-17627
306+
if ($routeId === "rest") { //MC-17627
313307
continue;
314308
}
315309
// skip wildcards
316-
if ($routeId == "*" || $controllerName == "*" || $actionName == "*") { //MC-17627
310+
if ($routeId === "*" || $controllerName === "*" || $actionName === "*") { //MC-17627
317311
continue;
318312
}
319313
$modules = $this->routeMapper->getDependencyByRoutePath(
@@ -333,7 +327,9 @@ protected function _caseGetUrl(string $currentModule, string &$contents): array
333327
}
334328
}
335329
} catch (NoSuchActionException $e) {
336-
throw new LocalizedException(__('Invalid URL path: %1', $e->getMessage()), $e);
330+
if (array_search($e->getMessage(), $this->getRoutesWhitelist()) === false) {
331+
throw new LocalizedException(__('Invalid URL path: %1', $e->getMessage()), $e);
332+
}
337333
}
338334

339335
return $dependencies;

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,6 @@ private function processConfigFile(string $module, string $configFile)
243243
if (!in_array($module, $this->routers[$routerId][$routeId])) {
244244
$this->routers[$routerId][$routeId][] = $module;
245245
}
246-
if (isset($route['frontName'])) {
247-
$frontName = (string)$route['frontName'];
248-
if (!isset($this->routers[$routerId][$frontName])) {
249-
$this->routers[$routerId][$frontName] = [];
250-
}
251-
if (!in_array($module, $this->routers[$routerId][$frontName])) {
252-
$this->routers[$routerId][$frontName][] = $module;
253-
}
254-
}
255246
}
256247
}
257248
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ protected static function _initRules()
234234
. '/_files/dependency_test/tables_*.php';
235235
$dbRuleTables = [];
236236
foreach (glob($replaceFilePattern) as $fileName) {
237-
$dbRuleTables = array_merge($dbRuleTables, @include $fileName); //phpcs:ignore
237+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
238+
$dbRuleTables = array_merge($dbRuleTables, @include $fileName);
238239
}
239240
self::$_rulesInstances = [
240241
new PhpRule(
@@ -264,14 +265,14 @@ private static function getRoutesWhitelist(): array
264265
{
265266
if (is_null(self::$routesWhitelist)) {
266267
$routesWhitelistFilePattern = realpath(__DIR__) . '/_files/dependency_test/whitelist/routes_*.php';
267-
self::$routesWhitelist = array_merge(
268-
...array_map(
269-
function ($fileName) {
270-
return include $fileName;
271-
},
272-
glob($routesWhitelistFilePattern)
273-
)
274-
);
268+
self::$routesWhitelist = [];
269+
foreach (glob($routesWhitelistFilePattern) as $fileName) {
270+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
271+
self::$routesWhitelist = array_merge(
272+
self::$routesWhitelist,
273+
include $fileName
274+
);
275+
}
275276
}
276277
return self::$routesWhitelist;
277278
}
@@ -294,7 +295,7 @@ protected function _getCleanedFileContents($fileType, $file)
294295
return preg_replace(
295296
'~\<!\-\-/.*?\-\-\>~s',
296297
'',
297-
(string)file_get_contents($file)
298+
file_get_contents($file)
298299
);
299300
case 'template':
300301
$contents = php_strip_whitespace($file);
@@ -310,7 +311,7 @@ function ($matches) use ($contents, &$contentsWithoutHtml) {
310311
);
311312
return $contentsWithoutHtml;
312313
}
313-
return (string)file_get_contents($file);
314+
return file_get_contents($file);
314315
}
315316

316317
/**
@@ -389,7 +390,8 @@ protected function getDependenciesFromFiles($module, $fileType, $file, $contents
389390
foreach (self::$_rulesInstances as $rule) {
390391
/** @var \Magento\TestFramework\Dependency\RuleInterface $rule */
391392
$newDependencies = $rule->getDependencyInfo($module, $fileType, $file, $contents);
392-
$dependencies = array_merge($dependencies, $newDependencies); //phpcs:ignore
393+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
394+
$dependencies = array_merge($dependencies, $newDependencies);
393395
}
394396
foreach ($dependencies as $key => $dependency) {
395397
foreach (self::$whiteList as $namespace) {
@@ -504,7 +506,7 @@ public function collectRedundant()
504506

505507
foreach (array_keys(self::$mapDependencies) as $module) {
506508
$declared = $this->_getDependencies($module, self::TYPE_HARD, self::MAP_TYPE_DECLARED);
507-
//phpcs:ignore
509+
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
508510
$found = array_merge(
509511
$this->_getDependencies($module, self::TYPE_HARD, self::MAP_TYPE_FOUND),
510512
$this->_getDependencies($module, self::TYPE_SOFT, self::MAP_TYPE_FOUND),

dev/tests/static/testsuite/Magento/Test/Integrity/_files/dependency_test/whitelist/routes_ce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
declare(strict_types=1);
77

88
return [
9-
'privacy-policy-cookie-restriction-mode/index/index'
9+
'privacy-policy-cookie-restriction-mode/index/index',
1010
];

0 commit comments

Comments
 (0)