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 |
||
17 | class PromiseAdapter implements PromiseInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var ReactPromise |
||
21 | */ |
||
22 | private $adaptee; |
||
23 | |||
24 | /** |
||
25 | * PromiseAdapter constructor. |
||
26 | * |
||
27 | * @param ReactPromise $adaptee |
||
28 | */ |
||
29 | public function __construct(ReactPromise $adaptee) |
||
33 | |||
34 | /** |
||
35 | * @param ReactPromise $adaptee |
||
36 | */ |
||
37 | private function setAdaptee(ReactPromise $adaptee) |
||
41 | |||
42 | /** |
||
43 | * @return ReactPromise |
||
44 | */ |
||
45 | public function getAdaptee() |
||
49 | |||
50 | /** |
||
51 | * @param callable|null $onFulfilled |
||
52 | * @param callable|null $onRejected |
||
53 | * @param callable|null $onProgress |
||
54 | * |
||
55 | * @return self |
||
56 | */ |
||
57 | View Code Duplication | public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
75 | |||
76 | /** |
||
77 | * @param callable|null $onFulfilled |
||
78 | * @param callable|null $onRejected |
||
79 | * @param callable|null $onProgress |
||
80 | * |
||
81 | * @return self |
||
82 | */ |
||
83 | View Code Duplication | public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
|
101 | |||
102 | /** |
||
103 | * @param callable $onRejected |
||
104 | * |
||
105 | * @return self |
||
106 | */ |
||
107 | public function otherwise(callable $onRejected) |
||
115 | |||
116 | /** |
||
117 | * @param callable $onAlways |
||
118 | * |
||
119 | * @return self |
||
120 | */ |
||
121 | public function always(callable $onAlways) |
||
129 | |||
130 | /** |
||
131 | * @param callable $onProgress |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | public function progress(callable $onProgress) |
||
143 | } |
||
144 |
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.