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 ModuleCollection 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 ModuleCollection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ModuleCollection implements \ArrayAccess, \IteratorAggregate |
||
31 | { |
||
32 | use AccessorTrait; |
||
33 | |||
34 | /** |
||
35 | * May be used with the {@link filter_descriptors_by_users()} method to filter the descriptors |
||
36 | * of enabled modules. |
||
37 | */ |
||
38 | const ONLY_ENABLED_MODULES = false; |
||
39 | |||
40 | /** |
||
41 | * May be used with the {@link filter_descriptors_by_users()} method to filter the descriptors |
||
42 | * of all modules, enabled or not. |
||
43 | */ |
||
44 | const ALL_MODULES = true; |
||
45 | |||
46 | /** |
||
47 | * Formats a SQL table name given the module id and the model id. |
||
48 | * |
||
49 | * @param string $module_id |
||
50 | * @param string $model_id |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | static public function format_model_name($module_id, $model_id = 'primary') |
||
58 | |||
59 | /** |
||
60 | * The descriptors for the modules. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | public $descriptors = []; |
||
65 | |||
66 | /** |
||
67 | * The paths where modules can be found. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $paths = []; |
||
72 | |||
73 | /** |
||
74 | * A cache for the modules index. |
||
75 | * |
||
76 | * @var Storage |
||
77 | */ |
||
78 | protected $cache; |
||
79 | |||
80 | /** |
||
81 | * Instantiated modules. |
||
82 | * |
||
83 | * @var Module[] |
||
84 | */ |
||
85 | protected $modules = []; |
||
86 | |||
87 | /** |
||
88 | * The index for the available modules is created with the accessor object. |
||
89 | * |
||
90 | * @param array $paths The paths to look for modules. |
||
91 | * @param Storage $cache The cache to use for the module indexes. |
||
92 | */ |
||
93 | public function __construct($paths, Storage $cache = null) |
||
98 | |||
99 | /** |
||
100 | * Revokes constructions. |
||
101 | * |
||
102 | * The following properties are revoked: |
||
103 | * |
||
104 | * - {@link $enabled_modules_descriptors} |
||
105 | * - {@link $disabled_modules_descriptors} |
||
106 | * - {@link $catalog_paths} |
||
107 | * - {@link $config_paths} |
||
108 | * |
||
109 | * The method is usually invoked when modules state changes, in order to reflect these |
||
110 | * changes. |
||
111 | */ |
||
112 | protected function revoke_constructions() |
||
119 | |||
120 | /** |
||
121 | * Enables a module. |
||
122 | * |
||
123 | * @param string $module_id Module identifier. |
||
124 | */ |
||
125 | public function enable($module_id) |
||
129 | |||
130 | /** |
||
131 | * Disables a module. |
||
132 | * |
||
133 | * @param string $module_id Module identifier. |
||
134 | */ |
||
135 | public function disable($module_id) |
||
139 | |||
140 | /** |
||
141 | * Used to enable or disable a module using the specified offset as a module identifier. |
||
142 | * |
||
143 | * @param string $module_id Identifier of the module. |
||
144 | * @param bool $enable Status of the module: `true` for enabled, `false` for disabled. |
||
145 | */ |
||
146 | public function offsetSet($module_id, $enable) |
||
150 | |||
151 | /** |
||
152 | * Disables a module by setting the {@link Descriptor::DISABLED} key of its descriptor to `true`. |
||
153 | * |
||
154 | * @param string $module_id Module identifier. |
||
155 | */ |
||
156 | public function offsetUnset($module_id) |
||
160 | |||
161 | /** |
||
162 | * Checks the availability of a module. |
||
163 | * |
||
164 | * A module is considered available when its descriptor is defined, and the |
||
165 | * {@link Descriptor::DISABLED} key of its descriptor is empty. |
||
166 | * |
||
167 | * Note: `empty()` will call {@link offsetGet()} to check if the value is not empty. So, unless |
||
168 | * you want to use the module you check, better check using `!isset()`, otherwise the module |
||
169 | * you check is loaded too. |
||
170 | * |
||
171 | * @param string $module_id Module identifier. |
||
172 | * |
||
173 | * @return boolean Whether or not the module is available. |
||
174 | */ |
||
175 | public function offsetExists($module_id) |
||
184 | |||
185 | /** |
||
186 | * Returns a module object. |
||
187 | * |
||
188 | * If the {@link autorun} property is `true`, the {@link Module::run()} method of the module |
||
189 | * is invoked upon its first loading. |
||
190 | * |
||
191 | * @param string $module_id Module identifier. |
||
192 | * |
||
193 | * @throws ModuleNotDefined when the requested module is not defined. |
||
194 | * |
||
195 | * @throws ModuleIsDisabled when the module is disabled. |
||
196 | * |
||
197 | * @throws ModuleConstructorMissing when the class that should be used to create its instance |
||
198 | * is not defined. |
||
199 | * |
||
200 | * @return Module |
||
201 | */ |
||
202 | public function offsetGet($module_id) |
||
213 | |||
214 | /** |
||
215 | * Returns an iterator for instantiated modules. |
||
216 | * |
||
217 | * @return \ArrayIterator |
||
218 | */ |
||
219 | public function getIterator() |
||
225 | |||
226 | /** |
||
227 | * Filter descriptors. |
||
228 | * |
||
229 | * @param callable $filter |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function filter_descriptors(callable $filter) |
||
237 | |||
238 | /** |
||
239 | * Returns the modules using a module. |
||
240 | * |
||
241 | * @param string $module_id Used module identifier. |
||
242 | * @param bool $all One of {@link ONLY_ENABLED_MODULES} or {@link ALL_MODULES}. |
||
243 | * Default: {@link ONLY_ENABLED_MODULES}. |
||
244 | * |
||
245 | * @return array A array of filtered descriptors. |
||
246 | */ |
||
247 | public function filter_descriptors_by_users($module_id, $all = self::ONLY_ENABLED_MODULES) |
||
263 | |||
264 | /** |
||
265 | * Indexes the modules found in the paths specified during construct. |
||
266 | * |
||
267 | * The index is made of an array of descriptors, an array of catalogs paths, an array of |
||
268 | * configs path, and finally an array of configs constructors. |
||
269 | * |
||
270 | * The method also creates a `DIR` constant for each module. The constant is defined in the |
||
271 | * namespace of the module e.g. `Icybee\ModuleCollection\Nodes\DIR`. |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | protected function lazy_get_index() |
||
283 | |||
284 | /** |
||
285 | * Obtain index either from cache or by building it. |
||
286 | * |
||
287 | * @return array|mixed|null |
||
288 | */ |
||
289 | private function obtain_index() |
||
311 | |||
312 | /** |
||
313 | * Construct the index for the modules. |
||
314 | * |
||
315 | * The index contains the following values: |
||
316 | * |
||
317 | * - (array) descriptors: The descriptors of the modules, ordered by weight. |
||
318 | * - (array) catalogs: Absolute paths to locale catalog directories. |
||
319 | * - (array) configs: Absolute paths to config directories. |
||
320 | * - (array) classes aliases: An array of _key/value_ pairs where _key_ is the alias of a class |
||
321 | * and _value_ if the real class. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function index_modules() |
||
354 | |||
355 | /** |
||
356 | * Indexes descriptors. |
||
357 | * |
||
358 | * The descriptors are extended with the following default values: |
||
359 | * |
||
360 | * - (string) category: null. |
||
361 | * - (string) class: ModuleCollection\<normalized_module_part> |
||
362 | * - (string) description: null. |
||
363 | * - (bool) disabled: false if required, true otherwise. |
||
364 | * - (string) extends: null. |
||
365 | * - (string) id: The module's identifier. |
||
366 | * - (array) models: Empty array. |
||
367 | * - (string) path: The absolute path to the module directory. |
||
368 | * - (string) permission: null. |
||
369 | * - (array) permissions: Empty array. |
||
370 | * - (bool) startup: false. |
||
371 | * - (bool) required: false. |
||
372 | * - (array) requires: Empty array. |
||
373 | * - (string) weight: 0. |
||
374 | * |
||
375 | * The descriptors are ordered according to their inheritance and weight. |
||
376 | * |
||
377 | * @param array $paths |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | protected function index_descriptors(array $paths) |
||
442 | |||
443 | /** |
||
444 | * Collects descriptors from paths. |
||
445 | * |
||
446 | * @param array $paths |
||
447 | * |
||
448 | * @return array |
||
449 | */ |
||
450 | protected function collect_descriptors(array $paths) |
||
499 | |||
500 | /** |
||
501 | * Reads the descriptor file. |
||
502 | * |
||
503 | * The descriptor file is extended with private values and default values. |
||
504 | * |
||
505 | * @param string $module_id The identifier of the module. |
||
506 | * @param string $path The path to the directory where the descriptor is located. |
||
507 | * |
||
508 | * @throws \InvalidArgumentException in the following situations: |
||
509 | * - The descriptor is not an array |
||
510 | * - The {@link Descriptor::TITLE} key is empty. |
||
511 | * - The {@link Descriptor::NS} key is empty. |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | protected function read_descriptor($module_id, $path) |
||
573 | |||
574 | /** |
||
575 | * Alters the module descriptor. |
||
576 | * |
||
577 | * @param array $descriptor Descriptor of the module to index. |
||
578 | * |
||
579 | * @return array The altered descriptor. |
||
580 | */ |
||
581 | protected function alter_descriptor(array $descriptor) |
||
625 | |||
626 | /** |
||
627 | * Traverses the descriptors and create two array of descriptors: one for the disabled modules |
||
628 | * and the other for enabled modules. The {@link $disabled_modules_descriptors} magic property |
||
629 | * receives the descriptors of the disabled modules, while the {@link $enabled_modules_descriptors} |
||
630 | * magic property receives the descriptors of the enabled modules. |
||
631 | */ |
||
632 | private function sort_modules_descriptors() |
||
654 | |||
655 | /** |
||
656 | * Returns the descriptors of the disabled modules. |
||
657 | * |
||
658 | * This method is the getter for the {@link $disabled_modules_descriptors} magic property. |
||
659 | * |
||
660 | * @return array |
||
661 | */ |
||
662 | protected function lazy_get_disabled_modules_descriptors() |
||
668 | |||
669 | /** |
||
670 | * Returns the descriptors of the enabled modules. |
||
671 | * |
||
672 | * This method is the getter for the {@link $enabled_modules_descriptors} magic property. |
||
673 | * |
||
674 | * @return array |
||
675 | */ |
||
676 | protected function lazy_get_enabled_modules_descriptors() |
||
682 | |||
683 | /** |
||
684 | * Returns the paths of the enabled modules which have a `locale` folder. |
||
685 | * |
||
686 | * @return array |
||
687 | */ |
||
688 | View Code Duplication | protected function lazy_get_locale_paths() |
|
704 | |||
705 | /** |
||
706 | * Returns the paths of the enabled modules which have a `config` folder. |
||
707 | * |
||
708 | * @return array |
||
709 | */ |
||
710 | View Code Duplication | protected function lazy_get_config_paths() |
|
726 | |||
727 | /** |
||
728 | * Orders the module ids provided according to module inheritance and weight. |
||
729 | * |
||
730 | * @param array $ids The module ids to order. |
||
731 | * @param array $descriptors Module descriptors. |
||
732 | * |
||
733 | * @return array |
||
734 | */ |
||
735 | public function order_ids(array $ids, array $descriptors = null) |
||
793 | |||
794 | /** |
||
795 | * Returns the usage of a module by other modules. |
||
796 | * |
||
797 | * @param string $module_id The identifier of the module. |
||
798 | * @param bool $all One of {@link ONLY_ENABLED_MODULES} or {@link ALL_MODULES}. |
||
799 | * Default: {@link ONLY_ENABLED_MODULES}. |
||
800 | * |
||
801 | * @return int |
||
802 | */ |
||
803 | public function usage($module_id, $all = self::ONLY_ENABLED_MODULES) |
||
807 | |||
808 | /** |
||
809 | * Checks if a module inherits from another. |
||
810 | * |
||
811 | * @param string $module_id Module identifier. |
||
812 | * @param string $parent_id Identifier of the parent module. |
||
813 | * |
||
814 | * @return boolean `true` if the module inherits from the other. |
||
815 | */ |
||
816 | public function is_inheriting($module_id, $parent_id) |
||
832 | |||
833 | /** |
||
834 | * Install all the enabled modules. |
||
835 | * |
||
836 | * @param ErrorCollection|null $errors |
||
837 | * |
||
838 | * @return ErrorCollection |
||
839 | * |
||
840 | * @throws ModuleCollectionInstallFailed if an error occurs. |
||
841 | */ |
||
842 | public function install(ErrorCollection $errors = null) |
||
868 | |||
869 | /** |
||
870 | * Resolves a class name using module inheritance. |
||
871 | * |
||
872 | * To resolve a given class name, the method checks in each module namespace—starting from the |
||
873 | * specified module—if the class exists. If it does, it returns its fully qualified name. |
||
874 | * |
||
875 | * @param string $unqualified_classname |
||
876 | * @param string|Module $module_id |
||
877 | * @param array $tried |
||
878 | * |
||
879 | * @return string|false The resolved file name, or `false` if it could not be resolved. |
||
880 | * |
||
881 | * @throws ModuleNotDefined if the specified module, or the module specified by |
||
882 | * {@link Descriptor::INHERITS} is not defined. |
||
883 | */ |
||
884 | public function resolve_classname($unqualified_classname, $module_id, array &$tried = []) |
||
909 | |||
910 | /** |
||
911 | * Changes a module availability. |
||
912 | * |
||
913 | * @param string $module_id |
||
914 | * @param bool $available |
||
915 | */ |
||
916 | protected function change_module_availability($module_id, $available) |
||
928 | |||
929 | /** |
||
930 | * Ensures that modules are indexed, index them if not. |
||
931 | */ |
||
932 | protected function ensure_modules_are_indexed() |
||
936 | |||
937 | /** |
||
938 | * Asserts that a module is defined. |
||
939 | * |
||
940 | * @param string $module_id Module identifier. |
||
941 | * |
||
942 | * @throws ModuleNotDefined if the module is not defined. |
||
943 | */ |
||
944 | protected function assert_module_is_defined($module_id) |
||
951 | |||
952 | /** |
||
953 | * Asserts that a module is enabled. |
||
954 | * |
||
955 | * @param string $module_id |
||
956 | * |
||
957 | * @throws ModuleIsDisabled if the module is disabled. |
||
958 | */ |
||
959 | protected function assert_module_is_enabled($module_id) |
||
966 | |||
967 | /** |
||
968 | * Asserts that a module constructor exists. |
||
969 | * |
||
970 | * @param string $module_id Module identifier. |
||
971 | * @param string $class Constructor class. |
||
972 | */ |
||
973 | protected function assert_constructor_exists($module_id, $class) |
||
980 | |||
981 | /** |
||
982 | * Instantiate a module. |
||
983 | * |
||
984 | * @param string $module_id Module identifier. |
||
985 | * |
||
986 | * @return Module |
||
987 | */ |
||
988 | protected function instantiate_module($module_id) |
||
1007 | |||
1008 | /** |
||
1009 | * Defines module constants. |
||
1010 | * |
||
1011 | * @param array $descriptors |
||
1012 | */ |
||
1013 | protected function define_constants(array $descriptors) |
||
1026 | } |
||
1027 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: