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 |
||
15 | class Constants extends Module { |
||
16 | /** |
||
17 | * Name of the constants checksum option. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
||
22 | |||
23 | /** |
||
24 | * Name of the transient for locking constants. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
||
29 | |||
30 | /** |
||
31 | * Sync module name. |
||
32 | * |
||
33 | * @access public |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | public function name() { |
||
40 | |||
41 | /** |
||
42 | * Initialize constants action listeners. |
||
43 | * |
||
44 | * @access public |
||
45 | * |
||
46 | * @param callable $callable Action handler callable. |
||
47 | */ |
||
48 | public function init_listeners( $callable ) { |
||
51 | |||
52 | /** |
||
53 | * Initialize constants action listeners for full sync. |
||
54 | * |
||
55 | * @access public |
||
56 | * |
||
57 | * @param callable $callable Action handler callable. |
||
58 | */ |
||
59 | public function init_full_sync_listeners( $callable ) { |
||
62 | |||
63 | /** |
||
64 | * Initialize the module in the sender. |
||
65 | * |
||
66 | * @access public |
||
67 | */ |
||
68 | public function init_before_send() { |
||
74 | |||
75 | /** |
||
76 | * Perform module cleanup. |
||
77 | * Deletes any transients and options that this module uses. |
||
78 | * Usually triggered when uninstalling the plugin. |
||
79 | * |
||
80 | * @access public |
||
81 | */ |
||
82 | public function reset_data() { |
||
86 | |||
87 | /** |
||
88 | * Set the constants whitelist. |
||
89 | * |
||
90 | * @access public |
||
91 | * @todo We don't seem to use this one. Should we remove it? |
||
92 | * |
||
93 | * @param array $constants The new constants whitelist. |
||
94 | */ |
||
95 | public function set_constants_whitelist( $constants ) { |
||
98 | |||
99 | /** |
||
100 | * Get the constants whitelist. |
||
101 | * |
||
102 | * @access public |
||
103 | * |
||
104 | * @return array The constants whitelist. |
||
105 | */ |
||
106 | public function get_constants_whitelist() { |
||
109 | |||
110 | /** |
||
111 | * Enqueue the constants actions for full sync. |
||
112 | * |
||
113 | * @access public |
||
114 | * |
||
115 | * @param array $config Full sync configuration for this sync module. |
||
116 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
117 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
118 | * |
||
119 | * @return array Number of actions enqueued, and next module state. |
||
120 | */ |
||
121 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
134 | |||
135 | /** |
||
136 | * Send the constants actions for full sync. |
||
137 | * |
||
138 | * @access public |
||
139 | * |
||
140 | * @param array $config Full sync configuration for this sync module. |
||
141 | * @param int $send_until The timestamp until the current request can send. |
||
142 | * @param array $state This module Full Sync status. |
||
143 | * |
||
144 | * @return array This module Full Sync status. |
||
145 | */ |
||
146 | public function send_full_sync_actions( $config, $send_until, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
153 | |||
154 | /** |
||
155 | * Retrieve an estimated number of actions that will be enqueued. |
||
156 | * |
||
157 | * @access public |
||
158 | * |
||
159 | * @param array $config Full sync configuration for this sync module. |
||
160 | * |
||
161 | * @return array Number of items yet to be enqueued. |
||
162 | */ |
||
163 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
166 | |||
167 | /** |
||
168 | * Retrieve the actions that will be sent for this module during a full sync. |
||
169 | * |
||
170 | * @access public |
||
171 | * |
||
172 | * @return array Full sync actions of this module. |
||
173 | */ |
||
174 | public function get_full_sync_actions() { |
||
177 | |||
178 | /** |
||
179 | * Sync the constants if we're supposed to. |
||
180 | * |
||
181 | * @access public |
||
182 | */ |
||
183 | public function maybe_sync_constants() { |
||
217 | |||
218 | /** |
||
219 | * Retrieve all constants as per the current constants whitelist. |
||
220 | * Public so that we don't have to store an option for each constant. |
||
221 | * |
||
222 | * @access public |
||
223 | * |
||
224 | * @return array All constants. |
||
225 | */ |
||
226 | public function get_all_constants() { |
||
234 | |||
235 | /** |
||
236 | * Retrieve the value of a constant. |
||
237 | * Used as a wrapper to standartize access to constants. |
||
238 | * |
||
239 | * @access private |
||
240 | * |
||
241 | * @param string $constant Constant name. |
||
242 | * |
||
243 | * @return mixed Return value of the constant. |
||
244 | */ |
||
245 | private function get_constant( $constant ) { |
||
250 | |||
251 | /** |
||
252 | * Expand the constants within a hook before they are serialized and sent to the server. |
||
253 | * |
||
254 | * @access public |
||
255 | * |
||
256 | * @param array $args The hook parameters. |
||
257 | * |
||
258 | * @return array $args The hook parameters. |
||
259 | */ |
||
260 | public function expand_constants( $args ) { |
||
274 | |||
275 | /** |
||
276 | * Return Total number of objects. |
||
277 | * |
||
278 | * @param array $config Full Sync config. |
||
279 | * |
||
280 | * @return int total |
||
281 | */ |
||
282 | public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
285 | |||
286 | /** |
||
287 | * Retrieve a set of constants by their IDs. |
||
288 | * |
||
289 | * @access public |
||
290 | * |
||
291 | * @param string $object_type Object type. |
||
292 | * @param array $ids Object IDs. |
||
293 | * @return array Array of objects. |
||
294 | */ |
||
295 | View Code Duplication | public function get_objects_by_id( $object_type, $ids ) { |
|
296 | if ( empty( $ids ) || empty( $object_type ) || 'constant' !== $object_type ) { |
||
297 | return array(); |
||
298 | } |
||
299 | |||
300 | $objects = array(); |
||
301 | foreach ( (array) $ids as $id ) { |
||
302 | $object = $this->get_object_by_id( $object_type, $id ); |
||
303 | |||
304 | if ( 'all' === $id ) { |
||
305 | // If all was requested it contains all options and can simply be returned. |
||
306 | return $object; |
||
307 | } |
||
308 | $objects[ $id ] = $object; |
||
309 | } |
||
310 | |||
311 | return $objects; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Retrieve a constant by its name. |
||
316 | * |
||
317 | * @access public |
||
318 | * |
||
319 | * @param string $object_type Type of the sync object. |
||
320 | * @param string $id ID of the sync object. |
||
321 | * @return mixed Value of Constant. |
||
322 | */ |
||
323 | public function get_object_by_id( $object_type, $id ) { |
||
336 | |||
337 | } |
||
338 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: