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 Widget_Cache extends Fragment_Cache { |
||
9 | |||
10 | /** |
||
11 | * @inheritDoc |
||
12 | */ |
||
13 | View Code Duplication | public function enable() { |
|
22 | |||
23 | /** |
||
24 | * @inheritDoc |
||
25 | */ |
||
26 | View Code Duplication | public function disable() { |
|
35 | |||
36 | /** |
||
37 | * Adds timestamp to widget instance to use as salt. |
||
38 | * |
||
39 | * @param array $instance Widget instance to modify. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | public function widget_update_callback( $instance ) { |
||
51 | |||
52 | /** |
||
53 | * Invalidate widget instance cache on Customizer save. |
||
54 | */ |
||
55 | public function update_widget() { |
||
87 | |||
88 | /** |
||
89 | * Set up and echo widget cache |
||
90 | * |
||
91 | * @param array $instance Widget instance data. |
||
92 | * @param object $widget Widget object instance. |
||
93 | * @param array $args Arguments. |
||
94 | * |
||
95 | * @return bool false |
||
96 | */ |
||
97 | public function widget_display_callback( $instance, $widget, $args ) { |
||
112 | |||
113 | /** |
||
114 | * Generate widget output, capture with buffer and timestamp. |
||
115 | * |
||
116 | * @param string $name Fragment name. |
||
117 | * @param array $args Arguments. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | protected function callback( $name, $args ) { |
||
128 | } |
||
129 |
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.