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 |
||
34 | class Module extends Prototyped |
||
35 | { |
||
36 | /* |
||
37 | * PERMISSIONS: |
||
38 | * |
||
39 | * NONE: Well, you can't do anything |
||
40 | * |
||
41 | * ACCESS: You can access the module and view its records |
||
42 | * |
||
43 | * CREATE: You can create new records |
||
44 | * |
||
45 | * MAINTAIN: You can edit the records you created |
||
46 | * |
||
47 | * MANAGE: You can delete the records you created |
||
48 | * |
||
49 | * ADMINISTER: You have complete control over the module |
||
50 | * |
||
51 | */ |
||
52 | const PERMISSION_NONE = 0; |
||
53 | const PERMISSION_ACCESS = 1; |
||
54 | const PERMISSION_CREATE = 2; |
||
55 | const PERMISSION_MAINTAIN = 3; |
||
56 | const PERMISSION_MANAGE = 4; |
||
57 | const PERMISSION_ADMINISTER = 5; |
||
58 | |||
59 | /** |
||
60 | * Defines the name of the operation used to save the records of the module. |
||
61 | */ |
||
62 | const OPERATION_SAVE = 'save'; |
||
63 | |||
64 | /** |
||
65 | * Defines the name of the operation used to delete the records of the module. |
||
66 | */ |
||
67 | const OPERATION_DELETE = 'delete'; |
||
68 | |||
69 | /** |
||
70 | * Returns the identifier of the module as defined by its descriptor. |
||
71 | * |
||
72 | * This method is the getter for the {@link $id} magic property. |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | protected function get_id() |
||
80 | |||
81 | /** |
||
82 | * Returns the path of the module as defined by its descriptor. |
||
83 | * |
||
84 | * This method is the getter for the {@link $path} magic property. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | protected function get_path() |
||
92 | |||
93 | /** |
||
94 | * The descriptor of the module. |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $descriptor; |
||
99 | |||
100 | /** |
||
101 | * Returns the descriptor of the module. |
||
102 | * |
||
103 | * This method is the getter for the {@link $descriptor} magic property. |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | protected function get_descriptor() |
||
111 | |||
112 | /** |
||
113 | * Cache for loaded models. |
||
114 | * |
||
115 | * @var ActiveRecord\Model[] |
||
116 | */ |
||
117 | private $models = []; |
||
118 | |||
119 | /** |
||
120 | * @var ModuleCollection |
||
121 | */ |
||
122 | private $collection; |
||
123 | |||
124 | /** |
||
125 | * @return ModuleCollection |
||
126 | */ |
||
127 | protected function get_collection() |
||
131 | |||
132 | /** |
||
133 | * Constructor. |
||
134 | * |
||
135 | * Initializes the {@link $descriptor} property. |
||
136 | * |
||
137 | * @param ModuleCollection $collection |
||
138 | * @param array $descriptor |
||
139 | */ |
||
140 | public function __construct(ModuleCollection $collection, array $descriptor) |
||
145 | |||
146 | /** |
||
147 | * Returns the identifier of the module. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function __toString() |
||
155 | |||
156 | /** |
||
157 | * Returns the _flat_ version of the module's identifier. |
||
158 | * |
||
159 | * This method is the getter for the {@link $flat_id} magic property. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function get_flat_id() |
||
172 | |||
173 | /** |
||
174 | * Returns the primary model of the module. |
||
175 | * |
||
176 | * This is the getter for the {@link $model} magic property. |
||
177 | * |
||
178 | * @return ActiveRecord\Model |
||
179 | */ |
||
180 | protected function get_model() |
||
184 | |||
185 | /** |
||
186 | * Returns the module title, translated to the current language. |
||
187 | * |
||
188 | * @return string |
||
189 | * |
||
190 | * @deprecated |
||
191 | */ |
||
192 | protected function get_title() |
||
198 | |||
199 | /** |
||
200 | * Returns the parent module. |
||
201 | * |
||
202 | * @return Module|null |
||
203 | */ |
||
204 | protected function get_parent() |
||
208 | |||
209 | /** |
||
210 | * Checks if the module is installed. |
||
211 | * |
||
212 | * @param ErrorCollection $errors Error collection. |
||
213 | * |
||
214 | * @return mixed `true` if the module is installed, `false` if the module |
||
215 | * (or parts of) is not installed, `null` if the module has no installation. |
||
216 | */ |
||
217 | public function is_installed(ErrorCollection $errors) |
||
242 | |||
243 | /** |
||
244 | * Install the module. |
||
245 | * |
||
246 | * If the module has models they are installed. |
||
247 | * |
||
248 | * @param ErrorCollection $errors Error collection. |
||
249 | * |
||
250 | * @return boolean|null true if the module has successfully been installed, false if the |
||
251 | * module (or parts of the module) fails to install or null if the module has |
||
252 | * no installation process. |
||
253 | */ |
||
254 | public function install(ErrorCollection $errors) |
||
291 | |||
292 | /** |
||
293 | * Uninstall the module. |
||
294 | * |
||
295 | * Basically it uninstall the models installed by the module. |
||
296 | * |
||
297 | * @return boolean|null `true` if the module was successfully uninstalled. `false` if the module |
||
298 | * (or parts of the module) failed to uninstall. `null` if there is no uninstall process. |
||
299 | */ |
||
300 | public function uninstall() |
||
326 | |||
327 | /** |
||
328 | * Get a model from the module. |
||
329 | * |
||
330 | * If the model has not been created yet, it is created on the fly. |
||
331 | * |
||
332 | * @param string $which The identifier of the model to get. |
||
333 | * |
||
334 | * @return Model The requested model. |
||
335 | * |
||
336 | * @throws ModelNotDefined when the model is not defined by the module. |
||
337 | * @throws \RuntimeException when the class of the model does not exists. |
||
338 | */ |
||
339 | public function model($which = 'primary') |
||
397 | |||
398 | /** |
||
399 | * Resolves model tags. |
||
400 | * |
||
401 | * @param array|string $tags |
||
402 | * @param string $which |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | protected function resolve_model_tags($tags, $which) |
||
540 | |||
541 | /** |
||
542 | * Get a block. |
||
543 | * |
||
544 | * @param string $name The name of the block to get. |
||
545 | * |
||
546 | * @return mixed Depends on the implementation. Should return a string or an object |
||
547 | * implementing `__toString`. |
||
548 | * |
||
549 | * @throws \RuntimeException if the block is not defined. |
||
550 | */ |
||
551 | public function getBlock($name) |
||
572 | } |
||
573 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: