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 |
||
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 |
|
49 | |||
50 | /** |
||
51 | * @param Throwable $exception |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | View Code Duplication | public function isGoneAwayInUpdateException(Throwable $exception): bool |
|
67 | } |
||
68 |
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.