Issues (7)

Delegate/ExceptionDelegator.php (6 issues)

1
<?php
2
3
namespace WebStream\Exception\Delegate;
4
5
use WebStream\DI\Injector;
0 ignored issues
show
The type WebStream\DI\Injector 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...
6
use WebStream\Exception\DelegateException;
7
8
/**
9
 * ExceptionDelegator
10
 * @author Ryuichi TANAKA.
11
 * @since 2014/05/05
12
 * @version 0.7
13
 */
14
class ExceptionDelegator
15
{
16
    use Injector;
17
18
    /**
19
     * @var Logger ロガー
0 ignored issues
show
The type WebStream\Exception\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...
20
     */
21
    private $logger;
22
23
    /**
24
     * @var object インスタンス
25
     */
26
    private $instance;
27
28
    /**
29
     * @var string メソッド名
30
     */
31
    private $method;
32
33
    /**
34
     * @var \Exception 例外オブジェクト
35
     */
36
    private $exceptionObject;
37
38
    /**
39
     * @var array<Container> 例外ハンドリングリスト
40
     */
41
    private $exceptionHandler;
42
43
    /**
44
     * constructor
45
     */
46 8
    public function __construct($instance, \Exception $exceptionObject, string $method = null)
47
    {
48 8
        $this->instance = $instance;
49 8
        $this->method = $method;
50 8
        $this->exceptionObject = $exceptionObject;
51 8
        $this->exceptionHandler = [];
52
        $this->logger = new class () { public function __call($name, $args) {} };
0 ignored issues
show
Closing brace must not be followed by any comment or statement on the same line
Loading history...
The closing brace for the class must go on the next line after the body
Loading history...
Documentation Bug introduced by
It seems like new ClassNode() of type anonymous//Delegate/ExceptionDelegator.php$0 is incompatible with the declared type WebStream\Exception\Delegate\Logger of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
    }
54
55
    /**
56
     * 例外ハンドリングリストを設定する
57
     * @param array<Container> 例外ハンドリングリスト
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...
58
     */
59 4
    public function setExceptionHandler(array $exceptionHandler)
60
    {
61 4
        $this->exceptionHandler = $exceptionHandler;
62
    }
63
64
    /**
65
     * 例外を実行する
66
     */
67 8
    public function raise()
68
    {
69 8
        $originException = $this->exceptionObject;
70 8
        $delegateException = null;
71 8
        if ($originException instanceof DelegateException) {
72
            // 複数レイヤ間で例外がやりとりされる場合、すでにDelegateExceptionでラップ済みなので戻す
73
            $originException = $originException->getOriginException();
74
        }
75 8
        $invokeMethods = [];
76 8
        foreach ($this->exceptionHandler as $exceptionHandlerAnnotation) {
77 4
            $exceptions = $exceptionHandlerAnnotation['exceptions'];
78 4
            $refMethod = $exceptionHandlerAnnotation['refMethod'];
79 4
            foreach ($exceptions as $exception) {
80 4
                if (is_a($originException, is_object($exception) ? get_class($exception) : $exception)) {
81
                    // 一つのメソッドに複数の捕捉例外が指定された場合(派生例外クラス含む)、先勝で1回のみ実行する
82
                    // そうでなければ複数回メソッドが実行されるため
83
                    // ただし同一クラス内に限る(親クラスの同一名のメソッドは実行する)
84 4
                    $classpath = $refMethod->class . "#" . $refMethod->name;
85 4
                    if (!array_key_exists($classpath, $invokeMethods)) {
86 4
                        $invokeMethods[$classpath] = $refMethod;
87
                    }
88
                }
89
            }
90
        }
91 8
        if (count($invokeMethods) > 0) {
92 4
            $delegateException = new DelegateException($this->exceptionObject);
93 4
            $delegateException->enableHandled();
94
        }
95 8
        foreach ($invokeMethods as $classpath => $invokeMethod) {
96 4
            $params = ["class" => get_class($this->instance), "method" => $this->method, "exception" => $originException];
97 4
            $invokeMethod->invokeArgs($this->instance, [$params]);
98 4
            $this->logger->debug("Execution of handling is success: " . $classpath);
99
        }
100 8
        throw $delegateException ?: $originException;
101
    }
102
}
103