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 Loader implements LoaderContract { |
||
18 | /** |
||
19 | * Array of action hooks to attach. |
||
20 | * |
||
21 | * @var array[] |
||
22 | */ |
||
23 | protected $actions = array(); |
||
24 | |||
25 | /** |
||
26 | * Array of filter hooks to attach. |
||
27 | * |
||
28 | * @var array[] |
||
29 | */ |
||
30 | protected $filters = array(); |
||
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | 6 | public function run() { |
|
54 | |||
55 | /** |
||
56 | * Register a service with the loader. |
||
57 | * |
||
58 | * @param mixed $service |
||
59 | */ |
||
60 | public function register( $service ) { |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | * |
||
77 | * @param HasActions $service |
||
78 | */ |
||
79 | 3 | View Code Duplication | public function register_actions( HasActions $service ) { |
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | * |
||
95 | * @param HasFilters $service |
||
96 | */ |
||
97 | 3 | View Code Duplication | public function register_filters( HasFilters $service ) { |
109 | |||
110 | /** |
||
111 | * {@inheritDoc} |
||
112 | * |
||
113 | * @param HasShortcode $service |
||
114 | */ |
||
115 | 3 | public function register_shortcode( HasShortcode $service ) { |
|
118 | |||
119 | /** |
||
120 | * Utility to register the actions and hooks into a single collection. |
||
121 | * |
||
122 | * @param array $hooks |
||
123 | * @param string $hook |
||
124 | * @param object $service |
||
125 | * @param string $method |
||
126 | * @param int $priority |
||
127 | * @param int $accepted_args |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | 6 | protected function add( $hooks, $hook, $service, $method, $priority, $accepted_args ) { |
|
142 | } |
||
143 |
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.