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 |
||
21 | View Code Duplication | class MultiRequestErroredEvent extends AbstractEvent |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * @var HttpAdapterException[] |
||
25 | */ |
||
26 | private $exceptions; |
||
27 | |||
28 | /** |
||
29 | * @var ResponseInterface[] |
||
30 | */ |
||
31 | private $responses = []; |
||
32 | |||
33 | /** |
||
34 | * @param HttpAdapterInterface $httpAdapter |
||
35 | * @param HttpAdapterException[] $exceptions |
||
36 | */ |
||
37 | 207 | public function __construct(HttpAdapterInterface $httpAdapter, array $exceptions) |
|
43 | |||
44 | 207 | public function clearExceptions() |
|
48 | |||
49 | /** |
||
50 | * @return bool |
||
51 | */ |
||
52 | 54 | public function hasExceptions() |
|
56 | |||
57 | /** |
||
58 | * @return HttpAdapterException[] |
||
59 | */ |
||
60 | 135 | public function getExceptions() |
|
64 | |||
65 | /** |
||
66 | * @param HttpAdapterException[] $exceptions |
||
67 | */ |
||
68 | 207 | public function setExceptions(array $exceptions) |
|
73 | |||
74 | /** |
||
75 | * @param HttpAdapterException[] $exceptions |
||
76 | */ |
||
77 | 207 | public function addExceptions(array $exceptions) |
|
83 | |||
84 | /** |
||
85 | * @param HttpAdapterException[] $exceptions |
||
86 | */ |
||
87 | 9 | public function removeExceptions(array $exceptions) |
|
93 | |||
94 | /** |
||
95 | * @param HttpAdapterException $exception |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 45 | public function hasException(HttpAdapterException $exception) |
|
103 | |||
104 | /** |
||
105 | * @param HttpAdapterException $exception |
||
106 | */ |
||
107 | 207 | public function addException(HttpAdapterException $exception) |
|
111 | |||
112 | /** |
||
113 | * @param HttpAdapterException $exception |
||
114 | */ |
||
115 | 36 | public function removeException(HttpAdapterException $exception) |
|
120 | |||
121 | 108 | public function clearResponses() |
|
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | 72 | public function hasResponses() |
|
133 | |||
134 | /** |
||
135 | * @return ResponseInterface[] |
||
136 | */ |
||
137 | 99 | public function getResponses() |
|
141 | |||
142 | /** |
||
143 | * @param ResponseInterface[] $responses |
||
144 | */ |
||
145 | 108 | public function setResponses(array $responses) |
|
150 | |||
151 | /** |
||
152 | * @param ResponseInterface[] $responses |
||
153 | */ |
||
154 | 108 | public function addResponses(array $responses) |
|
160 | |||
161 | /** |
||
162 | * @param ResponseInterface[] $responses |
||
163 | */ |
||
164 | 9 | public function removeResponses(array $responses) |
|
170 | |||
171 | /** |
||
172 | * @param ResponseInterface $response |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | 36 | public function hasResponse(ResponseInterface $response) |
|
180 | |||
181 | /** |
||
182 | * @param ResponseInterface $response |
||
183 | */ |
||
184 | 90 | public function addResponse(ResponseInterface $response) |
|
188 | |||
189 | /** |
||
190 | * @param ResponseInterface $response |
||
191 | */ |
||
192 | 18 | public function removeResponse(ResponseInterface $response) |
|
197 | } |
||
198 |
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.