Skip to content

Commit 9529549

Browse files
committed
Merge branch 'MAGETWO-88509' into MPI-PR-2.2.5
2 parents a4c2855 + f4f6af9 commit 9529549

File tree

2 files changed

+84
-42
lines changed

2 files changed

+84
-42
lines changed

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

Lines changed: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,40 @@
1111
use Magento\Framework\Component\ComponentRegistrar;
1212
use Magento\Framework\App\Utility\Files;
1313

14+
/**
15+
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
16+
*/
1417
class ClassesTest extends \PHPUnit\Framework\TestCase
1518
{
19+
/**
20+
* @var ComponentRegistrar
21+
*/
22+
private $componentRegistrar;
23+
1624
/**
1725
* List of already found classes to avoid checking them over and over again
1826
*
1927
* @var array
2028
*/
21-
protected static $_existingClasses = [];
29+
private $existingClasses = [];
2230

23-
protected static $_keywordsBlacklist = ["String", "Array", "Boolean", "Element"];
31+
/**
32+
* @var array
33+
*/
34+
private static $keywordsBlacklist = ["String", "Array", "Boolean", "Element"];
2435

25-
protected static $_namespaceBlacklist = null;
36+
/**
37+
* @var array|null
38+
*/
39+
private $referenceBlackList = null;
2640

27-
protected static $_referenceBlackList = null;
41+
/**
42+
* Set Up
43+
*/
44+
protected function setUp()
45+
{
46+
$this->componentRegistrar = new ComponentRegistrar();
47+
}
2848

2949
public function testPhpFiles()
3050
{
@@ -69,9 +89,9 @@ function ($file) {
6989
$classes
7090
);
7191

72-
$this->_collectResourceHelpersPhp($contents, $classes);
92+
$this->collectResourceHelpersPhp($contents, $classes);
7393

74-
$this->_assertClassesExist($classes, $file);
94+
$this->assertClassesExist($classes, $file);
7595
},
7696
Files::init()->getPhpFiles(
7797
Files::INCLUDE_APP_CODE
@@ -90,7 +110,7 @@ function ($file) {
90110
* @param string $contents
91111
* @param array &$classes
92112
*/
93-
protected function _collectResourceHelpersPhp($contents, &$classes)
113+
private function collectResourceHelpersPhp($contents, &$classes)
94114
{
95115
$regex = '/(?:\:\:|\->)getResourceHelper\(\s*\'([a-z\d\\\\]+)\'\s*\)/ix';
96116
$matches = Classes::getAllMatches($contents, $regex);
@@ -108,7 +128,7 @@ public function testConfigFiles()
108128
*/
109129
function ($path) {
110130
$classes = Classes::collectClassesInConfig(simplexml_load_file($path));
111-
$this->_assertClassesExist($classes, $path);
131+
$this->assertClassesExist($classes, $path);
112132
},
113133
Files::init()->getMainConfigFiles()
114134
);
@@ -145,7 +165,7 @@ function ($path) {
145165
}
146166
$classes = array_merge($classes, Classes::collectLayoutClasses($xml));
147167

148-
$this->_assertClassesExist(array_unique($classes), $path);
168+
$this->assertClassesExist(array_unique($classes), $path);
149169
},
150170
Files::init()->getLayoutFiles()
151171
);
@@ -161,7 +181,7 @@ function ($path) {
161181
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
162182
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
163183
*/
164-
protected function _assertClassesExist($classes, $path)
184+
private function assertClassesExist($classes, $path)
165185
{
166186
if (!$classes) {
167187
return;
@@ -177,7 +197,7 @@ protected function _assertClassesExist($classes, $path)
177197
} else {
178198
$this->assertTrue(
179199
isset(
180-
self::$_existingClasses[$class]
200+
$this->existingClasses[$class]
181201
) || Files::init()->classFileExists(
182202
$class
183203
) || Classes::isVirtual(
@@ -187,7 +207,7 @@ protected function _assertClassesExist($classes, $path)
187207
)
188208
);
189209
}
190-
self::$_existingClasses[$class] = 1;
210+
$this->existingClasses[$class] = 1;
191211
} catch (\PHPUnit\Framework\AssertionFailedError $e) {
192212
$badClasses[] = '\\' . $class;
193213
}
@@ -230,7 +250,7 @@ function ($file) {
230250

231251
$classParts = explode(' ', $classNameMatch[0]);
232252
$className = array_pop($classParts);
233-
$this->_assertClassNamespace($file, $relativePath, $contents, $className);
253+
$this->assertClassNamespace($file, $relativePath, $contents, $className);
234254
},
235255
Files::init()->getPhpFiles()
236256
);
@@ -245,7 +265,7 @@ function ($file) {
245265
* @param string $contents
246266
* @param string $className
247267
*/
248-
protected function _assertClassNamespace($file, $relativePath, $contents, $className)
268+
private function assertClassNamespace($file, $relativePath, $contents, $className)
249269
{
250270
$namespacePattern = '/(Magento|Zend)\/[a-zA-Z]+[^\.]+/';
251271
$formalPattern = '/^namespace\s[a-zA-Z]+(\\\\[a-zA-Z0-9]+)*/m';
@@ -349,7 +369,7 @@ function ($file) {
349369
$vendorClasses = array_filter($vendorClasses, 'strlen');
350370
$vendorClasses = $this->referenceBlacklistFilter($vendorClasses);
351371
if (!empty($vendorClasses)) {
352-
$this->_assertClassesExist($vendorClasses, $file);
372+
$this->assertClassesExist($vendorClasses, $file);
353373
}
354374

355375
if (!empty($result3['exception']) && $result3['exception'][0] != "") {
@@ -368,7 +388,7 @@ function ($file) {
368388

369389
$badClasses = $this->referenceBlacklistFilter($badClasses);
370390
$badClasses = $this->removeSpecialCases($badClasses, $file, $contents, $namespacePath);
371-
$this->_assertClassReferences($badClasses, $file);
391+
$this->assertClassReferences($badClasses, $file);
372392
},
373393
Files::init()->getPhpFiles()
374394
);
@@ -380,7 +400,7 @@ function ($file) {
380400
* @param $aliasClasses
381401
* @param $badClasses
382402
*/
383-
protected function handleAliasClasses($aliasClasses, $badClasses)
403+
private function handleAliasClasses($aliasClasses, $badClasses)
384404
{
385405
foreach ($aliasClasses as $aliasClass) {
386406
foreach ($badClasses as $badClass) {
@@ -397,24 +417,33 @@ protected function handleAliasClasses($aliasClasses, $badClasses)
397417
* @param $classes
398418
* @return array
399419
*/
400-
protected function referenceBlacklistFilter($classes)
420+
private function referenceBlacklistFilter($classes)
401421
{
402422
// exceptions made for the files from the blacklist
403-
self::_setReferenceBlacklist();
423+
$classes = $this->getReferenceBlacklist();
404424
foreach ($classes as $class) {
405-
if (in_array($class, self::$_referenceBlackList)) {
425+
if (in_array($class, $this->referenceBlackList)) {
406426
unset($classes[array_search($class, $classes)]);
407427
}
408428
}
409429
return $classes;
410430
}
411431

412-
protected function _setReferenceBlacklist()
432+
/**
433+
* Returns array of class names from black list.
434+
*
435+
* @return array
436+
*/
437+
private function getReferenceBlacklist()
413438
{
414-
if (!isset(self::$_referenceBlackList)) {
415-
$blackList = file(__DIR__ . '/_files/blacklist/reference.txt', FILE_IGNORE_NEW_LINES);
416-
self::$_referenceBlackList = $blackList;
439+
if (!isset($this->referenceBlackList)) {
440+
$this->referenceBlackList = file(
441+
__DIR__ . '/_files/blacklist/reference.txt',
442+
FILE_IGNORE_NEW_LINES
443+
);
417444
}
445+
446+
return $this->referenceBlackList;
418447
}
419448

420449
/**
@@ -426,18 +455,21 @@ protected function _setReferenceBlacklist()
426455
* @param string $namespacePath
427456
* @return array
428457
*/
429-
protected function removeSpecialCases($badClasses, $file, $contents, $namespacePath)
458+
private function removeSpecialCases($badClasses, $file, $contents, $namespacePath)
430459
{
431460
foreach ($badClasses as $badClass) {
432461
// Remove valid usages of Magento modules from the list
433462
// for example: 'Magento_Sales::actions_edit'
434-
if (preg_match('/Magento_[A-Z0-9][a-z0-9]*/', $badClass)) {
435-
unset($badClasses[array_search($badClass, $badClasses)]);
436-
continue;
463+
if (preg_match('/^[A-Z][a-z]+_[A-Z0-9][a-z0-9]+$/', $badClass)) {
464+
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $badClass);
465+
if ($moduleDir !== null) {
466+
unset($badClasses[array_search($badClass, $badClasses)]);
467+
continue;
468+
}
437469
}
438470

439471
// Remove usage of key words such as "Array", "String", and "Boolean"
440-
if (in_array($badClass, self::$_keywordsBlacklist)) {
472+
if (in_array($badClass, self::$keywordsBlacklist)) {
441473
unset($badClasses[array_search($badClass, $badClasses)]);
442474
continue;
443475
}
@@ -482,12 +514,11 @@ protected function removeSpecialCases($badClasses, $file, $contents, $namespaceP
482514
*/
483515
private function removeSpecialCasesNonFullyQualifiedClassNames($namespacePath, &$badClasses, $badClass)
484516
{
485-
$componentRegistrar = new ComponentRegistrar();
486517
$namespaceParts = explode('/', $namespacePath);
487518
$moduleDir = null;
488519
if (isset($namespaceParts[1])) {
489520
$moduleName = array_shift($namespaceParts) . '_' . array_shift($namespaceParts);
490-
$moduleDir = $componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
521+
$moduleDir = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
491522
}
492523
if ($moduleDir) {
493524
$fullPath = $moduleDir . '/' . implode('/', $namespaceParts) . '/' .
@@ -505,7 +536,7 @@ private function removeSpecialCasesNonFullyQualifiedClassNames($namespacePath, &
505536
unset($badClasses[array_search($badClass, $badClasses)]);
506537
return true;
507538
} else {
508-
return $this->removeSpecialCasesForAllOthers($componentRegistrar, $namespacePath, $badClass, $badClasses);
539+
return $this->removeSpecialCasesForAllOthers($namespacePath, $badClass, $badClasses);
509540
}
510541
}
511542

@@ -516,11 +547,10 @@ private function removeSpecialCasesNonFullyQualifiedClassNames($namespacePath, &
516547
* @param string $badClass
517548
* @return null|string
518549
*/
519-
protected function getLibraryDirByPath($namespacePath, $badClass)
550+
private function getLibraryDirByPath($namespacePath, $badClass)
520551
{
521552
$libraryDir = null;
522553
$fullPath = null;
523-
$componentRegistrar = new ComponentRegistrar();
524554
$namespaceParts = explode('/', $namespacePath);
525555
if (isset($namespaceParts[1]) && $namespaceParts[1]) {
526556
$vendor = array_shift($namespaceParts);
@@ -529,17 +559,26 @@ protected function getLibraryDirByPath($namespacePath, $badClass)
529559
$subLib = $namespaceParts[0];
530560
$subLib = strtolower(preg_replace('/(.)([A-Z])/', "$1-$2", $subLib));
531561
$libraryName = $vendor . '/' . $lib . '-' . $subLib;
532-
$libraryDir = $componentRegistrar->getPath(ComponentRegistrar::LIBRARY, strtolower($libraryName));
562+
$libraryDir = $this->componentRegistrar->getPath(
563+
ComponentRegistrar::LIBRARY,
564+
strtolower($libraryName)
565+
);
533566
if ($libraryDir) {
534567
array_shift($namespaceParts);
535568
} else {
536569
$libraryName = $vendor . '/' . $lib;
537-
$libraryDir = $componentRegistrar->getPath(ComponentRegistrar::LIBRARY, strtolower($libraryName));
570+
$libraryDir = $this->componentRegistrar->getPath(
571+
ComponentRegistrar::LIBRARY,
572+
strtolower($libraryName)
573+
);
538574
}
539575
} else {
540576
$lib = strtolower(preg_replace('/(.)([A-Z])/', "$1-$2", $lib));
541577
$libraryName = $vendor . '/' . $lib;
542-
$libraryDir = $componentRegistrar->getPath(ComponentRegistrar::LIBRARY, strtolower($libraryName));
578+
$libraryDir = $this->componentRegistrar->getPath(
579+
ComponentRegistrar::LIBRARY,
580+
strtolower($libraryName)
581+
);
543582
}
544583
}
545584
if ($libraryDir) {
@@ -550,13 +589,12 @@ protected function getLibraryDirByPath($namespacePath, $badClass)
550589
}
551590

552591
/**
553-
* @param ComponentRegistrar $componentRegistrar
554592
* @param string $namespacePath
555593
* @param string $badClass
556594
* @param array $badClasses
557595
* @return bool
558596
*/
559-
private function removeSpecialCasesForAllOthers($componentRegistrar, $namespacePath, $badClass, &$badClasses)
597+
private function removeSpecialCasesForAllOthers($namespacePath, $badClass, &$badClasses)
560598
{
561599
// Remove usage of classes that do NOT using fully-qualified class names (possibly under same namespace)
562600
$directories = [
@@ -571,7 +609,7 @@ private function removeSpecialCasesForAllOthers($componentRegistrar, $namespaceP
571609
BP . '/dev/tests/static/testsuite/',
572610
BP . '/setup/src/',
573611
];
574-
$libraryPaths = $componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
612+
$libraryPaths = $this->componentRegistrar->getPaths(ComponentRegistrar::LIBRARY);
575613
$directories = array_merge($directories, $libraryPaths);
576614
// Full list of directories where there may be namespace classes
577615
foreach ($directories as $directory) {
@@ -590,7 +628,7 @@ private function removeSpecialCasesForAllOthers($componentRegistrar, $namespaceP
590628
* @param array $badClasses
591629
* @param string $file
592630
*/
593-
protected function _assertClassReferences($badClasses, $file)
631+
private function assertClassReferences($badClasses, $file)
594632
{
595633
if (empty($badClasses)) {
596634
return;

dev/tests/static/testsuite/Magento/Test/Integrity/_files/blacklist/reference.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,8 @@ DoubleColon
121121
\Magento\Mtf\Client\ElementInterface
122122
BarFactory
123123
PartialNamespace\BarFactory
124-
Product\OptionFactory
124+
Product\OptionFactory
125+
Magento_Module
126+
Magento_Framework
127+
Magento_TestModule
128+
Magento_Test

0 commit comments

Comments
 (0)