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 hasValidPHPVersion() |
||
144 | |||
145 | /** |
||
146 | * @return bool |
||
147 | */ |
||
148 | public function hasValidWPVersion() |
||
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isPluginActive( $plugin ) |
||
163 | |||
164 | /** |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function isPluginDependency( $plugin ) |
||
171 | |||
172 | /** |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function isPluginInstalled( $plugin ) |
||
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function isPluginValid( $plugin ) |
||
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isPluginVersionValid( $plugin ) |
||
207 | |||
208 | /** |
||
209 | * @return void |
||
210 | */ |
||
211 | public function printNotices() |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function hasPendingDependencies() |
||
230 | |||
231 | /** |
||
232 | * @return void|null |
||
233 | */ |
||
234 | public function setDependencyNotice() |
||
261 | |||
262 | /** |
||
263 | * @return void |
||
264 | */ |
||
265 | View Code Duplication | protected function addInvalidPHPVersionNotice() |
|
273 | |||
274 | /** |
||
275 | * @return void |
||
276 | */ |
||
277 | View Code Duplication | protected function addInvalidWPVersionNotice() |
|
285 | |||
286 | /** |
||
287 | * @param string $plugin |
||
288 | * @param string $error |
||
289 | * @param bool $isValid |
||
290 | * @return bool |
||
291 | */ |
||
292 | protected function catchError( $plugin, $error, $isValid ) |
||
304 | |||
305 | /** |
||
306 | * @return array |
||
307 | */ |
||
308 | protected function getAllPlugins() |
||
313 | |||
314 | /** |
||
315 | * @return array |
||
316 | */ |
||
317 | protected function getMustUsePlugins() |
||
328 | |||
329 | /** |
||
330 | * @return array|false |
||
331 | */ |
||
332 | protected function getPlugin( $plugin ) |
||
339 | |||
340 | /** |
||
341 | * @return array|string |
||
342 | */ |
||
343 | protected function getPluginData( $plugin, $data, $key = null ) |
||
359 | |||
360 | /** |
||
361 | * @return array|string |
||
362 | */ |
||
363 | protected function getPluginInformation( $plugin, $key = null ) |
||
367 | |||
368 | /** |
||
369 | * @return string |
||
370 | */ |
||
371 | protected function getPluginLink( $plugin ) |
||
385 | |||
386 | /** |
||
387 | * @return array|string |
||
388 | */ |
||
389 | protected function getPluginRequirements( $plugin, $key = null ) |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | protected function getPluginSlug( $plugin ) |
||
405 | |||
406 | /** |
||
407 | * @return void |
||
408 | */ |
||
409 | protected function redirect() |
||
418 | } |
||
419 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.