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 |
||
8 | class Menu_Cache extends Fragment_Cache { |
||
9 | |||
10 | /** |
||
11 | * @inheritDoc |
||
12 | */ |
||
13 | public function enable() { |
||
32 | |||
33 | /** |
||
34 | * @inheritDoc |
||
35 | */ |
||
36 | public function disable() { |
||
50 | |||
51 | /** |
||
52 | * Return cached menu, using pre-generation hook. |
||
53 | * |
||
54 | * @param string $menu Menu HTML to return. |
||
55 | * @param object $args Menu arguments. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function pre_wp_nav_menu( $menu, $args ) { |
||
72 | |||
73 | /** |
||
74 | * Fake no menu matches to force menu run custom callback. |
||
75 | * |
||
76 | * @deprecated |
||
77 | * |
||
78 | * @param array $args Menu arguments. |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | public function wp_nav_menu_args( $args ) { |
||
100 | |||
101 | /** |
||
102 | * Strip current* classes from menu items, since shared when cached. |
||
103 | * |
||
104 | * @param array $menu_items Array of menu item objects. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function wp_nav_menu_objects( $menu_items ) { |
||
120 | |||
121 | /** |
||
122 | * Save timestamp when menus were last modified for cache salt. |
||
123 | */ |
||
124 | public function update_menus_edited() { |
||
130 | |||
131 | /** |
||
132 | * Invalidate menu cache on related Customizer saves. |
||
133 | */ |
||
134 | public function customize_save() { |
||
155 | |||
156 | /** |
||
157 | * Restore arguments and fetch cached fragment for them. |
||
158 | * |
||
159 | * @deprecated |
||
160 | * |
||
161 | * @param array $args Arguments. |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function fallback_cb( $args ) { |
||
191 | |||
192 | /** |
||
193 | * Generate and timestamp menu output. |
||
194 | * |
||
195 | * @param string $name Fragment name. |
||
196 | * @param array $args Arguments. |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | protected function callback( $name, $args ) { |
||
208 | } |
||
209 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state