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 a term object 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|\WP_Term |
||
44 | */ |
||
45 | public function get_object_by_id( $object_type, $id ) { |
||
62 | |||
63 | /** |
||
64 | * Initialize terms action listeners. |
||
65 | * |
||
66 | * @access public |
||
67 | * |
||
68 | * @param callable $callable Action handler callable. |
||
69 | */ |
||
70 | public function init_listeners( $callable ) { |
||
81 | |||
82 | /** |
||
83 | * Initialize terms action listeners for full sync. |
||
84 | * |
||
85 | * @access public |
||
86 | * |
||
87 | * @param callable $callable Action handler callable. |
||
88 | */ |
||
89 | public function init_full_sync_listeners( $callable ) { |
||
92 | |||
93 | /** |
||
94 | * Initialize the module in the sender. |
||
95 | * |
||
96 | * @access public |
||
97 | */ |
||
98 | public function init_before_send() { |
||
102 | |||
103 | /** |
||
104 | * Enqueue the terms actions for full sync. |
||
105 | * |
||
106 | * @access public |
||
107 | * |
||
108 | * @param array $config Full sync configuration for this sync module. |
||
109 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
110 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
111 | * @return array Number of actions enqueued, and next module state. |
||
112 | */ |
||
113 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
117 | |||
118 | /** |
||
119 | * Retrieve the WHERE SQL clause based on the module config. |
||
120 | * |
||
121 | * @access private |
||
122 | * |
||
123 | * @param array $config Full sync configuration for this sync module. |
||
124 | * @return string WHERE SQL clause, or `null` if no comments are specified in the module config. |
||
125 | */ |
||
126 | View Code Duplication | private function get_where_sql( $config ) { |
|
135 | |||
136 | /** |
||
137 | * Retrieve an estimated number of actions that will be enqueued. |
||
138 | * |
||
139 | * @access public |
||
140 | * |
||
141 | * @param array $config Full sync configuration for this sync module. |
||
142 | * @return int Number of items yet to be enqueued. |
||
143 | */ |
||
144 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
145 | global $wpdb; |
||
146 | |||
147 | $query = "SELECT count(*) FROM $wpdb->term_taxonomy"; |
||
148 | |||
149 | $where_sql = $this->get_where_sql( $config ); |
||
150 | if ( $where_sql ) { |
||
151 | $query .= ' WHERE ' . $where_sql; |
||
152 | } |
||
153 | |||
154 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
||
155 | $count = $wpdb->get_var( $query ); |
||
156 | |||
157 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Retrieve the actions that will be sent for this module during a full sync. |
||
162 | * |
||
163 | * @access public |
||
164 | * |
||
165 | * @return array Full sync actions of this module. |
||
166 | */ |
||
167 | public function get_full_sync_actions() { |
||
170 | |||
171 | /** |
||
172 | * Handler for creating and updating terms. |
||
173 | * |
||
174 | * @access public |
||
175 | * |
||
176 | * @param int $term_id Term ID. |
||
177 | * @param int $tt_id Term taxonomy ID. |
||
178 | * @param string $taxonomy Taxonomy slug. |
||
179 | */ |
||
180 | public function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
210 | |||
211 | /** |
||
212 | * Filter blacklisted taxonomies. |
||
213 | * |
||
214 | * @access public |
||
215 | * |
||
216 | * @param array $args Hook args. |
||
217 | * @return array|boolean False if not whitelisted, the original hook args otherwise. |
||
218 | */ |
||
219 | public function filter_blacklisted_taxonomies( $args ) { |
||
228 | |||
229 | /** |
||
230 | * Set the taxonomy whitelist. |
||
231 | * |
||
232 | * @access public |
||
233 | * |
||
234 | * @param array $taxonomies The new taxonomyy whitelist. |
||
235 | */ |
||
236 | public function set_taxonomy_whitelist( $taxonomies ) { |
||
239 | |||
240 | /** |
||
241 | * Set module defaults. |
||
242 | * Define the taxonomy whitelist to be the default one. |
||
243 | * |
||
244 | * @access public |
||
245 | */ |
||
246 | public function set_defaults() { |
||
249 | |||
250 | /** |
||
251 | * Expand the term taxonomy IDs to terms within a hook before they are serialized and sent to the server. |
||
252 | * |
||
253 | * @access public |
||
254 | * |
||
255 | * @param array $args The hook parameters. |
||
256 | * @return array $args The expanded hook parameters. |
||
257 | */ |
||
258 | public function expand_term_taxonomy_id( $args ) { |
||
273 | } |
||
274 |
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.