@@ -57,7 +57,7 @@ public function __construct(
57
57
if (!$ resetData ) {
58
58
throw new LocalizedException (__ ('Error parsing %1 ' , $ resetPath ));
59
59
}
60
- $ this ->classList = array_replace ( $ this -> classList , $ resetData) ;
60
+ $ this ->classList += $ resetData ;
61
61
}
62
62
$ this ->resetAfterWeakMap = new WeakMap ;
63
63
}
@@ -85,7 +85,7 @@ public function addInstance(object $instance) : void
85
85
{
86
86
if ($ instance instanceof ResetAfterRequestInterface
87
87
|| \method_exists ($ instance , self ::RESET_STATE_METHOD )
88
- || isset ( $ this ->classList [ \get_class ($ instance)] )
88
+ || $ this ->isObjectInClassList ($ instance )
89
89
) {
90
90
$ this ->resetAfterWeakMap [$ instance ] = true ;
91
91
}
@@ -129,8 +129,18 @@ public function setObjectManager(ObjectManagerInterface $objectManager) : void
129
129
$ this ->objectManager = $ objectManager ;
130
130
}
131
131
132
+ public function isObjectInClassList (object $ object )
133
+ {
134
+ foreach ($ this ->classList as $ key => $ value ) {
135
+ if ($ object instanceof $ key ) {
136
+ return true ;
137
+ }
138
+ }
139
+ return false ;
140
+ }
141
+
132
142
/**
133
- * State reset without reflection
143
+ * State reset using reflection (or RESET_STATE_METHOD instead if it exists)
134
144
*
135
145
* @param object $instance
136
146
* @return void
@@ -142,29 +152,23 @@ private function resetStateWithReflection(object $instance)
142
152
$ instance ->{self ::RESET_STATE_METHOD }();
143
153
return ;
144
154
}
145
- $ className = \get_class ($ instance );
155
+ foreach ($ this ->classList as $ className => $ value ) {
156
+ if ($ instance instanceof $ className ) {
157
+ $ this ->resetStateWithReflectionByClassName ($ instance , $ className );
158
+ }
159
+ }
160
+ }
161
+ private function resetStateWithReflectionByClassName (object $ instance , string $ className )
162
+ {
163
+ $ classResetValues = $ this ->classList [$ className ] ?? [];
146
164
$ reflectionClass = $ this ->reflectionCache [$ className ]
147
165
?? $ this ->reflectionCache [$ className ] = new \ReflectionClass ($ className );
148
166
foreach ($ reflectionClass ->getProperties () as $ property ) {
149
- $ type = $ property ->getType ()?->getName();
150
- if (empty ($ type ) && preg_match ('/@var\s+([^\s]+)/ ' , $ property ->getDocComment (), $ matches )) {
151
- $ type = $ matches [1 ];
152
- if (\str_contains ($ type , '[] ' )) {
153
- $ type = 'array ' ;
154
- }
155
- }
156
167
$ name = $ property ->getName ();
157
- if (!in_array ( $ type , [ ' bool ' , ' array ' , ' null ' , ' true ' , ' false ' ], true )) {
168
+ if (!array_key_exists ( $ name , $ classResetValues )) {
158
169
continue ;
159
170
}
160
- $ value = $ this ->classList [$ className ][$ name ] ??
161
- match ($ type ) {
162
- 'bool ' => false ,
163
- 'true ' => true ,
164
- 'false ' => false ,
165
- 'array ' => [],
166
- 'null ' => null ,
167
- };
171
+ $ value = $ classResetValues [$ name ];
168
172
$ property ->setAccessible (true );
169
173
$ property ->setValue ($ instance , $ value );
170
174
$ property ->setAccessible (false );
0 commit comments