13
13
use Magento \SemanticVersionChecker \Node \VirtualType ;
14
14
use Magento \SemanticVersionChecker \Operation \DiXml \VirtualTypeChanged ;
15
15
use Magento \SemanticVersionChecker \Operation \DiXml \VirtualTypeRemoved ;
16
+ use Magento \SemanticVersionChecker \Operation \DiXml \VirtualTypeToTypeChanged ;
16
17
use Magento \SemanticVersionChecker \Registry \XmlRegistry ;
17
18
use PHPSemVerChecker \Registry \Registry ;
18
19
use PHPSemVerChecker \Report \Report ;
@@ -57,10 +58,15 @@ public function analyze($registryBefore, $registryAfter)
57
58
foreach ($ nodesBefore as $ moduleName => $ moduleNodes ) {
58
59
/* @var VirtualType $nodeBefore */
59
60
$ fileBefore = $ registryBefore ->mapping [XmlRegistry::NODES_KEY ][$ moduleName ];
61
+
62
+ // Check if $moduleName exists in $registryAfter->mapping[XmlRegistry::NODES_KEY]
63
+ if (!isset ($ registryAfter ->mapping [XmlRegistry::NODES_KEY ][$ moduleName ])) {
64
+ continue ;
65
+ }
60
66
foreach ($ moduleNodes as $ name => $ nodeBefore ) {
61
67
// search nodesAfter the by name
62
68
$ nodeAfter = $ nodesAfter [$ moduleName ][$ name ] ?? false ;
63
-
69
+ $ fileAfter = $ registryAfter -> mapping [XmlRegistry:: NODES_KEY ][ $ moduleName ];
64
70
if ($ nodeAfter !== false && $ nodeBefore !== $ nodeAfter ) {
65
71
/* @var VirtualType $nodeAfter */
66
72
$ this ->triggerNodeChange ($ nodeBefore , $ nodeAfter , $ fileBefore );
@@ -78,14 +84,68 @@ public function analyze($registryBefore, $registryAfter)
78
84
}
79
85
}
80
86
81
- $ operation = new VirtualTypeRemoved ($ fileBefore , $ name );
87
+ $ finalPath = $ this ->convertClassNameToFilePath ($ fileAfter , $ name , '.php ' );
88
+
89
+ if (file_exists ($ finalPath )) {
90
+ $ operation = new VirtualTypeToTypeChanged ($ fileBefore , $ name );
91
+ } else {
92
+ $ operation = new VirtualTypeRemoved ($ fileBefore , $ name );
93
+ }
82
94
$ this ->report ->add ('di ' , $ operation );
83
95
}
84
96
}
85
97
86
98
return $ this ->report ;
87
99
}
88
100
101
+ /**
102
+ * Method to convert class name to file path
103
+ *
104
+ * @param string $filePath
105
+ * @param string $className
106
+ * @param string $extraString
107
+ * @return string
108
+ */
109
+ private function convertClassNameToFilePath ($ filePath , $ className , $ extraString = '' ): string
110
+ {
111
+ // Split the initial file path to get the base directory.
112
+ $ parts = explode ('/ ' , $ filePath );
113
+ $ classParts = explode ('\\' , $ className );
114
+
115
+ // Find the common part between the file path and class name.
116
+ $ baseDirParts = [];
117
+ foreach ($ parts as $ part ) {
118
+ $ baseDirParts [] = $ part ;
119
+
120
+ if (in_array ($ part , $ classParts )) {
121
+ break ;
122
+ }
123
+ }
124
+
125
+ // Reconstruct the base directory path.
126
+ $ baseDir = implode ('/ ' , $ baseDirParts );
127
+
128
+ // Replace namespace separators with directory separators in the class name.
129
+ $ classFilePath = str_replace ('\\' , '/ ' , $ className );
130
+
131
+ $ position = strpos ($ classFilePath , "/ " );
132
+
133
+ if ($ position !== false ) {
134
+ $ classFilePath = substr ($ classFilePath , $ position );
135
+ }
136
+
137
+ // Combine the base directory and class file path.
138
+ $ fullPath = rtrim ($ baseDir , '/ ' ) . $ classFilePath ;
139
+
140
+
141
+ // Append the extra string if provided.
142
+ if ($ extraString ) {
143
+ $ fullPath .= $ extraString ;
144
+ }
145
+ return $ fullPath ;
146
+ }
147
+
148
+
89
149
/**
90
150
* Return a filtered node list from type {@link VirtualType}
91
151
*
0 commit comments