@@ -33,7 +33,6 @@ class StaticProperties
33
33
* @var array
34
34
*/
35
35
protected static $ _classesToSkip = [
36
- 'Mage ' ,
37
36
\Magento \Framework \App \ObjectManager::class,
38
37
\Magento \TestFramework \Helper \Bootstrap::class,
39
38
\Magento \TestFramework \Event \Magento::class,
@@ -107,14 +106,31 @@ protected static function _isClassInCleanableFolders($classFile)
107
106
return false ; // File is not in an "include" directory
108
107
}
109
108
109
+ /**
110
+ * @var \ReflectionClass[]
111
+ */
112
+ static protected $ classes = [];
113
+
114
+ /**
115
+ * @param string $class
116
+ * @return \ReflectionClass
117
+ */
118
+ private static function getReflectionClass ($ class )
119
+ {
120
+ if (!isset (self ::$ classes [$ class ])) {
121
+ self ::$ classes [$ class ] = new \ReflectionClass ($ class );
122
+ }
123
+ return self ::$ classes [$ class ];
124
+ }
125
+
110
126
/**
111
127
* Restore static variables (after running controller test case)
112
128
* @TODO: refactor all code where objects are stored to static variables to use object manager instead
113
129
*/
114
130
public static function restoreStaticVariables ()
115
131
{
116
132
foreach (array_keys (self ::$ backupStaticVariables ) as $ class ) {
117
- $ reflectionClass = new \ ReflectionClass ($ class );
133
+ $ reflectionClass = self :: getReflectionClass ($ class );
118
134
$ staticProperties = $ reflectionClass ->getProperties (\ReflectionProperty::IS_STATIC );
119
135
foreach ($ staticProperties as $ staticProperty ) {
120
136
$ staticProperty ->setAccessible (true );
@@ -129,44 +145,50 @@ public static function restoreStaticVariables()
129
145
*/
130
146
public static function backupStaticVariables ()
131
147
{
132
- $ classFiles = Files::init ()->getPhpFiles (
133
- Files::INCLUDE_APP_CODE
134
- | Files::INCLUDE_LIBS
135
- | Files::INCLUDE_TESTS
148
+ if (count (self ::$ backupStaticVariables ) > 0 ) {
149
+ return ;
150
+ }
151
+ $ classFiles = array_filter (
152
+ Files::init ()->getPhpFiles (
153
+ Files::INCLUDE_APP_CODE
154
+ | Files::INCLUDE_LIBS
155
+ | Files::INCLUDE_TESTS
156
+ ),
157
+ function ($ classFile ) {
158
+ return StaticProperties::_isClassInCleanableFolders ($ classFile )
159
+ && strpos (file_get_contents ($ classFile ), ' static ' ) > 0 ;
160
+ }
136
161
);
137
162
$ namespacePattern = '/namespace [a-zA-Z0-9 \\\\]+;/ ' ;
138
163
$ classPattern = '/\nclass [a-zA-Z0-9_]+/ ' ;
139
164
foreach ($ classFiles as $ classFile ) {
140
- if (self ::_isClassInCleanableFolders ($ classFile )) {
141
- $ file = @fopen ($ classFile , 'r ' );
142
- $ code = fread ($ file , 4096 );
143
- preg_match ($ namespacePattern , $ code , $ namespace );
144
- preg_match ($ classPattern , $ code , $ class );
145
- if (!isset ($ namespace [0 ]) || !isset ($ class [0 ])) {
146
- fclose ($ file );
147
- continue ;
148
- }
149
- // trim namespace and class name
150
- $ namespace = substr ($ namespace [0 ], 10 , strlen ($ namespace [0 ]) - 11 );
151
- $ class = substr ($ class [0 ], 7 , strlen ($ class [0 ]) - 7 );
152
- $ className = $ namespace . '\\' . $ class ;
153
-
154
- try {
155
- $ reflectionClass = new \ReflectionClass ($ className );
156
- } catch (\Exception $ e ) {
157
- fclose ($ file );
158
- continue ;
159
- }
160
- if (self ::_isClassCleanable ($ reflectionClass )) {
161
- $ staticProperties = $ reflectionClass ->getProperties (\ReflectionProperty::IS_STATIC );
162
- foreach ($ staticProperties as $ staticProperty ) {
163
- $ staticProperty ->setAccessible (true );
164
- $ value = $ staticProperty ->getValue ();
165
- self ::$ backupStaticVariables [$ className ][$ staticProperty ->getName ()] = $ value ;
166
- }
165
+ $ file = @fopen ($ classFile , 'r ' );
166
+ $ code = fread ($ file , 4096 );
167
+ fclose ($ file );
168
+ preg_match ($ namespacePattern , $ code , $ namespace );
169
+ preg_match ($ classPattern , $ code , $ class );
170
+ if (!isset ($ namespace [0 ]) || !isset ($ class [0 ])) {
171
+ continue ;
172
+ }
173
+ // trim namespace and class name
174
+ $ namespace = substr ($ namespace [0 ], 10 , strlen ($ namespace [0 ]) - 11 );
175
+ $ class = substr ($ class [0 ], 7 , strlen ($ class [0 ]) - 7 );
176
+ $ className = $ namespace . '\\' . $ class ;
177
+
178
+ try {
179
+ $ reflectionClass = self ::getReflectionClass ($ className );
180
+ } catch (\Exception $ e ) {
181
+ continue ;
182
+ }
183
+ if (self ::_isClassCleanable ($ reflectionClass )) {
184
+ $ staticProperties = $ reflectionClass ->getProperties (\ReflectionProperty::IS_STATIC );
185
+ foreach ($ staticProperties as $ staticProperty ) {
186
+ $ staticProperty ->setAccessible (true );
187
+ $ value = $ staticProperty ->getValue ();
188
+ self ::$ backupStaticVariables [$ className ][$ staticProperty ->getName ()] = $ value ;
167
189
}
168
- fclose ($ file );
169
190
}
191
+
170
192
}
171
193
}
172
194
0 commit comments