getExceptionFromResult()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 6.008

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 34
c 2
b 0
f 0
nc 24
nop 1
dl 0
loc 54
ccs 31
cts 33
cp 0.9394
crap 6.008
rs 8.7537

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Japha\Bridge\Driver\Pjb62\Proxy;
6
7
use Psr\Log\LoggerInterface;
8
use Soluble\Japha\Bridge\Driver\Pjb62;
9
use Soluble\Japha\Bridge\Exception;
10
use Soluble\Japha\Bridge\Driver\Pjb62\Client;
11
12
class DefaultThrowExceptionProxyFactory extends Pjb62\ThrowExceptionProxyFactory
13
{
14
    /**
15
     * @var LoggerInterface
16
     */
17
    protected $logger;
18
19
    /**
20
     * @var string
21
     */
22
    protected $defaultException = 'JavaException';
23
24
    /**
25
     * @var array
26
     */
27
    protected $msgPatternsMapping = [
28
        'NoSuchMethodException' => '/(php.java.bridge.NoSuchProcedureException)|(Cause: java.lang.NoSuchMethodException)/',
29
        'ClassNotFoundException' => '/Cause: java.lang.ClassNotFoundException/',
30
        //'InvalidArgumentException' => '/^Invoke failed(.*)php.java.bridge.NoSuchProcedureException/',
31
        'SqlException' => '/^Invoke failed(.*)java.sql.SQLException/',
32
        'NoSuchFieldException' => '/Cause: java.lang.NoSuchFieldException/',
33
        //'NullPointerException' => '/Cause: java.lang.NullPointerException/'
34
    ];
35
36
    /**
37
     * @param Client          $client
38
     * @param LoggerInterface $logger
39
     */
40 25
    public function __construct(Client $client, LoggerInterface $logger)
41
    {
42 25
        parent::__construct($client);
43 25
        $this->logger = $logger;
44 25
    }
45
46
    /**
47
     * @param Pjb62\Exception\JavaException $result
48
     *
49
     * @throws Exception\JavaExceptionInterface
50
     */
51 22
    public function checkResult(Pjb62\Exception\JavaException $result): void
52
    {
53 22
        throw $this->getExceptionFromResult($result);
54
    }
55
56
    /**
57
     * @param Pjb62\Exception\JavaException $result
58
     *
59
     * @return Exception\JavaExceptionInterface
60
     */
61 22
    private function getExceptionFromResult(Pjb62\Exception\JavaException $result): Exception\JavaExceptionInterface
62
    {
63 22
        $message = (string) $result->__get('message')->__toString();
64
65 22
        $exceptionClass = $this->defaultException;
66
67 22
        foreach ($this->msgPatternsMapping as $key => $pattern) {
68 22
            if (preg_match($pattern, $message)) {
69 19
                $exceptionClass = (string) $key;
70 19
                break;
71
            }
72
        }
73
74 22
        $cls = '\\Soluble\\Japha\\Bridge\\Exception\\'.$exceptionClass;
75
76
        // Public message, mask any login/passwords
77 22
        $message = preg_replace('/user=([^&\ ]+)|password=([^&\ ]+)/', '****', $message);
78 22
        $stackTrace = $result->getCause()->__toString();
0 ignored issues
show
Bug introduced by
The method getCause() does not exist on Soluble\Japha\Bridge\Dri...Exception\JavaException. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

78
        $stackTrace = $result->/** @scrutinizer ignore-call */ getCause()->__toString();
Loading history...
79 22
        $code = $result->getCode();
80
81 22
        $driverException = null;
82 22
        if ($result instanceof \Exception) {
0 ignored issues
show
introduced by
$result is always a sub-type of Exception.
Loading history...
83 22
            $driverException = $result;
84
        }
85
86
        // Getting original class name from cause
87 22
        preg_match('/Cause: ([^:]+):/', $message, $matches);
88 22
        if (count($matches) > 1) {
89 22
            $javaExceptionClass = $matches[1];
90
        } else {
91
            $javaExceptionClass = 'Unknown java exception class';
92
        }
93
94
        // Getting cause from message
95 22
        $tmp = explode('Cause: ', $message);
96 22
        if (count($tmp) > 1) {
97 22
            array_shift($tmp);
98 22
            $cause = trim(implode(', ', $tmp));
99
        } else {
100
            $cause = $message;
101
        }
102 22
        $e = new $cls(
103 22
            $message,
104 22
            $cause,
105 22
            $stackTrace,
106 22
            $javaExceptionClass,
107 22
            $code,
108 22
            $driverException,
109 22
            null
110
        );
111
112 22
        $this->logException($e, $exceptionClass);
113
114 22
        return $e;
115
    }
116
117 22
    private function logException(\Throwable $e, string $exceptionClass): void
118
    {
119 22
        $this->logger->error(sprintf(
120 22
            '[soluble-japha] Encountered exception %s: %s, code %s (%s)',
121 22
            $exceptionClass,
122 22
            $e->getMessage(),
123 22
            $e->getCode() ?? '?',
124 22
            get_class($e)
125
        ));
126 22
    }
127
}
128