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() |
||
80 | |||
81 | /** |
||
82 | * @return void |
||
83 | */ |
||
84 | public function ajaxActivatePluginLink() |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function canActivate() |
||
105 | |||
106 | /** |
||
107 | * @return void |
||
108 | * @action activated_plugin |
||
109 | * @action admin_notices |
||
110 | */ |
||
111 | public function deactivate( $plugin ) |
||
122 | |||
123 | /** |
||
124 | * @return void|null |
||
125 | */ |
||
126 | public function getDependencyAction() |
||
144 | |||
145 | /** |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function hasDependency( $plugin ) |
||
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function hasValidPHPVersion() |
||
163 | |||
164 | /** |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function hasValidWPVersion() |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function isPluginActive( $plugin ) |
||
182 | |||
183 | /** |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function isPluginDependency( $plugin ) |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isPluginInstalled( $plugin ) |
||
200 | |||
201 | /** |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function isPluginValid( $plugin ) |
||
208 | |||
209 | /** |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function isPluginVersionValid( $plugin ) |
||
226 | |||
227 | /** |
||
228 | * @return void |
||
229 | */ |
||
230 | public function printNotices() |
||
236 | |||
237 | /** |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function hasPendingDependencies() |
||
249 | |||
250 | /** |
||
251 | * @return void|null |
||
252 | */ |
||
253 | public function setDependencyNotice() |
||
280 | |||
281 | /** |
||
282 | * @return void |
||
283 | */ |
||
284 | View Code Duplication | protected function addInvalidPHPVersionNotice() |
|
292 | |||
293 | /** |
||
294 | * @return void |
||
295 | */ |
||
296 | View Code Duplication | protected function addInvalidWPVersionNotice() |
|
304 | |||
305 | /** |
||
306 | * @param string $error |
||
307 | * @param bool $bool |
||
308 | * @return bool |
||
309 | */ |
||
310 | protected function catchError( $plugin, $error, $isValid ) |
||
322 | |||
323 | /** |
||
324 | * @return array |
||
325 | */ |
||
326 | protected function getAllPlugins() |
||
331 | |||
332 | /** |
||
333 | * @return array |
||
334 | */ |
||
335 | protected function getMustUsePlugins() |
||
346 | |||
347 | /** |
||
348 | * @return array|false |
||
349 | */ |
||
350 | protected function getPlugin( $plugin ) |
||
357 | |||
358 | /** |
||
359 | * @return array|string |
||
360 | */ |
||
361 | protected function getPluginData( $plugin, array $data, $key = null ) |
||
374 | |||
375 | /** |
||
376 | * @return array|string |
||
377 | */ |
||
378 | protected function getPluginInformation( $plugin, $key = null ) |
||
386 | |||
387 | /** |
||
388 | * @return string |
||
389 | */ |
||
390 | protected function getPluginLink( $plugin ) |
||
404 | |||
405 | /** |
||
406 | * @return array|string |
||
407 | */ |
||
408 | protected function getPluginRequirements( $plugin, $key = null ) |
||
417 | |||
418 | /** |
||
419 | * @return string |
||
420 | */ |
||
421 | protected function getPluginSlug( $plugin ) |
||
425 | |||
426 | /** |
||
427 | * @return void |
||
428 | */ |
||
429 | protected function redirect() |
||
438 | } |
||
439 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.