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 |
||
5 | View Code Duplication | class Error extends \Error |
|
|
|||
6 | { |
||
7 | /** |
||
8 | * @var mixed[] |
||
9 | */ |
||
10 | protected $context = []; |
||
11 | |||
12 | /** |
||
13 | * @param string $message |
||
14 | * @param int $code |
||
15 | * @param \Error|\Exception|null $previous |
||
16 | */ |
||
17 | 33 | public function __construct($message = 'Unknown error', $code = 0, $previous = null) |
|
21 | |||
22 | /** |
||
23 | * @param mixed[] $context |
||
24 | */ |
||
25 | 2 | public function setContext(array $context = []) |
|
26 | { |
||
27 | 2 | $this->context = $context; |
|
28 | 2 | } |
|
29 | |||
30 | /** |
||
31 | * @return mixed[] |
||
32 | */ |
||
33 | 1 | public function getContext() |
|
34 | { |
||
35 | 1 | return $this->context; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | 2 | public function __toString() |
|
45 | |||
46 | /** |
||
47 | * Return Error full trace in string format. |
||
48 | * |
||
49 | * @param \Error|\Exception $ex |
||
50 | * @return string |
||
51 | */ |
||
52 | 2 | public static function toString($ex) |
|
61 | |||
62 | /** |
||
63 | * Return Error full trace in array format. |
||
64 | * |
||
65 | * @param \Error|\Exception $ex |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 1 | public static function toTrace($ex) |
|
72 | |||
73 | /** |
||
74 | * Return Error stack trace in array format. |
||
75 | * |
||
76 | * @param \Error|\Exception $ex |
||
77 | * @return string[] |
||
78 | */ |
||
79 | 4 | public static function toStackTrace($ex) |
|
89 | |||
90 | /** |
||
91 | * Return Error throwable trace in array format. |
||
92 | * |
||
93 | * @param \Error|\Exception $ex |
||
94 | * @return string[] |
||
95 | */ |
||
96 | 4 | public static function toThrowableTrace($ex) |
|
106 | |||
107 | /** |
||
108 | * Return Error stack trace in string format. |
||
109 | * |
||
110 | * @param \Error|\Exception $ex |
||
111 | * @return string |
||
112 | */ |
||
113 | 3 | public static function toStackString($ex) |
|
128 | |||
129 | /** |
||
130 | * Return Error throwable trace in string format. |
||
131 | * |
||
132 | * @param \Error|\Exception $ex |
||
133 | * @return string |
||
134 | */ |
||
135 | 3 | public static function toThrowableString($ex) |
|
150 | } |
||
151 |
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.