|
25 | 25 | use Magento\SemanticVersionChecker\Registry\XmlRegistry;
|
26 | 26 | use PHPSemVerChecker\Registry\Registry;
|
27 | 27 | use PHPSemVerChecker\Report\Report;
|
| 28 | +use Magento\SemanticVersionChecker\Operation\SystemXml\FieldDuplicated; |
28 | 29 |
|
29 | 30 | /**
|
30 | 31 | * Analyzes <kbd>system.xml</kbd> files:
|
@@ -92,14 +93,63 @@ public function analyze($registryBefore, $registryAfter)
|
92 | 93 | $beforeFile = $registryBefore->mapping[XmlRegistry::NODES_KEY][$moduleName];
|
93 | 94 | $this->reportRemovedNodes($beforeFile, $removedNodes);
|
94 | 95 | }
|
| 96 | + print_r('Added Nodes'); |
| 97 | + print_r($addedNodes); |
95 | 98 | if ($addedNodes) {
|
96 | 99 | $afterFile = $registryAfter->mapping[XmlRegistry::NODES_KEY][$moduleName];
|
97 |
| - $this->reportAddedNodes($afterFile, $addedNodes); |
| 100 | + $duplicateNode = $this->reportAddedNodesWithDuplicateCheck($afterFile, $addedNodes, $moduleNodesBefore); |
| 101 | + print_r('Duplicate node '.$duplicateNode.' found'); |
| 102 | + print_r("After file ". $afterFile ); |
| 103 | + if ($duplicateNode) { |
| 104 | + $this->reportDuplicateNodes($afterFile, $addedNodes); |
| 105 | + } else { |
| 106 | + $this->reportAddedNodes($afterFile, $addedNodes); |
| 107 | + } |
98 | 108 | }
|
99 | 109 | }
|
100 | 110 | return $this->report;
|
101 | 111 | }
|
102 | 112 |
|
| 113 | + /** |
| 114 | + * Check and Report duplicate nodes |
| 115 | + * |
| 116 | + * @param $afterFile |
| 117 | + * @param $addedNodes |
| 118 | + * @param $moduleNodesBefore |
| 119 | + * @return bool|void |
| 120 | + */ |
| 121 | + private function reportAddedNodesWithDuplicateCheck($afterFile, $addedNodes, $moduleNodesBefore) |
| 122 | + { |
| 123 | + print_r('Report Added Nodes function called.'); |
| 124 | + foreach ($addedNodes as $nodeId => $node) { |
| 125 | + // Check for duplicates by comparing node content except for 'id' |
| 126 | + $isDuplicate = false; |
| 127 | + foreach ($moduleNodesBefore as $existingNodeId => $existingNode) { |
| 128 | + if ($this->isDuplicateNode($node, $existingNode)) { |
| 129 | + $isDuplicate = true; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + return $isDuplicate; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Check if node is duplicate or not |
| 139 | + * |
| 140 | + * @param $node |
| 141 | + * @param $existingNode |
| 142 | + * @return bool |
| 143 | + */ |
| 144 | + private function isDuplicateNode($node, $existingNode) |
| 145 | + { |
| 146 | + // Remove 'id' key for comparison |
| 147 | + unset($node['id'], $existingNode['id']); |
| 148 | + |
| 149 | + // Compare the remaining parts of the nodes |
| 150 | + return $node == $existingNode; |
| 151 | + } |
| 152 | + |
103 | 153 | /**
|
104 | 154 | * Extracts the node from <var>$registry</var> as an associative array.
|
105 | 155 | *
|
@@ -164,6 +214,26 @@ private function reportAddedNodes(string $file, array $nodes)
|
164 | 214 | }
|
165 | 215 | }
|
166 | 216 |
|
| 217 | + /** |
| 218 | + * Creates reports for <var>$nodes</var> considering that they have been duplicated. |
| 219 | + * |
| 220 | + * @param string $file |
| 221 | + * @param NodeInterface[] $nodes |
| 222 | + */ |
| 223 | + private function reportDuplicateNodes(string $file, array $nodes) |
| 224 | + { |
| 225 | + print_r('Duplicate Nodes switch case'); |
| 226 | + foreach ($nodes as $node) { |
| 227 | + echo "<br/> $node->getPath() <br/>"; |
| 228 | + switch (true) { |
| 229 | + case $node instanceof Field: |
| 230 | + $this->report->add('system', new FieldDuplicated($file, $node->getPath())); |
| 231 | + break; |
| 232 | + default: |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
167 | 237 | /**
|
168 | 238 | * Creates reports for <var>$modules</var> considering that <kbd>system.xml</kbd> has been removed from them.
|
169 | 239 | *
|
|
0 commit comments