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:
Complex classes like GateKeeper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GateKeeper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class GateKeeper |
||
10 | { |
||
11 | /** |
||
12 | * [plugin_file_path] => [plugin_name]|[plugin_version]|[plugin_url] |
||
13 | */ |
||
14 | const DEPENDENCIES = [ |
||
15 | 'meta-box/meta-box.php' => 'Meta Box|4.11|https://wordpress.org/plugins/meta-box/', |
||
16 | ]; |
||
17 | const MIN_PHP_VERSION = '5.6.0'; |
||
18 | const MIN_WORDPRESS_VERSION = '4.7'; |
||
19 | |||
20 | public $errors = []; |
||
21 | |||
22 | /** |
||
23 | * @var Application |
||
24 | */ |
||
25 | protected $app; |
||
26 | |||
27 | /** |
||
28 | * @var Notice |
||
29 | */ |
||
30 | protected $notice; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $plugin; |
||
36 | |||
37 | public function __construct( $plugin ) |
||
49 | |||
50 | public function init() |
||
60 | |||
61 | /** |
||
62 | * @return void |
||
63 | */ |
||
64 | public function activatePlugin() |
||
78 | |||
79 | /** |
||
80 | * @return void |
||
81 | */ |
||
82 | public function ajaxActivatePluginLink() |
||
100 | |||
101 | /** |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function canActivate() |
||
108 | |||
109 | /** |
||
110 | * @return void |
||
111 | * @action activated_plugin |
||
112 | * @action admin_notices |
||
113 | */ |
||
114 | public function deactivate( $plugin ) |
||
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function hasDependency( $plugin ) |
||
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function hasPendingDependencies() |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function hasValidPHPVersion() |
||
157 | |||
158 | /** |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function hasValidWPVersion() |
||
166 | |||
167 | /** |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function isPluginActive( $plugin ) |
||
176 | |||
177 | /** |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function isPluginDependency( $plugin ) |
||
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function isPluginInstalled( $plugin ) |
||
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function isPluginValid( $plugin ) |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function isPluginVersionValid( $plugin ) |
||
220 | |||
221 | /** |
||
222 | * @return void |
||
223 | */ |
||
224 | public function printNotices() |
||
230 | |||
231 | /** |
||
232 | * @return void|null |
||
233 | */ |
||
234 | public function setDependencyNotice() |
||
246 | |||
247 | /** |
||
248 | * @return void |
||
249 | */ |
||
250 | View Code Duplication | protected function addInvalidPHPVersionNotice() |
|
258 | |||
259 | /** |
||
260 | * @return void |
||
261 | */ |
||
262 | View Code Duplication | protected function addInvalidWPVersionNotice() |
|
270 | |||
271 | /** |
||
272 | * @param string $plugin |
||
273 | * @param string $error |
||
274 | * @param bool $isValid |
||
275 | * @return bool |
||
276 | */ |
||
277 | protected function catchError( $plugin, $error, $isValid ) |
||
289 | |||
290 | /** |
||
291 | * @return array |
||
292 | */ |
||
293 | protected function getAllPlugins() |
||
298 | |||
299 | /** |
||
300 | * @return string |
||
301 | */ |
||
302 | protected function getDependencyActions() |
||
318 | |||
319 | /** |
||
320 | * @return string |
||
321 | */ |
||
322 | protected function getDependencyLinks() |
||
328 | |||
329 | /** |
||
330 | * @return array |
||
331 | */ |
||
332 | protected function getMustUsePlugins() |
||
343 | |||
344 | /** |
||
345 | * @return array|false |
||
346 | */ |
||
347 | protected function getPlugin( $plugin ) |
||
354 | |||
355 | /** |
||
356 | * @return array|string |
||
357 | */ |
||
358 | protected function getPluginData( $plugin, $data, $key = null ) |
||
374 | |||
375 | /** |
||
376 | * @return array|string |
||
377 | */ |
||
378 | protected function getPluginInformation( $plugin, $key = null ) |
||
382 | |||
383 | /** |
||
384 | * @return string |
||
385 | */ |
||
386 | protected function getPluginLink( $plugin ) |
||
400 | |||
401 | /** |
||
402 | * @return array|string |
||
403 | */ |
||
404 | protected function getPluginRequirements( $plugin, $key = null ) |
||
412 | |||
413 | /** |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function getPluginSlug( $plugin ) |
||
420 | |||
421 | /** |
||
422 | * @return void |
||
423 | */ |
||
424 | protected function redirect() |
||
433 | } |
||
434 |
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.