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 ajaxIsPluginActive() |
||
81 | |||
82 | /** |
||
83 | * @return void |
||
84 | */ |
||
85 | public function ajaxActivatePlugin() |
||
98 | |||
99 | /** |
||
100 | * @return bool |
||
101 | */ |
||
102 | public function canActivate() |
||
106 | |||
107 | /** |
||
108 | * @return void |
||
109 | * @action activated_plugin |
||
110 | * @action admin_notices |
||
111 | */ |
||
112 | public function deactivate( $plugin ) |
||
123 | |||
124 | /** |
||
125 | * @return void|null |
||
126 | */ |
||
127 | public function getDependencyAction() |
||
145 | |||
146 | /** |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function hasDependency( $plugin ) |
||
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function hasValidPHPVersion() |
||
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function hasValidWPVersion() |
||
173 | |||
174 | /** |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function isPluginActive( $plugin ) |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function isPluginDependency( $plugin ) |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isPluginInstalled( $plugin ) |
||
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function isPluginValid( $plugin ) |
||
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function isPluginVersionValid( $plugin ) |
||
227 | |||
228 | /** |
||
229 | * @return void |
||
230 | */ |
||
231 | public function printNotices() |
||
237 | |||
238 | /** |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function hasPendingDependencies() |
||
250 | |||
251 | /** |
||
252 | * @return void|null |
||
253 | */ |
||
254 | public function setDependencyNotice() |
||
281 | |||
282 | /** |
||
283 | * @return void |
||
284 | */ |
||
285 | View Code Duplication | protected function addInvalidPHPVersionNotice() |
|
293 | |||
294 | /** |
||
295 | * @return void |
||
296 | */ |
||
297 | View Code Duplication | protected function addInvalidWPVersionNotice() |
|
305 | |||
306 | /** |
||
307 | * @param string $error |
||
308 | * @param bool $bool |
||
309 | * @return bool |
||
310 | */ |
||
311 | protected function catchError( $plugin, $error, $isValid ) |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | protected function getAllPlugins() |
||
332 | |||
333 | /** |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function getMustUsePlugins() |
||
347 | |||
348 | /** |
||
349 | * @return array|false |
||
350 | */ |
||
351 | protected function getPlugin( $plugin ) |
||
358 | |||
359 | /** |
||
360 | * @return array|string |
||
361 | */ |
||
362 | protected function getPluginData( $plugin, array $data, $key = null ) |
||
375 | |||
376 | /** |
||
377 | * @return array|string |
||
378 | */ |
||
379 | protected function getPluginInformation( $plugin, $key = null ) |
||
387 | |||
388 | /** |
||
389 | * @return string |
||
390 | */ |
||
391 | protected function getPluginLink( $plugin ) |
||
405 | |||
406 | /** |
||
407 | * @return array|string |
||
408 | */ |
||
409 | protected function getPluginRequirements( $plugin, $key = null ) |
||
418 | |||
419 | /** |
||
420 | * @return string |
||
421 | */ |
||
422 | protected function getPluginSlug( $plugin ) |
||
426 | |||
427 | /** |
||
428 | * @return void |
||
429 | */ |
||
430 | protected function redirect() |
||
439 | } |
||
440 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.