CoreExecuteDelegator   F
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 482
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 211
dl 0
loc 482
rs 3.36
c 0
b 0
f 0
wmc 63

12 Methods

Rating   Name   Duplication   Size   Complexity  
C controllerInjector() 0 107 13
A viewInjector() 0 24 4
B getOriginMethod() 0 35 8
C run() 0 48 14
A getInstance() 0 3 2
A __call() 0 3 1
B modelInjector() 0 58 6
A execute() 0 9 3
A helperInjector() 0 38 5
A serviceInjector() 0 38 5
A __get() 0 3 1
A __construct() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like CoreExecuteDelegator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CoreExecuteDelegator, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace WebStream\Delegate;
3
4
use WebStream\DI\Injector;
5
use WebStream\Core\CoreInterface;
6
use WebStream\Core\CoreController;
7
use WebStream\Core\CoreService;
8
use WebStream\Core\CoreModel;
9
use WebStream\Core\CoreView;
10
use WebStream\Core\CoreHelper;
11
use WebStream\Cache\Driver\CacheDriverFactory;
12
use WebStream\Container\Container;
13
use WebStream\Annotation\Attributes\Alias;
14
use WebStream\Annotation\Attributes\Database;
15
use WebStream\Annotation\Attributes\ExceptionHandler;
16
use WebStream\Annotation\Attributes\Filter;
17
use WebStream\Annotation\Attributes\Header;
18
use WebStream\Annotation\Attributes\Query;
19
use WebStream\Annotation\Attributes\Template;
20
use WebStream\Exception\ApplicationException;
21
use WebStream\Exception\SystemException;
22
use WebStream\Exception\DelegateException;
23
use WebStream\Exception\Delegate\ExceptionDelegator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WebStream\Delegate\ExceptionDelegator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
24
use WebStream\Exception\Extend\AnnotationException;
25
use WebStream\Exception\Extend\MethodNotFoundException;
26
use WebStream\Util\CommonUtils;
27
use Doctrine\Common\Annotations\AnnotationException as DoctrineAnnotationException;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\AnnotationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
/**
30
 * CoreExecuteDelegator
31
 * @author Ryuichi TANAKA.
32
 * @since 2015/02/25
33
 * @version 0.4
34
 */
35
class CoreExecuteDelegator
36
{
37
    use Injector, CommonUtils;
38
39
    /**
40
     * @var CoreInterface インスタンス
41
     */
42
    private $instance;
43
44
    /**
45
     * @var CoreInterface 注入済みインスタンス
46
     */
47
    private $injectedInstance;
48
49
    /**
50
     * @var Container 依存コンテナ
51
     */
52
    private $container;
53
54
    /**
55
     * @var Logger ロガー
0 ignored issues
show
Bug introduced by
The type WebStream\Delegate\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
     */
57
    private $logger;
58
59
    /**
60
     * @var AnnotationContainer アノテーション
0 ignored issues
show
Bug introduced by
The type WebStream\Delegate\AnnotationContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
     */
62
    private $annotation;
63
64
    /**
65
     * @var array<AnnotationContainer> 例外ハンドラリスト
66
     */
67
    private $exceptionHandler;
68
69
    /**
70
     * constructor
71
     */
72
    public function __construct(CoreInterface $instance, Container $container)
73
    {
74
        $this->instance = $instance;
75
        $this->container = $container;
76
        $this->logger = $container->logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
Documentation Bug introduced by
It seems like $container->logger can also be of type string. However, the property $logger is declared as type WebStream\Delegate\Logger. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
77
    }
78
79
    /**
80
     * method missing
81
     */
82
    public function __call($method, $arguments)
83
    {
84
        return $this->run($method, $arguments);
85
    }
86
87
    /**
88
     * overload getter
89
     */
90
    public function __get($name)
91
    {
92
        return $this->getInstance()->{$name};
93
    }
94
95
    /**
96
     * 処理を実行する
97
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
98
     * @param array 引数リスト
99
     */
100
    public function run($method, $arguments = [])
101
    {
102
        // すでに注入済みのインスタンスの場合、そのまま実行
103
        if ($this->injectedInstance !== null) {
104
            return $this->execute($method, $arguments);
105
        }
106
107
        try {
108
            $result = null;
109
            if ($this->instance instanceof CoreController) {
110
                $this->controllerInjector($this->getOriginMethod($method), $arguments);
111
            } elseif ($this->instance instanceof CoreService) {
112
                $result = $this->serviceInjector($this->getOriginMethod($method), $arguments);
113
            } elseif ($this->instance instanceof CoreModel) {
114
                $result = $this->modelInjector($this->getOriginMethod($method), $arguments);
115
            } elseif ($this->instance instanceof CoreView) {
116
                $this->viewInjector($method, $arguments);
117
            } elseif ($this->instance instanceof CoreHelper) {
118
                $result = $this->helperInjector($this->getOriginMethod($method), $arguments);
119
            }
120
121
            return $result;
122
        } catch (DoctrineAnnotationException $e) {
123
            throw new AnnotationException($e);
124
        } catch (DelegateException $e) {
125
            // すでにデリゲート済み例外の場合はそのままスロー
126
            // カスタムアノテーション定義で発生する
127
            throw $e;
128
        } catch (\Exception $e) {
129
            $exceptionClass = get_class($e);
130
            switch ($exceptionClass) {
131
                case "Exception":
132
                case "LogicException":
133
                    $e = new ApplicationException($e->getMessage(), 500, $e);
134
                    break;
135
                case "RuntimeException":
136
                    $e = new SystemException($e->getMessage(), 500, $e);
137
                    break;
138
            }
139
140
            $exception = new ExceptionDelegator($this->getInstance(), $e, $method);
141
            $exception->inject('logger', $this->logger);
142
143
            if ($this->exceptionHandler !== null) {
144
                $exception->setExceptionHandler($this->exceptionHandler);
145
            }
146
147
            $exception->raise();
148
        }
149
    }
150
151
    /**
152
     * オリジナルのインスタンスを返却する
153
     * @return CoreInterface インスタンス
154
     */
155
    public function getInstance()
156
    {
157
        return $this->injectedInstance ?: $this->instance;
158
    }
159
160
    /**
161
     * メソッドを実行する
162
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
163
     * @param array 引数リスト
164
     */
165
    private function execute($method, $arguments)
166
    {
167
        // serviceの場合、modelの探索に行くためエラーにはしない
168
        if (!($this->injectedInstance instanceof CoreService) && !method_exists($this->injectedInstance, $method)) {
169
            $class = get_class($this->injectedInstance);
170
            throw new MethodNotFoundException("${class}#${method} is not defined.");
171
        }
172
173
        return call_user_func_array([$this->injectedInstance, $method], $arguments);
174
    }
175
176
    /**
177
     * Controllerに注入する
178
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
179
     * @param array 引数リスト
180
     */
181
    private function controllerInjector($method, $arguments)
182
    {
183
        if (!method_exists($this->instance, $method)) {
184
            $this->injectedInstance = $this->instance;
185
            $class = get_class($this->instance);
186
            $this->instance = null;
187
            throw new MethodNotFoundException("${class}#${method} is not defined.");
188
        }
189
190
        $applicationInfo = $this->container->applicationInfo;
0 ignored issues
show
Bug Best Practice introduced by
The property applicationInfo does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
192
        // テンプレートキャッシュチェック
193
        $pageName = $this->container->coreDelegator->getPageName();
0 ignored issues
show
Bug Best Practice introduced by
The property coreDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getPageName() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
        /** @scrutinizer ignore-call */ 
194
        $pageName = $this->container->coreDelegator->getPageName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
194
        $cacheFile = $applicationInfo->cachePrefix . $this->camel2snake($pageName) . "-" . $this->camel2snake($method);
0 ignored issues
show
Bug introduced by
The property cachePrefix does not exist on string.
Loading history...
195
196
        $factory = new CacheDriverFactory();
197
        $config = new Container(false);
198
        $config->cacheDir = $applicationInfo->applicationRoot . "/app/views/" . $applicationInfo->cacheDir;
0 ignored issues
show
Bug introduced by
The property cacheDir does not exist on string.
Loading history...
Bug introduced by
The property applicationRoot does not exist on string.
Loading history...
Bug Best Practice introduced by
The property cacheDir does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
199
        $config->classPrefix = "view_cache";
0 ignored issues
show
Bug Best Practice introduced by
The property classPrefix does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
200
        $cache = $factory->create("WebStream\Cache\Driver\TemporaryFile", $config);
201
        $cache->inject('logger', $this->logger);
202
        $data = $cache->get($cacheFile);
203
204
        if ($data !== null) {
205
            $this->logger->debug("Template cache read success: $cacheFile.cache");
206
            echo $data;
207
208
            return;
209
        }
210
211
        $resolver = new Resolver($this->container);
212
        $model = $resolver->runService() ?: $resolver->runModel();
213
214
        // アノテーション注入処理は1度しか行わない
215
        if ($this->injectedInstance === null) {
216
            $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug introduced by
The method read() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
            $annotation = $this->annotation = $this->container->annotationDelegator->/** @scrutinizer ignore-call */ read($this->instance, $method);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
217
            $annotationClosure = function ($classpath) use ($annotation) {
218
                if (array_key_exists($classpath, $annotation->annotationInfoList)) {
219
                    return $annotation->annotationInfoList[$classpath];
220
                }
221
                return null;
222
            };
223
224
            // @Header
225
            $header = $annotationClosure(Header::class);
226
227
            // @Filter
228
            $filter = $annotationClosure(Filter::class);
229
230
            // @Template
231
            $template = $annotationClosure(Template::class);
232
233
            // @ExceptionHandler
234
            $this->exceptionHandler = $annotationClosure(ExceptionHandler::class);
235
236
            // custom annotation
237
            $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList);
238
239
            // 各アノテーションでエラーがあった場合この時点で例外を起こす。
240
            // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない
241
            $exception = $annotation->exception;
242
            if ($exception instanceof ExceptionDelegator) {
243
                $exception->raise();
244
            }
245
246
            // initialize filter
247
            $container = $this->container;
248
            $container->model = $model;
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
249
            foreach ($filter->initialize as $refMethod) {
250
                $refMethod->invokeArgs($this->instance, [$container]);
251
            }
252
253
            // before filter
254
            foreach ($filter->before as $refMethod) {
255
                $refMethod->invoke($this->instance);
256
            }
257
258
            $this->injectedInstance = $this->instance;
259
            $this->execute($method, $arguments);
260
261
            // draw template
262
            if ($template !== null) {
263
                $mimeType = empty($header) ? "html" : $header[0]['contentType'];
264
                $refClass = new \ReflectionClass($template[0]['engine']);
265
                $templateEngine = $refClass->newInstance($this->container);
266
267
                $view = $resolver->runView();
268
                $view->setTemplateEngine($templateEngine);
269
                $view->draw([
270
                    'model' => $model,
271
                    'helper' => $resolver->runHelper(),
272
                    'mimeType' => $mimeType,
273
                    'filename' => $template[0]['filename']
274
                ]);
275
276
                if (array_key_exists('cacheTime', $template[0])) {
277
                    $cacheFile = $applicationInfo->cachePrefix . $this->camel2snake($pageName) . "-" . $this->camel2snake($method);
278
                    $view->templateCache($cacheFile, ob_get_contents(), $template[0]['cacheTime']);
279
                }
280
            }
281
282
            // after filter
283
            foreach ($filter->after as $refMethod) {
284
                $refMethod->invoke($this->injectedInstance);
285
            }
286
287
            $this->instance = null;
288
        }
289
    }
290
291
    /**
292
     * Serviceに注入する
293
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
294
     * @param array 引数リスト
295
     */
296
    private function serviceInjector($method, $arguments)
297
    {
298
        // アノテーション注入処理は1度しか行わない
299
        if ($this->injectedInstance === null) {
300
            $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
301
            $annotationClosure = function ($classpath) use ($annotation) {
302
                if (array_key_exists($classpath, $annotation->annotationInfoList)) {
303
                    return $annotation->annotationInfoList[$classpath];
304
                }
305
                return null;
306
            };
307
308
            // @Filter
309
            $filter = $annotationClosure(Filter::class);
310
311
            // @ExceptionHandler
312
            $exceptionHandler = $annotationClosure(ExceptionHandler::class);
313
314
            // custom annotation
315
            $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList);
316
317
            // 各アノテーションでエラーがあった場合この時点で例外を起こす。
318
            // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない
319
            $exception = $annotation->exception;
320
            if ($exception instanceof ExceptionDelegator) {
321
                $this->exceptionHandler = $exceptionHandler;
322
                $exception->raise();
323
            }
324
325
            foreach ($filter->initialize as $refMethod) {
326
                $refMethod->invokeArgs($this->instance, [$this->container]);
327
            }
328
329
            $this->injectedInstance = $this->instance;
330
            $this->instance = null;
331
        }
332
333
        return $this->execute($method, $arguments);
334
    }
335
336
    /**
337
     * Modelに注入する
338
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
339
     * @param array 引数リスト
340
     */
341
    private function modelInjector($method, $arguments)
342
    {
343
        // アノテーション注入処理は1度しか行わない
344
        if ($this->injectedInstance === null) {
345
            $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
346
            $annotationClosure = function ($classpath) use ($annotation) {
347
                if (array_key_exists($classpath, $annotation->annotationInfoList)) {
348
                    return $annotation->annotationInfoList[$classpath];
349
                }
350
                return null;
351
            };
352
353
            // @Filter
354
            $filter = $annotationClosure(Filter::class);
355
356
            // @ExceptionHandler
357
            $exceptionHandler = $annotationClosure(ExceptionHandler::class);
358
359
            // @Database
360
            $database = $annotationClosure(Database::class);
361
362
            // @Query
363
            $query = $annotationClosure(Query::class);
364
365
            // custom annotation
366
            $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList);
367
368
            // 各アノテーションでエラーがあった場合この時点で例外を起こす。
369
            // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない
370
            $exception = $annotation->exception;
371
            if ($exception instanceof ExceptionDelegator) {
372
                $this->exceptionHandler = $exceptionHandler;
373
                $exception->raise();
374
            }
375
376
            $connectionContainerList = [];
377
            foreach (($database ?? []) as $databaseInfo) {
378
                $container = new Container(false);
379
                $container->filepath = $databaseInfo['filepath'];
0 ignored issues
show
Bug Best Practice introduced by
The property filepath does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
380
                $container->configPath = $databaseInfo['configPath'];
0 ignored issues
show
Bug Best Practice introduced by
The property configPath does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
381
                $container->driverClassPath = $databaseInfo['driverClassPath'];
0 ignored issues
show
Bug Best Practice introduced by
The property driverClassPath does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
382
                $connectionContainerList[] = $container;
383
            }
384
385
            $connectionContainer = new Container();
386
            $connectionContainer->connectionContainerList = $connectionContainerList;
0 ignored issues
show
Bug Best Practice introduced by
The property connectionContainerList does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
387
            $connectionContainer->register("queryInfo", $query);
388
            $connectionContainer->logger = $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Container\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
389
390
            foreach ($filter->initialize as $refMethod) {
391
                $refMethod->invokeArgs($this->instance, [$connectionContainer]);
392
            }
393
394
            $this->injectedInstance = $this->instance;
395
            $this->instance = null;
396
        }
397
398
        return $this->execute($method, $arguments);
399
    }
400
401
    /**
402
     * Viewに注入する
403
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
404
     * @param array 引数リスト
405
     */
406
    private function viewInjector($method, $arguments)
407
    {
408
        // アノテーション注入処理は1度しか行わない
409
        if ($this->injectedInstance === null) {
410
            $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
411
            $annotationClosure = function ($classpath) use ($annotation) {
0 ignored issues
show
Unused Code introduced by
The assignment to $annotationClosure is dead and can be removed.
Loading history...
412
                if (array_key_exists($classpath, $annotation->annotationInfoList)) {
413
                    return $annotation->annotationInfoList[$classpath];
414
                }
415
                return null;
416
            };
417
418
            // 各アノテーションでエラーがあった場合この時点で例外を起こす。
419
            // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない
420
            $exception = $annotation->exception;
421
            if ($exception instanceof ExceptionDelegator) {
422
                $exception->raise();
423
            }
424
425
            $this->injectedInstance = $this->instance;
426
            $this->instance = null;
427
        }
428
429
        $this->execute($method, $arguments);
430
    }
431
432
    /**
433
     * Helperに注入する
434
     * @param string メソッド名
0 ignored issues
show
Documentation Bug introduced by
The doc comment メソッド名 at position 0 could not be parsed: Unknown type name 'メソッド名' at position 0 in メソッド名.
Loading history...
435
     * @param array 引数リスト
436
     */
437
    private function helperInjector($method, $arguments)
438
    {
439
        // アノテーション注入処理は1度しか行わない
440
        if ($this->injectedInstance === null) {
441
            $annotation = $this->annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
442
            $annotationClosure = function ($classpath) use ($annotation) {
443
                if (array_key_exists($classpath, $annotation->annotationInfoList)) {
444
                    return $annotation->annotationInfoList[$classpath];
445
                }
446
                return null;
447
            };
448
449
            // @Filter
450
            $filter = $annotationClosure(Filter::class);
451
452
            // @ExceptionHandler
453
            $exceptionHandler = $annotationClosure(ExceptionHandler::class);
454
455
            // custom annotation
456
            $this->instance->__customAnnotation($this->annotation->customAnnotationInfoList);
457
458
            // 各アノテーションでエラーがあった場合この時点で例外を起こす。
459
            // 例外発生を遅延実行させないとエラーになっていないアノテーション情報が取れない
460
            $exception = $annotation->exception;
461
            if ($exception instanceof ExceptionDelegator) {
462
                $this->exceptionHandler = $exceptionHandler;
463
                $exception->raise();
464
            }
465
466
            foreach ($filter->initialize as $refMethod) {
467
                $refMethod->invokeArgs($this->instance, [$this->container]);
468
            }
469
470
            $this->injectedInstance = $this->instance;
471
            $this->instance = null;
472
        }
473
474
        return $this->execute($method, $arguments);
475
    }
476
477
    /**
478
     * 実メソッド名を返却する
479
     * @param  stirng $method エイリアスメソッド名
0 ignored issues
show
Bug introduced by
The type WebStream\Delegate\stirng was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
480
     * @return stirng 実メソッド名
481
     */
482
    private function getOriginMethod($method)
483
    {
484
        // 実メソッドが定義済みの場合、エイリアスメソッド参照はしない
485
        if (method_exists($this->instance, $method)) {
486
            return $method;
487
        }
488
489
        $annotation = $this->container->annotationDelegator->read($this->instance, $method);
0 ignored issues
show
Bug Best Practice introduced by
The property annotationDelegator does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
490
        $annotationClosure = function ($classpath) use ($annotation) {
491
            if (array_key_exists($classpath, $annotation->annotationInfoList)) {
492
                return $annotation->annotationInfoList[$classpath];
493
            }
494
            return null;
495
        };
496
497
        $annotation = $annotationClosure(Alias::class);
498
499
        $originMethod = null;
500
        foreach ($annotation as $alias) {
501
            if ($originMethod !== null && $alias['method'] !== null) {
502
                throw new AnnotationException("Alias method of the same name is defined: $method");
503
            }
504
            if ($alias['method'] !== null) {
505
                $originMethod = $alias['method'];
506
            }
507
        }
508
509
        if ($originMethod !== null) {
510
            $class = get_class($this->instance);
511
            $this->logger->debug("Alias method found. Transfer from ${class}#${method} to ${class}#${originMethod}.");
512
        } else {
513
            $originMethod = $method;
514
        }
515
516
        return $originMethod;
517
    }
518
}
519