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 | class Nav_Menu_Item_Container extends Container { |
||
12 | |||
13 | /** |
||
14 | * Array of container clones for every menu item |
||
15 | * |
||
16 | * @see init() |
||
17 | * @var int |
||
18 | */ |
||
19 | protected $menu_item_instances = array(); |
||
20 | |||
21 | /** |
||
22 | * The menu item id this container is for |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $menu_item_id = 0; |
||
27 | |||
28 | /** |
||
29 | * {@inheritDoc} |
||
30 | */ |
||
31 | public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) { |
||
44 | |||
45 | /** |
||
46 | * Perform instance initialization |
||
47 | * |
||
48 | * @param int $menu_item_id Used to pass the correct menu_item_id to the Container object |
||
49 | */ |
||
50 | public function init( $menu_item_id = 0 ) { |
||
63 | |||
64 | /** |
||
65 | * Checks whether the current save request is valid |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | View Code Duplication | public function is_valid_save() { |
|
77 | |||
78 | /** |
||
79 | * Perform save operation after successful is_valid_save() check. |
||
80 | * The call is propagated to all fields in the container. |
||
81 | */ |
||
82 | View Code Duplication | public function save( $data = null ) { |
|
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function is_active() { |
||
97 | |||
98 | /** |
||
99 | * Get environment array for page request (in admin) |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function get_environment_for_request() { |
||
106 | |||
107 | /** |
||
108 | * Perform checks whether the container should be attached during the current request |
||
109 | * |
||
110 | * @return bool True if the container is allowed to be attached |
||
111 | */ |
||
112 | public function is_valid_attach_for_request() { |
||
127 | |||
128 | /** |
||
129 | * Get environment array for object id |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function get_environment_for_object( $object_id ) { |
||
136 | |||
137 | /** |
||
138 | * Check container attachment rules against object id |
||
139 | * |
||
140 | * @param int $object_id |
||
141 | * @return bool |
||
142 | */ |
||
143 | View Code Duplication | public function is_valid_attach_for_object( $object_id = null ) { |
|
156 | |||
157 | /** |
||
158 | * Output the container markup |
||
159 | */ |
||
160 | public function render() { |
||
163 | |||
164 | /** |
||
165 | * Trigger Save for all instances |
||
166 | */ |
||
167 | public function update( $menu_id, $current_menu_item_id ) { |
||
175 | |||
176 | /** |
||
177 | * Render custom fields inside each Nav Menu entry |
||
178 | */ |
||
179 | public function form( $item ) { |
||
187 | |||
188 | /** |
||
189 | * Create a clone of this container with it's own datastore for every menu item |
||
190 | */ |
||
191 | protected function get_clone_for_menu_item( $menu_item_id, $load = true ) { |
||
221 | |||
222 | /** |
||
223 | * Setup custom walker for the Nav Menu entries |
||
224 | */ |
||
225 | public static function edit_walker() { |
||
228 | } |
||
229 |
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.