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 |
||
16 | trait Observable |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $handlers = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $clauses = []; |
||
27 | |||
28 | /** |
||
29 | * @param \Closure|EventHandlerInterface|string $handler |
||
30 | * @param \Closure|EventHandlerInterface|string $condition |
||
31 | * |
||
32 | * @return \Kaylyu\Alipay\Kernel\Clauses\Clause |
||
33 | * |
||
34 | * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException |
||
35 | * @throws \ReflectionException |
||
36 | */ |
||
37 | View Code Duplication | public function push($handler, $condition = '*') |
|
49 | |||
50 | /** |
||
51 | * @param \Closure|EventHandlerInterface|string $handler |
||
52 | * @param \Closure|EventHandlerInterface|string $condition |
||
53 | * |
||
54 | * @return \Kaylyu\Alipay\Kernel\Clauses\Clause |
||
55 | * |
||
56 | * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException |
||
57 | * @throws \ReflectionException |
||
58 | */ |
||
59 | View Code Duplication | public function unshift($handler, $condition = '*') |
|
71 | |||
72 | /** |
||
73 | * @param string $condition |
||
74 | * @param \Closure|EventHandlerInterface|string $handler |
||
75 | * |
||
76 | * @return \Kaylyu\Alipay\Kernel\Clauses\Clause |
||
77 | * |
||
78 | * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException |
||
79 | */ |
||
80 | public function observe($condition, $handler) |
||
84 | |||
85 | /** |
||
86 | * @param string $condition |
||
87 | * @param \Closure|EventHandlerInterface|string $handler |
||
88 | * |
||
89 | * @return \Kaylyu\Alipay\Kernel\Clauses\Clause |
||
90 | * |
||
91 | * @throws \Kaylyu\Alipay\Kernel\Exceptions\InvalidArgumentException |
||
92 | */ |
||
93 | public function on($condition, $handler) |
||
97 | |||
98 | /** |
||
99 | * @param string|int $event |
||
100 | * @param mixed ...$payload |
||
101 | * |
||
102 | * @return mixed|null |
||
103 | */ |
||
104 | public function dispatch($event, $payload) |
||
108 | |||
109 | /** |
||
110 | * @param string|int $event |
||
111 | * @param mixed ...$payload |
||
112 | * |
||
113 | * @return mixed|null |
||
114 | */ |
||
115 | public function notify($event, $payload) |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getHandlers() |
||
153 | |||
154 | /** |
||
155 | * @param mixed $handler |
||
156 | * |
||
157 | * @return \Kaylyu\Alipay\Kernel\Clauses\Clause |
||
158 | */ |
||
159 | protected function newClause($handler): Clause |
||
163 | |||
164 | /** |
||
165 | * @param callable $handler |
||
166 | * @param mixed $payload |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function callHandler(callable $handler, $payload) |
||
185 | |||
186 | /** |
||
187 | * @param $handler |
||
188 | * @author kaylv <[email protected]> |
||
189 | * @return \Closure |
||
190 | * @throws InvalidArgumentException |
||
191 | */ |
||
192 | protected function makeClosure($handler) |
||
220 | |||
221 | /** |
||
222 | * @param $handler |
||
223 | * @param $condition |
||
224 | * |
||
225 | * @return array |
||
226 | * |
||
227 | * @throws \Kaylyu\Alipay\Exceptions\InvalidArgumentException |
||
228 | * @throws \ReflectionException |
||
229 | */ |
||
230 | protected function resolveHandlerAndCondition($handler, $condition): array |
||
238 | } |
||
239 |
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.