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 |
||
32 | abstract class WordPoints_Hook_Reactor implements WordPoints_Hook_SettingsI { |
||
33 | |||
34 | /** |
||
35 | * The unique slug identifying this hook reactor. |
||
36 | * |
||
37 | * @since 1.0.0 |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $slug; |
||
42 | |||
43 | /** |
||
44 | * The types of args that this reactor can target. |
||
45 | * |
||
46 | * @since 1.0.0 |
||
47 | * |
||
48 | * @var string|string[] |
||
49 | */ |
||
50 | protected $arg_types; |
||
51 | |||
52 | /** |
||
53 | * The settings fields used by this reactor. |
||
54 | * |
||
55 | * @since 1.0.0 |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $settings_fields; |
||
60 | |||
61 | /** |
||
62 | * The storage object for the standard reactions. |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | * |
||
66 | * @var WordPoints_Hook_Reaction_StorageI |
||
67 | */ |
||
68 | protected $standard_reactions; |
||
69 | |||
70 | /** |
||
71 | * The storage object for the network-wide reactions. |
||
72 | * |
||
73 | * @since 1.0.0 |
||
74 | * |
||
75 | * @var WordPoints_Hook_Reaction_StorageI |
||
76 | */ |
||
77 | protected $network_reactions; |
||
78 | |||
79 | /** |
||
80 | * The reaction storage class this reactor uses. |
||
81 | * |
||
82 | * @since 1.0.0 |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $standard_reactions_class = 'WordPoints_Hook_Reaction_Storage_Options'; |
||
87 | |||
88 | /** |
||
89 | * The network reaction storage class this reactor uses. |
||
90 | * |
||
91 | * @since 1.0.0 |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $network_reactions_class = 'WordPoints_Hook_Reaction_Storage_Options_Network'; |
||
96 | |||
97 | /** |
||
98 | * @since 1.0.0 |
||
99 | */ |
||
100 | public function __get( $var ) { |
||
137 | |||
138 | /** |
||
139 | * Get the slug of this reactor. |
||
140 | * |
||
141 | * @since 1.0.0 |
||
142 | * |
||
143 | * @return string The reactor's slug. |
||
144 | */ |
||
145 | public function get_slug() { |
||
148 | |||
149 | /** |
||
150 | * Get a list of the slugs of each type of arg that this reactor supports. |
||
151 | * |
||
152 | * @since 1.0.0 |
||
153 | * |
||
154 | * @return string[] The slugs of the arg types this reactor supports. |
||
155 | */ |
||
156 | public function get_arg_types() { |
||
159 | |||
160 | /** |
||
161 | * Get the settings fields used by the reactor. |
||
162 | * |
||
163 | * @since 1.0.0 |
||
164 | * |
||
165 | * @return string[] The meta keys used to store this reactor's settings. |
||
166 | */ |
||
167 | public function get_settings_fields() { |
||
170 | |||
171 | /** |
||
172 | * Check whether this reactor is network-wide. |
||
173 | * |
||
174 | * When a reactor is not network-wide, network reactions are not supported. For |
||
175 | * example, the points reactor is not network-wide when WordPoints isn't network- |
||
176 | * active, because the points types are created per-site. We default all reactors |
||
177 | * to being network wide only when WordPoints is network-active, but some may |
||
178 | * need to override this. |
||
179 | * |
||
180 | * @since 1.0.0 |
||
181 | * |
||
182 | * @return bool Whether this reactor is network-wide. |
||
183 | */ |
||
184 | public function is_network_wide() { |
||
187 | |||
188 | /** |
||
189 | * Get all reactions to a particular event for this reactor. |
||
190 | * |
||
191 | * On multisite it will return all reactions for the current site, both standard |
||
192 | * ones and any network-wide ones (if this reactor offers a network storage |
||
193 | * class). Or, if network mode is on, it will return only the network-wide ones. |
||
194 | * |
||
195 | * @since 1.0.0 |
||
196 | * |
||
197 | * @param string $event_slug The event slug. |
||
198 | * |
||
199 | * @return WordPoints_Hook_ReactionI[] All of the reaction objects. |
||
200 | */ |
||
201 | View Code Duplication | public function get_all_reactions_to_event( $event_slug ) { |
|
221 | |||
222 | /** |
||
223 | * Get all reactions for this reactor. |
||
224 | * |
||
225 | * On multisite it will return all reactions for the current site, both standard |
||
226 | * ones and any network-wide ones (if this reactor offers a network storage |
||
227 | * class). Or, if network mode is on, it will return only the network-wide ones. |
||
228 | * |
||
229 | * @since 1.0.0 |
||
230 | * |
||
231 | * @return WordPoints_Hook_ReactionI[] All of the reaction objects. |
||
232 | */ |
||
233 | View Code Duplication | public function get_all_reactions() { |
|
250 | |||
251 | /** |
||
252 | * @since 1.0.0 |
||
253 | */ |
||
254 | public function validate_settings( |
||
281 | |||
282 | /** |
||
283 | * @since 1.0.0 |
||
284 | */ |
||
285 | public function update_settings( WordPoints_Hook_ReactionI $reaction, array $settings ) { |
||
288 | |||
289 | /** |
||
290 | * Perform an action when the reactor is hit by an event being fired. |
||
291 | * |
||
292 | * @since 1.0.0 |
||
293 | * |
||
294 | * @param WordPoints_Hook_Event_Args $event_args The event args. |
||
295 | * @param WordPoints_Hook_ReactionI $reaction The reaction. |
||
296 | */ |
||
297 | abstract public function hit( |
||
301 | } |
||
302 | |||
304 |
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.