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 |
||
7 | class Hook_Manager { |
||
8 | |||
9 | /** |
||
10 | * An array containing all of the hooks that we've registered. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | private $registered_hooks; |
||
15 | |||
16 | /** |
||
17 | * The constructor. |
||
18 | */ |
||
19 | public function __construct() { |
||
22 | |||
23 | /** |
||
24 | * Adds an action to WordPress and registers it internally. |
||
25 | * |
||
26 | * @param string $tag The name of the action which is hooked. |
||
27 | * @param callable $callable The function to call. |
||
28 | * @param int $priority Used to specify the priority of the action. |
||
29 | * @param int $accepted_args Used to specify the number of arguments the callable accepts. |
||
30 | */ |
||
31 | View Code Duplication | public function add_action( $tag, $callable, $priority = 10, $accepted_args = 1 ) { |
|
39 | |||
40 | /** |
||
41 | * Adds a filter to WordPress and registers it internally. |
||
42 | * |
||
43 | * @param string $tag The name of the filter which is hooked. |
||
44 | * @param callable $callable The function to call. |
||
45 | * @param int $priority Used to specify the priority of the filter. |
||
46 | * @param int $accepted_args Used to specify the number of arguments the callable accepts. |
||
47 | */ |
||
48 | View Code Duplication | public function add_filter( $tag, $callable, $priority = 10, $accepted_args = 1 ) { |
|
56 | |||
57 | /** |
||
58 | * Removes all of the registered hooks. |
||
59 | */ |
||
60 | public function reset() { |
||
68 | } |
||
69 |