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 |
||
11 | class PromiseRejected implements PromiseInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var Error|Exception|ThrowableProxy|string|null |
||
15 | */ |
||
16 | protected $reason; |
||
17 | |||
18 | /** |
||
19 | * @param Error|Exception|string|null |
||
20 | * @throws InvalidArgumentException |
||
21 | */ |
||
22 | 179 | public function __construct($reason = null) |
|
33 | |||
34 | /** |
||
35 | * |
||
36 | */ |
||
37 | 111 | public function __destruct() |
|
41 | |||
42 | /** |
||
43 | * @override |
||
44 | * @inheritDoc |
||
45 | */ |
||
46 | 109 | View Code Duplication | public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
66 | |||
67 | /** |
||
68 | * @override |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | 76 | View Code Duplication | public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
90 | |||
91 | /** |
||
92 | * @override |
||
93 | * @inheritDoc |
||
94 | */ |
||
95 | 2 | public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
104 | |||
105 | /** |
||
106 | * @override |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | 1 | public function success(callable $onSuccess) |
|
113 | |||
114 | /** |
||
115 | * @override |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | 1 | public function failure(callable $onFailure) |
|
122 | |||
123 | /** |
||
124 | * @override |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | 1 | public function abort(callable $onCancel) |
|
131 | |||
132 | /** |
||
133 | * @override |
||
134 | * @inheritDoc |
||
135 | */ |
||
136 | 5 | View Code Duplication | public function always(callable $onFulfilledOrRejected) |
147 | |||
148 | /** |
||
149 | * @override |
||
150 | * @inheritDoc |
||
151 | */ |
||
152 | 1 | public function isPending() |
|
156 | |||
157 | /** |
||
158 | * @override |
||
159 | * @inheritDoc |
||
160 | */ |
||
161 | 3 | public function isFulfilled() |
|
165 | |||
166 | /** |
||
167 | * @override |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | 3 | public function isRejected() |
|
174 | |||
175 | /** |
||
176 | * @override |
||
177 | * @inheritDoc |
||
178 | */ |
||
179 | 3 | public function isCancelled() |
|
183 | |||
184 | /** |
||
185 | * @override |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | public function getPromise() |
||
192 | |||
193 | /** |
||
194 | * @override |
||
195 | * @inheritDoc |
||
196 | */ |
||
197 | public function resolve($value = null) |
||
201 | |||
202 | /** |
||
203 | * @override |
||
204 | * @inheritDoc |
||
205 | */ |
||
206 | public function reject($reason = null) |
||
210 | |||
211 | /** |
||
212 | * @override |
||
213 | * @inheritDoc |
||
214 | */ |
||
215 | 5 | public function cancel($reason = null) |
|
219 | |||
220 | /** |
||
221 | * @see Promise::getValue |
||
222 | */ |
||
223 | protected function getValue() |
||
227 | |||
228 | /** |
||
229 | * @see Promise::getReason |
||
230 | */ |
||
231 | 156 | protected function getReason() |
|
235 | |||
236 | /** |
||
237 | * @param Error|Exception|string $reason |
||
238 | * @throws Error|Exception |
||
239 | */ |
||
240 | 32 | protected function throwError($reason) |
|
249 | } |
||
250 |
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.