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 Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Module |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $name; |
||
25 | /** |
||
26 | * @var \CModule |
||
27 | */ |
||
28 | private $object; |
||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $beta = false; |
||
33 | |||
34 | /** |
||
35 | * @param string $moduleName |
||
36 | */ |
||
37 | public function __construct($moduleName) |
||
38 | { |
||
39 | $this->name = $this->normalizeName($moduleName); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param string $moduleName |
||
44 | * @return string |
||
45 | */ |
||
46 | protected function normalizeName($moduleName) |
||
47 | { |
||
48 | return preg_replace("/[^a-zA-Z0-9_.]+/i", "", trim($moduleName)); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return \CModule |
||
53 | */ |
||
54 | protected function &getObject() |
||
55 | { |
||
56 | if (!isset($this->object)) { |
||
57 | $this->object = \CModule::CreateModuleObject($this->name); |
||
58 | } |
||
59 | |||
60 | if (!is_object($this->object) || !($this->object instanceof \CModule)) { |
||
|
|||
61 | unset($this->object); |
||
62 | throw new Exception\ModuleNotFoundException('Module not found or incorrect', $this->name); |
||
63 | } |
||
64 | |||
65 | return $this->object; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Checks for module and module object existence. |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function exist() |
||
74 | { |
||
75 | try { |
||
76 | $this->getObject(); |
||
77 | } catch (Exception\ModuleNotFoundException $e) { |
||
78 | return false; |
||
79 | } |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Check if module exists and installed |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function isRegistered() |
||
93 | |||
94 | /** |
||
95 | * @return bool true for marketplace modules, false for kernel modules |
||
96 | */ |
||
97 | public function isThirdParty() |
||
101 | |||
102 | /** |
||
103 | * Install module. |
||
104 | * |
||
105 | * @throws Exception\ModuleException |
||
106 | * @throws BitrixException |
||
107 | */ |
||
108 | View Code Duplication | public function register() |
|
151 | |||
152 | /** |
||
153 | * Download module from Marketplace. |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function load() |
||
176 | |||
177 | /** |
||
178 | * Uninstall module. |
||
179 | * |
||
180 | * @throws Exception\ModuleException |
||
181 | * @throws BitrixException |
||
182 | */ |
||
183 | View Code Duplication | public function unRegister() |
|
224 | |||
225 | /** |
||
226 | * Uninstall and remove module directory. |
||
227 | */ |
||
228 | public function remove() |
||
244 | |||
245 | /** |
||
246 | * Update module. |
||
247 | * |
||
248 | * It must be called repeatedly until the method returns false. |
||
249 | * After each call php must be restarted (new process created) to update module class and function definitions. |
||
250 | * |
||
251 | * @param array $modulesUpdated [optional] |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function update(&$modulesUpdated = null) |
||
329 | |||
330 | /** |
||
331 | * Check update files. |
||
332 | * |
||
333 | * @param string $updateDir |
||
334 | */ |
||
335 | protected function validateUpdate($updateDir) |
||
351 | |||
352 | /** |
||
353 | * Returns module name. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function getName() |
||
361 | |||
362 | /** |
||
363 | * Beta releases allowed? |
||
364 | * |
||
365 | * @return boolean |
||
366 | */ |
||
367 | public function isBeta() |
||
371 | |||
372 | /** |
||
373 | * Set beta releases installation. |
||
374 | * |
||
375 | * @param boolean $beta |
||
376 | */ |
||
377 | public function setBeta($beta = true) |
||
381 | |||
382 | public function getVersion() |
||
386 | } |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.