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 |
||
13 | trait PromiseTrait |
||
14 | { |
||
15 | /** |
||
16 | * Resolve Promise or value. |
||
17 | * |
||
18 | * @param PromiseInterface|mixed $promiseOrValue |
||
19 | * @return PromiseInterface |
||
20 | * @resolves mixed |
||
21 | * @rejects Error|Exception|string|null |
||
22 | * @cancels Error|Exception|string|null |
||
23 | */ |
||
24 | 302 | public static function doResolve($promiseOrValue = null) |
|
33 | |||
34 | /** |
||
35 | * Reject Promise or value. |
||
36 | * |
||
37 | * @param PromiseInterface|mixed $promiseOrValue |
||
38 | * @return PromiseInterface |
||
39 | * @rejects Error|Exception|string|null |
||
40 | */ |
||
41 | 143 | View Code Duplication | public static function doReject($promiseOrValue = null) |
52 | |||
53 | /** |
||
54 | * Cancel Promise or value. |
||
55 | * |
||
56 | * @param PromiseInterface|mixed $promiseOrValue |
||
57 | * @return PromiseInterface |
||
58 | * @cancels Error|Exception|string|null |
||
59 | */ |
||
60 | 127 | View Code Duplication | public static function doCancel($promiseOrValue = null) |
77 | |||
78 | /** |
||
79 | * Return Promise that will resolve only once all the items in $promisesOrValues have resolved. |
||
80 | * |
||
81 | * Return Promise that will resolve only once all the items in $promisesOrValues have resolved. The resolution |
||
82 | * value of the returned promise will be an array containing the resolution values of each of the items in |
||
83 | * $promisesOrValues. |
||
84 | * |
||
85 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
86 | * @return PromiseInterface |
||
87 | * @resolves mixed |
||
88 | * @rejects Error|Exception|string|null |
||
89 | * @cancels Error|Exception|string|null |
||
90 | */ |
||
91 | 4 | public static function all($promisesOrValues) |
|
97 | |||
98 | /** |
||
99 | * Initiate a competitive race that allows one winner. |
||
100 | * |
||
101 | * Initiate a competitive race that allows one winner. Returns a promise which is resolved in the same way |
||
102 | * the first settled promise resolves. |
||
103 | * |
||
104 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
105 | * @return PromiseInterface |
||
106 | * @resolves mixed |
||
107 | * @rejects Error|Exception|string|null |
||
108 | * @cancels Error|Exception|string|null |
||
109 | */ |
||
110 | 7 | public static function race($promisesOrValues) |
|
143 | |||
144 | /** |
||
145 | * Return a promise that will resolve when any one of the items in $promisesOrValues resolves. |
||
146 | * |
||
147 | * Return a promise that will resolve when any one of the items in $promisesOrValues resolves. The resolution value |
||
148 | * of the returned promise will be the resolution value of the triggering item. |
||
149 | * |
||
150 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
151 | * @return PromiseInterface |
||
152 | * @resolves mixed |
||
153 | * @rejects Error|Exception|string|null |
||
154 | * @cancels Error|Exception|string|null |
||
155 | */ |
||
156 | 7 | public static function any($promisesOrValues) |
|
163 | |||
164 | /** |
||
165 | * Return Promise that will resolve when $howMany of the supplied items in $promisesOrValues resolve. |
||
166 | * |
||
167 | * Return Promise that will resolve when $howMany of the supplied items in $promisesOrValues resolve. The resolution |
||
168 | * value of the returned promise will be an array of length $howMany containing the resolution values of |
||
169 | * the triggering items. |
||
170 | * |
||
171 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
172 | * @param int $howMany |
||
173 | * @return PromiseInterface |
||
174 | * @resolves mixed |
||
175 | * @rejects Error|Exception|string|null |
||
176 | * @cancels Error|Exception|string|null |
||
177 | */ |
||
178 | 18 | public static function some($promisesOrValues, $howMany) |
|
247 | |||
248 | /** |
||
249 | * Map promises and/or values using specified $mapFunc. |
||
250 | * |
||
251 | * @see array_map |
||
252 | * |
||
253 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
254 | * @param callable $mapFunc |
||
255 | * @return PromiseInterface |
||
256 | * @resolves mixed |
||
257 | * @rejects Error|Exception|string|null |
||
258 | * @cancels Error|Exception|string|null |
||
259 | */ |
||
260 | 10 | public static function map($promisesOrValues, callable $mapFunc) |
|
297 | |||
298 | /** |
||
299 | * Reduce Promises and/or values using $reduceFunc with $initialValue being Promise or primitive value. |
||
300 | * |
||
301 | * @see array_reduce |
||
302 | * |
||
303 | * @param PromiseInterface[]|mixed[] $promisesOrValues |
||
304 | * @param callable $reduceFunc |
||
305 | * @param PromiseInterface|mixed|null $initialValue |
||
306 | * @return PromiseInterface |
||
307 | * @resolves mixed |
||
308 | * @rejects Error|Exception|string|null |
||
309 | * @cancels Error|Exception|string|null |
||
310 | */ |
||
311 | 15 | public static function reduce($promisesOrValues, callable $reduceFunc, $initialValue = null) |
|
349 | } |
||
350 |
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.