20
20
21
21
/**
22
22
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
23
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
24
*/
24
25
class DependencyTest extends \PHPUnit \Framework \TestCase
25
26
{
@@ -124,7 +125,7 @@ class DependencyTest extends \PHPUnit\Framework\TestCase
124
125
* )))
125
126
* @var array
126
127
*/
127
- protected static $ _mapDependencies = [];
128
+ protected static $ mapDependencies = [];
128
129
129
130
/**
130
131
* Regex pattern for validation file path of theme
@@ -410,7 +411,7 @@ private function collectDependency($dependency, $currentModule, &$undeclared)
410
411
$ this ->_setDependencies ($ currentModule , $ type , self ::MAP_TYPE_REDUNDANT , $ module );
411
412
}
412
413
413
- $ this ->_addDependencies ($ currentModule , $ type , self ::MAP_TYPE_FOUND , $ nsModule );
414
+ $ this ->addDependency ($ currentModule , $ type , self ::MAP_TYPE_FOUND , $ nsModule );
414
415
}
415
416
416
417
/**
@@ -421,7 +422,7 @@ private function collectDependency($dependency, $currentModule, &$undeclared)
421
422
*/
422
423
public function collectRedundant ()
423
424
{
424
- foreach (array_keys (self ::$ _mapDependencies ) as $ module ) {
425
+ foreach (array_keys (self ::$ mapDependencies ) as $ module ) {
425
426
$ declared = $ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_DECLARED );
426
427
$ found = array_merge (
427
428
$ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_FOUND ),
@@ -440,7 +441,7 @@ public function collectRedundant()
440
441
public function testRedundant ()
441
442
{
442
443
$ output = [];
443
- foreach (array_keys (self ::$ _mapDependencies ) as $ module ) {
444
+ foreach (array_keys (self ::$ mapDependencies ) as $ module ) {
444
445
$ result = [];
445
446
$ redundant = $ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_REDUNDANT );
446
447
if (count ($ redundant )) {
@@ -687,37 +688,40 @@ protected static function _getTypes()
687
688
* Converts a composer json component name into the Magento Module form
688
689
*
689
690
* @param string $jsonName The name of a composer json component or dependency e.g. 'magento/module-theme'
691
+ * @param array $packageModuleMap Mapping package name with module namespace.
690
692
* @return string The corresponding Magento Module e.g. 'Magento\Theme'
691
693
*/
692
- protected static function convertModuleName ($ jsonName)
694
+ protected static function convertModuleName (string $ jsonName, array $ packageModuleMap ): string
693
695
{
694
- if (strpos ($ jsonName , 'magento/module ' ) !== false ) {
695
- $ moduleName = str_replace ('- ' , ' ' , $ jsonName );
696
- $ moduleName = ucwords ($ moduleName );
697
- $ moduleName = str_replace ('module ' , '' , $ moduleName );
698
- $ moduleName = str_replace (' ' , '' , $ moduleName );
699
- $ moduleName = str_replace ('/ ' , '\\' , $ moduleName );
700
- return $ moduleName ;
701
- } elseif (strpos ($ jsonName , 'magento/magento ' ) !== false || strpos ($ jsonName , 'magento/framework ' ) !== false ) {
696
+ if (isset ($ packageModuleMap [$ jsonName ])) {
697
+ return $ packageModuleMap [$ jsonName ];
698
+ }
699
+
700
+ if (strpos ($ jsonName , 'magento/magento ' ) !== false || strpos ($ jsonName , 'magento/framework ' ) !== false ) {
702
701
$ moduleName = str_replace ('/ ' , "\t" , $ jsonName );
703
702
$ moduleName = str_replace ('framework- ' , "Framework \t" , $ moduleName );
704
703
$ moduleName = str_replace ('- ' , ' ' , $ moduleName );
705
704
$ moduleName = ucwords ($ moduleName );
706
705
$ moduleName = str_replace ("\t" , '\\' , $ moduleName );
707
706
$ moduleName = str_replace (' ' , '' , $ moduleName );
707
+
708
708
return $ moduleName ;
709
709
}
710
+
710
711
return $ jsonName ;
711
712
}
712
713
713
714
/**
714
- * Initialise map of dependencies
715
+ * Initialise map of dependencies.
715
716
*
717
+ * @return void
716
718
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
717
719
* @SuppressWarnings(PHPMD.NPathComplexity)
720
+ * @throws \Exception
718
721
*/
719
722
protected static function _initDependencies ()
720
723
{
724
+ $ packageModuleMap = self ::getPackageModuleMapping ();
721
725
$ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
722
726
foreach ($ jsonFiles as $ file ) {
723
727
$ contents = file_get_contents ($ file );
@@ -726,72 +730,100 @@ protected static function _initDependencies()
726
730
throw new \Exception ("Invalid Json: $ file " );
727
731
}
728
732
$ json = new \Magento \Framework \Config \Composer \Package (json_decode ($ contents ));
729
- $ moduleName = self ::convertModuleName ($ json ->get ('name ' ));
730
- self ::$ _mapDependencies [$ moduleName ] = @(self ::$ _mapDependencies [$ moduleName ] ?: []);
731
-
733
+ $ moduleName = self ::convertModuleName ($ json ->get ('name ' ), $ packageModuleMap );
734
+ if (!isset (self ::$ mapDependencies [$ moduleName ])) {
735
+ self ::$ mapDependencies [$ moduleName ] = [];
736
+ }
732
737
foreach (self ::_getTypes () as $ type ) {
733
- if (!isset (self ::$ _mapDependencies [$ moduleName ][$ type ])) {
734
- self ::$ _mapDependencies [$ moduleName ][$ type ] = [
738
+ if (!isset (self ::$ mapDependencies [$ moduleName ][$ type ])) {
739
+ self ::$ mapDependencies [$ moduleName ][$ type ] = [
735
740
self ::MAP_TYPE_DECLARED => [],
736
741
self ::MAP_TYPE_FOUND => [],
737
742
self ::MAP_TYPE_REDUNDANT => [],
738
743
];
739
744
}
740
745
}
741
746
742
- $ require = $ json ->get ('require ' );
743
- if (isset ($ require ) && !empty ($ require )) {
744
- foreach ($ require as $ requiredModule => $ version ) {
745
- if (0 === strpos ($ requiredModule , 'magento/ ' )
746
- && 'magento/magento-composer-installer ' != $ requiredModule
747
- ) {
748
- $ type = self ::TYPE_HARD ;
749
- self ::_addDependencies (
750
- $ moduleName ,
751
- $ type ,
752
- self ::MAP_TYPE_DECLARED ,
753
- self ::convertModuleName ($ requiredModule )
754
- );
755
- }
756
- }
757
- }
758
- $ suggest = $ json ->get ('suggest ' );
759
- if (isset ($ suggest ) && !empty ($ suggest )) {
760
- foreach ($ suggest as $ requiredModule => $ version ) {
761
- if (0 === strpos ($ requiredModule , 'magento/ ' )
762
- && 'magento/magento-composer-installer ' != $ requiredModule
763
- ) {
764
- $ type = self ::TYPE_SOFT ;
765
- self ::_addDependencies (
766
- $ moduleName ,
767
- $ type ,
768
- self ::MAP_TYPE_DECLARED ,
769
- self ::convertModuleName ($ requiredModule )
770
- );
771
- }
772
- }
773
- }
747
+ $ require = array_keys ((array )$ json ->get ('require ' ));
748
+ self ::addDependencies ($ moduleName , $ require , self ::TYPE_HARD , $ packageModuleMap );
749
+
750
+ $ suggest = array_keys ((array )$ json ->get ('suggest ' ));
751
+ self ::addDependencies ($ moduleName , $ suggest , self ::TYPE_SOFT , $ packageModuleMap );
774
752
}
775
753
}
776
754
777
755
/**
778
- * Add dependency map items
756
+ * Add dependencies to dependency list.
779
757
*
780
- * @param $module
781
- * @param $type
782
- * @param $mapType
783
- * @param $dependencies
758
+ * @param string $moduleName
759
+ * @param array $packageNames
760
+ * @param string $type
761
+ * @param array $packageModuleMap
762
+ *
763
+ * @return void
764
+ */
765
+ private static function addDependencies (
766
+ string $ moduleName ,
767
+ array $ packageNames ,
768
+ string $ type ,
769
+ array $ packageModuleMap
770
+ ): void {
771
+ $ packageNames = array_filter ($ packageNames , function ($ packageName ) use ($ packageModuleMap ) {
772
+ return isset ($ packageModuleMap [$ packageName ]) ||
773
+ 0 === strpos ($ packageName , 'magento/ ' ) && 'magento/magento-composer-installer ' != $ packageName ;
774
+ });
775
+
776
+ foreach ($ packageNames as $ packageName ) {
777
+ self ::addDependency (
778
+ $ moduleName ,
779
+ $ type ,
780
+ self ::MAP_TYPE_DECLARED ,
781
+ self ::convertModuleName ($ packageName , $ packageModuleMap )
782
+ );
783
+ }
784
+ }
785
+
786
+ /**
787
+ * Add dependency map items.
788
+ *
789
+ * @param string $module
790
+ * @param string $type
791
+ * @param string $mapType
792
+ * @param string $dependency
793
+ *
794
+ * @return void
784
795
*/
785
- protected static function _addDependencies ( $ module , $ type , $ mapType , $ dependencies )
796
+ private static function addDependency ( string $ module , string $ type , string $ mapType , string $ dependency ): void
786
797
{
787
- if (! is_array ( $ dependencies )) {
788
- $ dependencies = [ $ dependencies ] ;
798
+ if (isset ( self :: $ mapDependencies [ $ module ][ $ type ][ $ mapType ] )) {
799
+ self :: $ mapDependencies [ $ module ][ $ type ][ $ mapType ][ $ dependency ] = $ dependency ;
789
800
}
790
- foreach ($ dependencies as $ dependency ) {
791
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
792
- self ::$ _mapDependencies [$ module ][$ type ][$ mapType ][$ dependency ] = $ dependency ;
801
+ }
802
+
803
+ /**
804
+ * Returns package name on module name mapping.
805
+ *
806
+ * @return array
807
+ * @throws \Exception
808
+ */
809
+ private static function getPackageModuleMapping (): array
810
+ {
811
+ $ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
812
+
813
+ $ packageModuleMapping = [];
814
+ foreach ($ jsonFiles as $ file ) {
815
+ $ contents = file_get_contents ($ file );
816
+ $ composerJson = json_decode ($ contents );
817
+ if (null == $ composerJson ) {
818
+ throw new \Exception ("Invalid Json: $ file " );
793
819
}
820
+ $ moduleXml = simplexml_load_file (dirname ($ file ) . '/etc/module.xml ' );
821
+ $ moduleName = str_replace ('_ ' , '\\' , (string )$ moduleXml ->module ->attributes ()->name );
822
+ $ packageName = $ composerJson ->name ;
823
+ $ packageModuleMapping [$ packageName ] = $ moduleName ;
794
824
}
825
+
826
+ return $ packageModuleMapping ;
795
827
}
796
828
797
829
/**
@@ -804,9 +836,10 @@ protected static function _addDependencies($module, $type, $mapType, $dependenci
804
836
*/
805
837
protected function _getDependencies ($ module , $ type , $ mapType )
806
838
{
807
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
808
- return self ::$ _mapDependencies [$ module ][$ type ][$ mapType ];
839
+ if (isset (self ::$ mapDependencies [$ module ][$ type ][$ mapType ])) {
840
+ return self ::$ mapDependencies [$ module ][$ type ][$ mapType ];
809
841
}
842
+
810
843
return [];
811
844
}
812
845
@@ -823,8 +856,8 @@ protected function _setDependencies($module, $type, $mapType, $dependencies)
823
856
if (!is_array ($ dependencies )) {
824
857
$ dependencies = [$ dependencies ];
825
858
}
826
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
827
- self ::$ _mapDependencies [$ module ][$ type ][$ mapType ] = $ dependencies ;
859
+ if (isset (self ::$ mapDependencies [$ module ][$ type ][$ mapType ])) {
860
+ self ::$ mapDependencies [$ module ][$ type ][$ mapType ] = $ dependencies ;
828
861
}
829
862
}
830
863
@@ -836,6 +869,6 @@ protected function _setDependencies($module, $type, $mapType, $dependencies)
836
869
*/
837
870
protected function _isFake ($ module )
838
871
{
839
- return isset (self ::$ _mapDependencies [$ module ]) ? false : true ;
872
+ return isset (self ::$ mapDependencies [$ module ]) ? false : true ;
840
873
}
841
874
}
0 commit comments