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 |
||
19 | View Code Duplication | class MultiHttpAdapterException extends \Exception |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $exceptions; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $responses; |
||
30 | |||
31 | /** |
||
32 | * @param array $exceptions |
||
33 | * @param array $responses |
||
34 | */ |
||
35 | 354 | public function __construct(array $exceptions = [], array $responses = []) |
|
42 | |||
43 | 345 | public function clearExceptions() |
|
47 | |||
48 | /** |
||
49 | * @return bool |
||
50 | */ |
||
51 | 54 | public function hasExceptions() |
|
55 | |||
56 | /** |
||
57 | * @return array |
||
58 | */ |
||
59 | 273 | public function getExceptions() |
|
63 | |||
64 | /** |
||
65 | * @param array $exceptions |
||
66 | */ |
||
67 | 345 | public function setExceptions(array $exceptions) |
|
72 | |||
73 | /** |
||
74 | * @param array $exceptions |
||
75 | */ |
||
76 | 345 | public function addExceptions(array $exceptions) |
|
82 | |||
83 | /** |
||
84 | * @param array $exceptions |
||
85 | */ |
||
86 | 9 | public function removeExceptions(array $exceptions) |
|
92 | |||
93 | /** |
||
94 | * @param HttpAdapterException $exception |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | 45 | public function hasException(HttpAdapterException $exception) |
|
102 | |||
103 | /** |
||
104 | * @param HttpAdapterException $exception |
||
105 | */ |
||
106 | 282 | public function addException(HttpAdapterException $exception) |
|
110 | |||
111 | /** |
||
112 | * @param HttpAdapterException $exception |
||
113 | */ |
||
114 | 18 | public function removeException(HttpAdapterException $exception) |
|
119 | |||
120 | 345 | public function clearResponses() |
|
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | 81 | public function hasResponses() |
|
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | 264 | public function getResponses() |
|
140 | |||
141 | /** |
||
142 | * @param array $responses |
||
143 | */ |
||
144 | 345 | public function setResponses(array $responses) |
|
149 | |||
150 | /** |
||
151 | * @param array |
||
152 | */ |
||
153 | 345 | public function addResponses(array $responses) |
|
159 | |||
160 | /** |
||
161 | * @param array $responses |
||
162 | */ |
||
163 | 9 | public function removeResponses(array $responses) |
|
169 | |||
170 | /** |
||
171 | * @param ResponseInterface $response |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | 45 | public function hasResponse(ResponseInterface $response) |
|
179 | |||
180 | /** |
||
181 | * @param ResponseInterface $response |
||
182 | */ |
||
183 | 264 | public function addResponse(ResponseInterface $response) |
|
187 | |||
188 | /** |
||
189 | * @param ResponseInterface $response |
||
190 | */ |
||
191 | 18 | public function removeResponse(ResponseInterface $response) |
|
196 | } |
||
197 |
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.