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 |
||
16 | class Terms extends Module { |
||
17 | /** |
||
18 | * Whitelist for taxonomies we want to sync. |
||
19 | * |
||
20 | * @access private |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private $taxonomy_whitelist; |
||
25 | |||
26 | /** |
||
27 | * Sync module name. |
||
28 | * |
||
29 | * @access public |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public function name() { |
||
36 | |||
37 | /** |
||
38 | * Allows WordPress.com servers to retrieve term-related objects via the sync API. |
||
39 | * |
||
40 | * @param string $object_type The type of object. |
||
41 | * @param int $id The id of the object. |
||
42 | * |
||
43 | * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type. |
||
44 | */ |
||
45 | public function get_object_by_id( $object_type, $id ) { |
||
67 | |||
68 | /** |
||
69 | * Initialize terms action listeners. |
||
70 | * |
||
71 | * @access public |
||
72 | * |
||
73 | * @param callable $callable Action handler callable. |
||
74 | */ |
||
75 | public function init_listeners( $callable ) { |
||
86 | |||
87 | /** |
||
88 | * Initialize terms action listeners for full sync. |
||
89 | * |
||
90 | * @access public |
||
91 | * |
||
92 | * @param callable $callable Action handler callable. |
||
93 | */ |
||
94 | public function init_full_sync_listeners( $callable ) { |
||
97 | |||
98 | /** |
||
99 | * Initialize the module in the sender. |
||
100 | * |
||
101 | * @access public |
||
102 | */ |
||
103 | public function init_before_send() { |
||
107 | |||
108 | /** |
||
109 | * Enqueue the terms actions for full sync. |
||
110 | * |
||
111 | * @access public |
||
112 | * |
||
113 | * @param array $config Full sync configuration for this sync module. |
||
114 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
115 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
116 | * @return array Number of actions enqueued, and next module state. |
||
117 | */ |
||
118 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
122 | |||
123 | /** |
||
124 | * Retrieve the WHERE SQL clause based on the module config. |
||
125 | * |
||
126 | * @access private |
||
127 | * |
||
128 | * @param array $config Full sync configuration for this sync module. |
||
129 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
130 | */ |
||
131 | View Code Duplication | private function get_where_sql( $config ) { |
|
140 | |||
141 | /** |
||
142 | * Retrieve an estimated number of actions that will be enqueued. |
||
143 | * |
||
144 | * @access public |
||
145 | * |
||
146 | * @param array $config Full sync configuration for this sync module. |
||
147 | * @return int Number of items yet to be enqueued. |
||
148 | */ |
||
149 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
164 | |||
165 | /** |
||
166 | * Retrieve the actions that will be sent for this module during a full sync. |
||
167 | * |
||
168 | * @access public |
||
169 | * |
||
170 | * @return array Full sync actions of this module. |
||
171 | */ |
||
172 | public function get_full_sync_actions() { |
||
175 | |||
176 | /** |
||
177 | * Handler for creating and updating terms. |
||
178 | * |
||
179 | * @access public |
||
180 | * |
||
181 | * @param int $term_id Term ID. |
||
182 | * @param int $tt_id Term taxonomy ID. |
||
183 | * @param string $taxonomy Taxonomy slug. |
||
184 | */ |
||
185 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
215 | |||
216 | /** |
||
217 | * Filter blacklisted taxonomies. |
||
218 | * |
||
219 | * @access public |
||
220 | * |
||
221 | * @param array $args Hook args. |
||
222 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
223 | */ |
||
224 | public function filter_blacklisted_taxonomies( $args ) { |
||
233 | |||
234 | /** |
||
235 | * Set the taxonomy whitelist. |
||
236 | * |
||
237 | * @access public |
||
238 | * |
||
239 | * @param array $taxonomies The new taxonomyy whitelist. |
||
240 | */ |
||
241 | public function set_taxonomy_whitelist( $taxonomies ) { |
||
244 | |||
245 | /** |
||
246 | * Set module defaults. |
||
247 | * Define the taxonomy whitelist to be the default one. |
||
248 | * |
||
249 | * @access public |
||
250 | */ |
||
251 | public function set_defaults() { |
||
254 | |||
255 | /** |
||
256 | * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
||
257 | * |
||
258 | * @access public |
||
259 | * |
||
260 | * @param array $args The hook parameters. |
||
261 | * @return array $args The expanded hook parameters. |
||
262 | */ |
||
263 | public function expand_term_taxonomy_id( $args ) { |
||
278 | } |
||
279 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.