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 |
||
20 | class Hook { |
||
21 | |||
22 | /** @var string Function executed by the hook */ |
||
23 | protected $hook_function; |
||
24 | |||
25 | /** @var string Context in which the hook is executed */ |
||
26 | protected $hook_context; |
||
27 | |||
28 | /** |
||
29 | * Constructor for Hook class |
||
30 | * |
||
31 | * @param string $hook_function_in Hook function to be subscribed or executed |
||
32 | * @param string $hook_context_in Hook context to be subscribed or executed |
||
33 | */ |
||
34 | public function __construct($hook_function_in, $hook_context_in = 'all'){ |
||
38 | |||
39 | /** |
||
40 | * Methods for subscribing to Hooks |
||
41 | */ |
||
42 | |||
43 | |||
44 | /** |
||
45 | * Subscribe a class implementing HookSubscriberInterface to the Hook |
||
46 | * The Hook is by default enabled. |
||
47 | * |
||
48 | * @param string $hsubscriber Name of the subscriber module |
||
49 | */ |
||
50 | View Code Duplication | public function subscribe($hsubscriber){ |
|
|
|||
51 | if(HookProvider::getInstance()->isModuleOperational()){ |
||
52 | Database::prepare( |
||
53 | "INSERT IGNORE INTO `##maj_hooks` (majh_hook_function, majh_hook_context, majh_module_name)". |
||
54 | " VALUES (?, ?, ?)" |
||
55 | )->execute(array($this->hook_function, $this->hook_context, $hsubscriber)); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Define the priority for execution of the Hook for the specific HookSubscriberInterface |
||
61 | * |
||
62 | * @param string $hsubscriber Name of the subscriber module |
||
63 | * @param int $priority Priority of execution |
||
64 | */ |
||
65 | View Code Duplication | public function setPriority($hsubscriber, $priority){ |
|
76 | |||
77 | /** |
||
78 | * Enable the hook for a specific HookSubscriberInterface. |
||
79 | * |
||
80 | * @param string $hsubscriber Name of the subscriber module |
||
81 | */ |
||
82 | View Code Duplication | public function enable($hsubscriber){ |
|
93 | |||
94 | /** |
||
95 | * Disable the hook for a specific HookSubscriberInterface. |
||
96 | * |
||
97 | * @param string $hsubscriber Name of the subscriber module |
||
98 | */ |
||
99 | View Code Duplication | public function disable($hsubscriber){ |
|
110 | |||
111 | /** |
||
112 | * Remove the hook for a specific HookSubscriberInterface. |
||
113 | * |
||
114 | * @param string $hsubscriber Name of the subscriber module |
||
115 | */ |
||
116 | View Code Duplication | public function remove($hsubscriber){ |
|
126 | |||
127 | |||
128 | /** |
||
129 | * Methods for execution of the Hook |
||
130 | * |
||
131 | */ |
||
132 | |||
133 | /** |
||
134 | * Return the resuls of the execution of the hook function for a defined list of modules, only for those enabled. |
||
135 | * |
||
136 | * @param string[] $module_names List of Modules to execute |
||
137 | * @return array Results of the hook executions |
||
138 | */ |
||
139 | public function executeOnlyFor(array $module_names) { |
||
152 | |||
153 | /** |
||
154 | * Return the results of the execution of the hook function for all subscribed and enabled modules, in the order defined by their priority. |
||
155 | * Parameters can be passed if the hook requires them. |
||
156 | * |
||
157 | * @return array Results of the hook executions |
||
158 | */ |
||
159 | public function execute(){ |
||
180 | |||
181 | /** |
||
182 | * Returns the number of active modules linked to a hook |
||
183 | * |
||
184 | * @return int Number of active modules |
||
185 | */ |
||
186 | public function getNumberActiveModules(){ |
||
202 | |||
203 | /** |
||
204 | * Return whether any active module is linked to a hook |
||
205 | * |
||
206 | * @return bool True is active modules exist, false otherwise |
||
207 | */ |
||
208 | public function hasAnyActiveModule(){ |
||
211 | |||
212 | } |
||
213 |
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.