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 |
||
11 | abstract class Predefined_Options_Field extends Field { |
||
12 | /** |
||
13 | * Stores the field options (if any) |
||
14 | * |
||
15 | * @var array|callable |
||
16 | **/ |
||
17 | protected $options = array(); |
||
18 | |||
19 | /** |
||
20 | * Set the options of this field. |
||
21 | * Accepts either array of data or a callback that returns the data. |
||
22 | * |
||
23 | * @param array|callable $options |
||
24 | */ |
||
25 | 9 | View Code Duplication | public function set_options( $options ) { |
39 | |||
40 | /** |
||
41 | * Add new options to this field. |
||
42 | * Accepts an array of data. |
||
43 | * |
||
44 | * @param array|callable $options |
||
45 | */ |
||
46 | 9 | View Code Duplication | public function add_options( $options ) { |
57 | |||
58 | /** |
||
59 | * Check if there are callbacks and populate the options |
||
60 | */ |
||
61 | protected function load_options() { |
||
84 | |||
85 | /** |
||
86 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
87 | * Will also work with a callable that returns an array. |
||
88 | * |
||
89 | * @param array|callable $options |
||
90 | * @return array |
||
91 | */ |
||
92 | public function parse_options( $options ) { |
||
108 | |||
109 | /** |
||
110 | * Retrieve the current options. |
||
111 | * |
||
112 | * @return array|callable $options |
||
113 | */ |
||
114 | 8 | public function get_options() { |
|
117 | } // END Field |
||
118 |
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.