19
19
*/
20
20
class RemoveUnusedArguments implements UpgradeInterface
21
21
{
22
+ const ARGUMENTS_BLOCK_REGEX_PATTERN = "/\s*<arguments.*\/arguments>/s " ;
23
+
22
24
/**
23
- * Upgrades all test xml files
25
+ * Updates all actionGroup xml files
24
26
*
25
27
* @param InputInterface $input
26
28
* @param OutputInterface $output
@@ -33,36 +35,30 @@ public function execute(InputInterface $input, OutputInterface $output)
33
35
if (empty ($ testPaths [0 ])) {
34
36
$ testPaths = $ scriptUtil ->getAllModulePaths ();
35
37
}
36
- $ actionGroupXmlFiles = $ scriptUtil ->getModuleXmlFilesByScope ($ testPaths , DIRECTORY_SEPARATOR . 'ActionGroup ' . DIRECTORY_SEPARATOR );
38
+ $ xmlFiles = $ scriptUtil ->getModuleXmlFilesByScope ($ testPaths , 'ActionGroup ' );
37
39
$ actionGroupsUpdated = 0 ;
38
40
$ fileSystem = new Filesystem ();
39
- foreach ($ actionGroupXmlFiles as $ actionGroupXml ) {
40
- $ contents = $ actionGroupXml ->getContents ();
41
- $ domDocument = new \DOMDocument ();
42
- $ domDocument ->load ($ actionGroupXml );
43
- $ actionGroupArgumentsCheck = new ActionGroupArgumentsCheck ();
44
- $ unusedArgumentsFound = false ;
41
+ foreach ($ xmlFiles as $ file ) {
42
+ $ contents = $ file ->getContents ();
43
+ $ argumentsCheck = new ActionGroupArgumentsCheck ();
45
44
/** @var DOMElement $actionGroup */
46
- $ actionGroup = $ domDocument -> getElementsByTagName ( ' actionGroup ' )-> item ( 0 );
47
- $ arguments = $ actionGroupArgumentsCheck ->extractActionGroupArguments ($ actionGroup );
48
- $ unusedArguments = $ actionGroupArgumentsCheck ->findUnusedArguments ($ arguments , $ contents );
49
- if (sizeof ($ unusedArguments ) == 0 ) {
45
+ $ actionGroup = $ argumentsCheck -> getActionGroupDomElement ( $ contents );
46
+ $ allArguments = $ argumentsCheck ->extractActionGroupArguments ($ actionGroup );
47
+ $ unusedArguments = $ argumentsCheck ->findUnusedArguments ($ allArguments , $ contents );
48
+ if (empty ($ unusedArguments )) {
50
49
continue ;
51
50
}
52
- foreach ( $ unusedArguments as $ argument ) {
53
- $ unusedArgumentsFound = true ;
54
- $ contents = preg_replace (" /\s*<argument.* " . $ argument . " .*\/>/ " , "" , $ contents );
51
+ //Remove <arguments> block if all arguments are unused
52
+ if (! empty ( array_diff ( $ allArguments , $ unusedArguments ))) {
53
+ $ contents = preg_replace (self :: ARGUMENTS_BLOCK_REGEX_PATTERN , '' , $ contents );
55
54
}
56
- $ newDomDocument = new \DOMDocument ();
57
- $ newDomDocument ->loadXML ($ contents );
58
- if ($ unusedArgumentsFound ) {
59
- if ($ newDomDocument ->getElementsByTagName ("argument " )->length == 0 ) {
60
- $ contents = preg_replace ("/\s*<arguments.*\/arguments>/s " , "" , $ contents );
55
+ else {
56
+ foreach ($ unusedArguments as $ argument ) {
57
+ $ contents = preg_replace ("/\s*<argument.* " .$ argument .".*\/>/ " , '' , $ contents );
61
58
}
62
- $ fileSystem ->dumpFile ($ actionGroupXml ->getRealPath (), $ contents );
63
- $ actionGroupsUpdated ++;
64
-
65
59
}
60
+ $ fileSystem ->dumpFile ($ file ->getRealPath (), $ contents );
61
+ $ actionGroupsUpdated ++;
66
62
}
67
63
return "Finished removing unused action group arguments from " . $ actionGroupsUpdated . " files. " ;
68
64
}
0 commit comments