ServerGoneAwayExceptionsAwareTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 41.38 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 0
dl 24
loc 58
c 0
b 0
f 0
ccs 0
cts 12
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isGoneAwayException() 12 12 3
A isGoneAwayInUpdateException() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Adgoal\DBALFaultTolerance\Driver;
4
5
use Throwable;
6
7
/**
8
 * Trait ServerGoneAwayExceptionsAwareTrait.
9
 */
10
trait ServerGoneAwayExceptionsAwareTrait
11
{
12
    /** @var string[] */
13
    protected $goneAwayExceptions = [
14
        'MySQL server has gone away', //code 2006
15
        'Lost connection to MySQL server during query', //code 2013
16
        'The MySQL server is running with the --read-only option so it cannot execute this statement',
17
        'Connection refused',
18
        'Lost connection to MySQL server during query', //code 2013
19
        'Deadlock found when trying to get lock', //code 1213
20
        'Lock wait timeout exceeded', //code 1205
21
    ];
22
23
    /** @var string[] */
24
    protected $goneAwayInUpdateExceptions = [
25
        'MySQL server has gone away',
26
        'The MySQL server is running with the --read-only option so it cannot execute this statement',
27
        'Connection refused',
28
        'Deadlock found when trying to get lock', //code 1213
29
        'Lock wait timeout exceeded', //code 1205
30
    ];
31
32
    /**
33
     * @param Throwable $exception
34
     *
35
     * @return bool
36
     */
37 View Code Duplication
    public function isGoneAwayException(Throwable $exception): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $message = $exception->getMessage();
40
41
        foreach ($this->goneAwayExceptions as $goneAwayException) {
42
            if (stripos($message, $goneAwayException) !== false) {
43
                return true;
44
            }
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * @param Throwable $exception
52
     *
53
     * @return bool
54
     */
55 View Code Duplication
    public function isGoneAwayInUpdateException(Throwable $exception): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $message = $exception->getMessage();
58
59
        foreach ($this->goneAwayInUpdateExceptions as $goneAwayException) {
60
            if (stripos($message, $goneAwayException) !== false) {
61
                return true;
62
            }
63
        }
64
65
        return false;
66
    }
67
}
68