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 namespace Anomaly\Streams\Platform\Traits; |
||
13 | trait FiresCallbacks |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * The local callbacks. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $callbacks = []; |
||
22 | |||
23 | /** |
||
24 | * The static callbacks. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected static $listeners = []; |
||
29 | |||
30 | /** |
||
31 | * Register a new callback. |
||
32 | * |
||
33 | * @param $trigger |
||
34 | * @param $callback |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function on($trigger, $callback) |
||
47 | |||
48 | /** |
||
49 | * Register a new listener. |
||
50 | * |
||
51 | * @param $trigger |
||
52 | * @param $callback |
||
53 | * @return $this |
||
54 | */ |
||
55 | public function listen($trigger, $callback) |
||
65 | |||
66 | /** |
||
67 | * Fire a set of closures by trigger. |
||
68 | * |
||
69 | * @param $trigger |
||
70 | * @param array $parameters |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function fire($trigger, array $parameters = []) |
||
123 | |||
124 | /** |
||
125 | * Return if the callback exists. |
||
126 | * |
||
127 | * @param $trigger |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasCallback($trigger) |
||
134 | } |
||
135 |
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.