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 | * The id field in the database. |
||
39 | * |
||
40 | * @access public |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function id_field() { |
||
47 | |||
48 | /** |
||
49 | * The table in the database. |
||
50 | * |
||
51 | * @access public |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function table_name() { |
||
58 | |||
59 | /** |
||
60 | * Allows WordPress.com servers to retrieve term-related objects via the sync API. |
||
61 | * |
||
62 | * @param string $object_type The type of object. |
||
63 | * @param int $id The id of the object. |
||
64 | * |
||
65 | * @return bool|object A WP_Term object, or a row from term_taxonomy table depending on object type. |
||
66 | */ |
||
67 | public function get_object_by_id( $object_type, $id ) { |
||
99 | |||
100 | /** |
||
101 | * Initialize terms action listeners. |
||
102 | * |
||
103 | * @access public |
||
104 | * |
||
105 | * @param callable $callable Action handler callable. |
||
106 | */ |
||
107 | public function init_listeners( $callable ) { |
||
118 | |||
119 | /** |
||
120 | * Initialize terms action listeners for full sync. |
||
121 | * |
||
122 | * @access public |
||
123 | * |
||
124 | * @param callable $callable Action handler callable. |
||
125 | */ |
||
126 | public function init_full_sync_listeners( $callable ) { |
||
129 | |||
130 | /** |
||
131 | * Initialize the module in the sender. |
||
132 | * |
||
133 | * @access public |
||
134 | */ |
||
135 | public function init_before_send() { |
||
139 | |||
140 | /** |
||
141 | * Enqueue the terms actions for full sync. |
||
142 | * |
||
143 | * @access public |
||
144 | * |
||
145 | * @param array $config Full sync configuration for this sync module. |
||
146 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
147 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
148 | * @return array Number of actions enqueued, and next module state. |
||
149 | */ |
||
150 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
154 | |||
155 | /** |
||
156 | * Retrieve the WHERE SQL clause based on the module config. |
||
157 | * |
||
158 | * @access public |
||
159 | * |
||
160 | * @param array $config Full sync configuration for this sync module. |
||
161 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
162 | */ |
||
163 | View Code Duplication | public function get_where_sql( $config ) { |
|
172 | |||
173 | /** |
||
174 | * Retrieve an estimated number of actions that will be enqueued. |
||
175 | * |
||
176 | * @access public |
||
177 | * |
||
178 | * @param array $config Full sync configuration for this sync module. |
||
179 | * @return int Number of items yet to be enqueued. |
||
180 | */ |
||
181 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
196 | |||
197 | /** |
||
198 | * Retrieve the actions that will be sent for this module during a full sync. |
||
199 | * |
||
200 | * @access public |
||
201 | * |
||
202 | * @return array Full sync actions of this module. |
||
203 | */ |
||
204 | public function get_full_sync_actions() { |
||
207 | |||
208 | /** |
||
209 | * Handler for creating and updating terms. |
||
210 | * |
||
211 | * @access public |
||
212 | * |
||
213 | * @param int $term_id Term ID. |
||
214 | * @param int $tt_id Term taxonomy ID. |
||
215 | * @param string $taxonomy Taxonomy slug. |
||
216 | */ |
||
217 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
247 | |||
248 | /** |
||
249 | * Filter blacklisted taxonomies. |
||
250 | * |
||
251 | * @access public |
||
252 | * |
||
253 | * @param array $args Hook args. |
||
254 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
255 | */ |
||
256 | public function filter_blacklisted_taxonomies( $args ) { |
||
265 | |||
266 | /** |
||
267 | * Set the taxonomy whitelist. |
||
268 | * |
||
269 | * @access public |
||
270 | * |
||
271 | * @param array $taxonomies The new taxonomyy whitelist. |
||
272 | */ |
||
273 | public function set_taxonomy_whitelist( $taxonomies ) { |
||
276 | |||
277 | /** |
||
278 | * Set module defaults. |
||
279 | * Define the taxonomy whitelist to be the default one. |
||
280 | * |
||
281 | * @access public |
||
282 | */ |
||
283 | public function set_defaults() { |
||
286 | |||
287 | /** |
||
288 | * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
||
289 | * |
||
290 | * @access public |
||
291 | * |
||
292 | * @param array $args The hook parameters. |
||
293 | * @return array $args The expanded hook parameters. |
||
294 | */ |
||
295 | public function expand_term_taxonomy_id( $args ) { |
||
310 | |||
311 | /** |
||
312 | * Gets a term object based on a given row from the term_relationships database table. |
||
313 | * |
||
314 | * @access public |
||
315 | * |
||
316 | * @param object $relationship A row object from the term_relationships table. |
||
317 | * @return object|bool A term object, or false if term taxonomy doesn't exist. |
||
318 | */ |
||
319 | public function expand_terms_for_relationship( $relationship ) { |
||
322 | } |
||
323 |
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.