@@ -112,7 +112,7 @@ class DependencyTest extends \PHPUnit\Framework\TestCase
112
112
* )))
113
113
* @var array
114
114
*/
115
- protected static $ _mapDependencies = [];
115
+ protected static $ mapDependencies = [];
116
116
117
117
/**
118
118
* Regex pattern for validation file path of theme
@@ -395,7 +395,7 @@ private function collectDependency($dependency, $currentModule, &$undeclared)
395
395
$ this ->_setDependencies ($ currentModule , $ type , self ::MAP_TYPE_REDUNDANT , $ module );
396
396
}
397
397
398
- $ this ->_addDependencies ($ currentModule , $ type , self ::MAP_TYPE_FOUND , $ nsModule );
398
+ $ this ->addDependency ($ currentModule , $ type , self ::MAP_TYPE_FOUND , $ nsModule );
399
399
}
400
400
401
401
/**
@@ -406,7 +406,7 @@ private function collectDependency($dependency, $currentModule, &$undeclared)
406
406
*/
407
407
public function collectRedundant ()
408
408
{
409
- foreach (array_keys (self ::$ _mapDependencies ) as $ module ) {
409
+ foreach (array_keys (self ::$ mapDependencies ) as $ module ) {
410
410
$ declared = $ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_DECLARED );
411
411
$ found = $ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_FOUND );
412
412
$ found ['Magento\Framework ' ] = 'Magento\Framework ' ;
@@ -422,7 +422,7 @@ public function collectRedundant()
422
422
public function testRedundant ()
423
423
{
424
424
$ output = [];
425
- foreach (array_keys (self ::$ _mapDependencies ) as $ module ) {
425
+ foreach (array_keys (self ::$ mapDependencies ) as $ module ) {
426
426
$ result = [];
427
427
$ redundant = $ this ->_getDependencies ($ module , self ::TYPE_HARD , self ::MAP_TYPE_REDUNDANT );
428
428
if (count ($ redundant )) {
@@ -649,18 +649,16 @@ protected static function _getTypes()
649
649
* Converts a composer json component name into the Magento Module form
650
650
*
651
651
* @param string $jsonName The name of a composer json component or dependency e.g. 'magento/module-theme'
652
+ * @param array $packageModuleMap Mapping package name with module namespace.
652
653
* @return string The corresponding Magento Module e.g. 'Magento\Theme'
653
654
*/
654
- protected static function convertModuleName ($ jsonName )
655
+ protected static function convertModuleName ($ jsonName, array $ packageModuleMap )
655
656
{
656
- if (strpos ($ jsonName , 'magento/module ' ) !== false ) {
657
- $ moduleName = str_replace ('- ' , ' ' , $ jsonName );
658
- $ moduleName = ucwords ($ moduleName );
659
- $ moduleName = str_replace ('module ' , '' , $ moduleName );
660
- $ moduleName = str_replace (' ' , '' , $ moduleName );
661
- $ moduleName = str_replace ('/ ' , '\\' , $ moduleName );
662
- return $ moduleName ;
663
- } elseif (strpos ($ jsonName , 'magento/magento ' ) !== false || strpos ($ jsonName , 'magento/framework ' ) !== false ) {
657
+ if (isset ($ packageModuleMap [$ jsonName ])) {
658
+ return $ packageModuleMap [$ jsonName ];
659
+ }
660
+
661
+ if (strpos ($ jsonName , 'magento/magento ' ) !== false || strpos ($ jsonName , 'magento/framework ' ) !== false ) {
664
662
$ moduleName = str_replace ('/ ' , "\t" , $ jsonName );
665
663
$ moduleName = str_replace ('framework- ' , "Framework \t" , $ moduleName );
666
664
$ moduleName = str_replace ('- ' , ' ' , $ moduleName );
@@ -669,17 +667,21 @@ protected static function convertModuleName($jsonName)
669
667
$ moduleName = str_replace (' ' , '' , $ moduleName );
670
668
return $ moduleName ;
671
669
}
670
+
672
671
return $ jsonName ;
673
672
}
674
673
675
674
/**
676
- * Initialise map of dependencies
675
+ * Initialise map of dependencies.
677
676
*
677
+ * @return void
678
678
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
679
679
* @SuppressWarnings(PHPMD.NPathComplexity)
680
+ * @throws \Exception
680
681
*/
681
682
protected static function _initDependencies ()
682
683
{
684
+ $ packageModuleMap = self ::getPackageModuleMapping ();
683
685
$ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
684
686
foreach ($ jsonFiles as $ file ) {
685
687
$ contents = file_get_contents ($ file );
@@ -688,72 +690,100 @@ protected static function _initDependencies()
688
690
throw new \Exception ("Invalid Json: $ file " );
689
691
}
690
692
$ json = new \Magento \Framework \Config \Composer \Package (json_decode ($ contents ));
691
- $ moduleName = self ::convertModuleName ($ json ->get ('name ' ));
692
- self ::$ _mapDependencies [$ moduleName ] = @(self ::$ _mapDependencies [$ moduleName ] ?: []);
693
-
693
+ $ moduleName = self ::convertModuleName ($ json ->get ('name ' ), $ packageModuleMap );
694
+ if (!isset (self ::$ mapDependencies [$ moduleName ])) {
695
+ self ::$ mapDependencies [$ moduleName ] = [];
696
+ }
694
697
foreach (self ::_getTypes () as $ type ) {
695
- if (!isset (self ::$ _mapDependencies [$ moduleName ][$ type ])) {
696
- self ::$ _mapDependencies [$ moduleName ][$ type ] = [
698
+ if (!isset (self ::$ mapDependencies [$ moduleName ][$ type ])) {
699
+ self ::$ mapDependencies [$ moduleName ][$ type ] = [
697
700
self ::MAP_TYPE_DECLARED => [],
698
701
self ::MAP_TYPE_FOUND => [],
699
702
self ::MAP_TYPE_REDUNDANT => [],
700
703
];
701
704
}
702
705
}
703
706
704
- $ require = $ json ->get ('require ' );
705
- if (isset ($ require ) && !empty ($ require )) {
706
- foreach ($ require as $ requiredModule => $ version ) {
707
- if (0 === strpos ($ requiredModule , 'magento/ ' )
708
- && 'magento/magento-composer-installer ' != $ requiredModule
709
- ) {
710
- $ type = self ::TYPE_HARD ;
711
- self ::_addDependencies (
712
- $ moduleName ,
713
- $ type ,
714
- self ::MAP_TYPE_DECLARED ,
715
- self ::convertModuleName ($ requiredModule )
716
- );
717
- }
718
- }
719
- }
720
- $ suggest = $ json ->get ('suggest ' );
721
- if (isset ($ suggest ) && !empty ($ suggest )) {
722
- foreach ($ suggest as $ requiredModule => $ version ) {
723
- if (0 === strpos ($ requiredModule , 'magento/ ' )
724
- && 'magento/magento-composer-installer ' != $ requiredModule
725
- ) {
726
- $ type = self ::TYPE_SOFT ;
727
- self ::_addDependencies (
728
- $ moduleName ,
729
- $ type ,
730
- self ::MAP_TYPE_DECLARED ,
731
- self ::convertModuleName ($ requiredModule )
732
- );
733
- }
734
- }
735
- }
707
+ $ require = array_keys ((array )$ json ->get ('require ' ));
708
+ self ::addDependencies ($ moduleName , $ require , self ::TYPE_HARD , $ packageModuleMap );
709
+
710
+ $ suggest = array_keys ((array )$ json ->get ('suggest ' ));
711
+ self ::addDependencies ($ moduleName , $ suggest , self ::TYPE_SOFT , $ packageModuleMap );
736
712
}
737
713
}
738
714
739
715
/**
740
- * Add dependency map items
716
+ * Add dependencies to dependency list.
741
717
*
742
- * @param $module
743
- * @param $type
744
- * @param $mapType
745
- * @param $dependencies
718
+ * @param string $moduleName
719
+ * @param array $packageNames
720
+ * @param string $type
721
+ * @param array $packageModuleMap
722
+ *
723
+ * @return void
724
+ */
725
+ private static function addDependencies (
726
+ string $ moduleName ,
727
+ array $ packageNames ,
728
+ string $ type ,
729
+ array $ packageModuleMap
730
+ ) {
731
+ $ packageNames = array_filter ($ packageNames , function ($ packageName ) use ($ packageModuleMap ) {
732
+ return isset ($ packageModuleMap [$ packageName ]) ||
733
+ 0 === strpos ($ packageName , 'magento/ ' ) && 'magento/magento-composer-installer ' != $ packageName ;
734
+ });
735
+
736
+ foreach ($ packageNames as $ packageName ) {
737
+ self ::addDependency (
738
+ $ moduleName ,
739
+ $ type ,
740
+ self ::MAP_TYPE_DECLARED ,
741
+ self ::convertModuleName ($ packageName , $ packageModuleMap )
742
+ );
743
+ }
744
+ }
745
+
746
+ /**
747
+ * Add dependency map items.
748
+ *
749
+ * @param string $module
750
+ * @param string $type
751
+ * @param string $mapType
752
+ * @param string $dependency
753
+ *
754
+ * @return void
746
755
*/
747
- protected static function _addDependencies ( $ module , $ type , $ mapType , $ dependencies )
756
+ private static function addDependency ( string $ module , string $ type , string $ mapType , string $ dependency )
748
757
{
749
- if (! is_array ( $ dependencies )) {
750
- $ dependencies = [ $ dependencies ] ;
758
+ if (isset ( self :: $ mapDependencies [ $ module ][ $ type ][ $ mapType ] )) {
759
+ self :: $ mapDependencies [ $ module ][ $ type ][ $ mapType ][ $ dependency ] = $ dependency ;
751
760
}
752
- foreach ($ dependencies as $ dependency ) {
753
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
754
- self ::$ _mapDependencies [$ module ][$ type ][$ mapType ][$ dependency ] = $ dependency ;
761
+ }
762
+
763
+ /**
764
+ * Returns package name on module name mapping.
765
+ *
766
+ * @return array
767
+ * @throws \Exception
768
+ */
769
+ private static function getPackageModuleMapping ()
770
+ {
771
+ $ jsonFiles = Files::init ()->getComposerFiles (ComponentRegistrar::MODULE , false );
772
+
773
+ $ packageModuleMapping = [];
774
+ foreach ($ jsonFiles as $ file ) {
775
+ $ contents = file_get_contents ($ file );
776
+ $ composerJson = json_decode ($ contents );
777
+ if (null == $ composerJson ) {
778
+ throw new \Exception ("Invalid Json: $ file " );
755
779
}
780
+ $ moduleXml = simplexml_load_file (dirname ($ file ) . '/etc/module.xml ' );
781
+ $ moduleName = str_replace ('_ ' , '\\' , (string )$ moduleXml ->module ->attributes ()->name );
782
+ $ packageName = $ composerJson ->name ;
783
+ $ packageModuleMapping [$ packageName ] = $ moduleName ;
756
784
}
785
+
786
+ return $ packageModuleMapping ;
757
787
}
758
788
759
789
/**
@@ -766,8 +796,8 @@ protected static function _addDependencies($module, $type, $mapType, $dependenci
766
796
*/
767
797
protected function _getDependencies ($ module , $ type , $ mapType )
768
798
{
769
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
770
- return self ::$ _mapDependencies [$ module ][$ type ][$ mapType ];
799
+ if (isset (self ::$ mapDependencies [$ module ][$ type ][$ mapType ])) {
800
+ return self ::$ mapDependencies [$ module ][$ type ][$ mapType ];
771
801
}
772
802
return [];
773
803
}
@@ -785,8 +815,8 @@ protected function _setDependencies($module, $type, $mapType, $dependencies)
785
815
if (!is_array ($ dependencies )) {
786
816
$ dependencies = [$ dependencies ];
787
817
}
788
- if (isset (self ::$ _mapDependencies [$ module ][$ type ][$ mapType ])) {
789
- self ::$ _mapDependencies [$ module ][$ type ][$ mapType ] = $ dependencies ;
818
+ if (isset (self ::$ mapDependencies [$ module ][$ type ][$ mapType ])) {
819
+ self ::$ mapDependencies [$ module ][$ type ][$ mapType ] = $ dependencies ;
790
820
}
791
821
}
792
822
@@ -798,6 +828,6 @@ protected function _setDependencies($module, $type, $mapType, $dependencies)
798
828
*/
799
829
protected function _isFake ($ module )
800
830
{
801
- return isset (self ::$ _mapDependencies [$ module ]) ? false : true ;
831
+ return isset (self ::$ mapDependencies [$ module ]) ? false : true ;
802
832
}
803
833
}
0 commit comments