@@ -237,23 +237,9 @@ private function runBefore(array $filterClassList)
237
237
$ className = $ filterClassInfo [0 ];
238
238
$ arguments = ($ filterClassInfo [1 ] === []) ? null : $ filterClassInfo [1 ];
239
239
240
- if (isset ($ this ->filterClassInstances [$ className ])) {
241
- $ class = $ this ->filterClassInstances [$ className ];
242
- } else {
243
- $ class = new $ className ();
244
-
245
- if (! $ class instanceof FilterInterface) {
246
- throw FilterException::forIncorrectInterface ($ class ::class);
247
- }
248
-
249
- $ this ->filterClassInstances [$ className ] = $ class ;
250
- }
251
-
252
- if (! $ class instanceof FilterInterface) {
253
- throw FilterException::forIncorrectInterface ($ class ::class);
254
- }
240
+ $ instance = $ this ->createFilter ($ className );
255
241
256
- $ result = $ class ->before ($ this ->request , $ arguments );
242
+ $ result = $ instance ->before ($ this ->request , $ arguments );
257
243
258
244
if ($ result instanceof RequestInterface) {
259
245
$ this ->request = $ result ;
@@ -288,19 +274,9 @@ private function runAfter(array $filterClassList): ResponseInterface
288
274
$ className = $ filterClassInfo [0 ];
289
275
$ arguments = ($ filterClassInfo [1 ] === []) ? null : $ filterClassInfo [1 ];
290
276
291
- if (isset ($ this ->filterClassInstances [$ className ])) {
292
- $ class = $ this ->filterClassInstances [$ className ];
293
- } else {
294
- $ class = new $ className ();
295
-
296
- if (! $ class instanceof FilterInterface) {
297
- throw FilterException::forIncorrectInterface ($ class ::class);
298
- }
299
-
300
- $ this ->filterClassInstances [$ className ] = $ class ;
301
- }
277
+ $ instance = $ this ->createFilter ($ className );
302
278
303
- $ result = $ class ->after ($ this ->request , $ this ->response , $ arguments );
279
+ $ result = $ instance ->after ($ this ->request , $ this ->response , $ arguments );
304
280
305
281
if ($ result instanceof ResponseInterface) {
306
282
$ this ->response = $ result ;
@@ -312,6 +288,26 @@ private function runAfter(array $filterClassList): ResponseInterface
312
288
return $ this ->response ;
313
289
}
314
290
291
+ /**
292
+ * @param class-string $className
293
+ */
294
+ private function createFilter (string $ className ): FilterInterface
295
+ {
296
+ if (isset ($ this ->filterClassInstances [$ className ])) {
297
+ return $ this ->filterClassInstances [$ className ];
298
+ }
299
+
300
+ $ instance = new $ className ();
301
+
302
+ if (! $ instance instanceof FilterInterface) {
303
+ throw FilterException::forIncorrectInterface ($ instance ::class);
304
+ }
305
+
306
+ $ this ->filterClassInstances [$ className ] = $ instance ;
307
+
308
+ return $ instance ;
309
+ }
310
+
315
311
/**
316
312
* Runs "Required Filters" for the specified position.
317
313
*
0 commit comments