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 |
||
10 | abstract class Module extends ServiceProvider |
||
11 | { |
||
12 | use Macroable; |
||
13 | |||
14 | /** |
||
15 | * The laravel|lumen application instance. |
||
16 | * |
||
17 | * @var \Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
18 | */ |
||
19 | protected $app; |
||
20 | |||
21 | /** |
||
22 | * The module name. |
||
23 | * |
||
24 | * @var |
||
25 | */ |
||
26 | protected $name; |
||
27 | |||
28 | /** |
||
29 | * The module path. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $path; |
||
34 | |||
35 | /** |
||
36 | * @var array of cached Json objects, keyed by filename |
||
37 | */ |
||
38 | protected $moduleJson = []; |
||
39 | |||
40 | /** |
||
41 | * The constructor. |
||
42 | * |
||
43 | * @param Container $app |
||
44 | * @param $name |
||
45 | * @param $path |
||
46 | */ |
||
47 | 142 | public function __construct(Container $app, $name, $path) |
|
48 | { |
||
49 | 142 | parent::__construct($app); |
|
|
|||
50 | 142 | $this->name = $name; |
|
51 | 142 | $this->path = realpath($path); |
|
52 | 142 | } |
|
53 | |||
54 | /** |
||
55 | * Get laravel instance. |
||
56 | * |
||
57 | * @return \Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
58 | */ |
||
59 | 1 | public function getLaravel() |
|
60 | { |
||
61 | 1 | return $this->app; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get name. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | 2 | public function getName() |
|
70 | { |
||
71 | 2 | return $this->name; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Get name in lower case. |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 107 | public function getLowerName() |
|
80 | { |
||
81 | 107 | return strtolower($this->name); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Get name in studly case. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | 93 | public function getStudlyName() |
|
90 | { |
||
91 | 93 | return Str::studly($this->name); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get name in snake case. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | 6 | public function getSnakeName() |
|
100 | { |
||
101 | 6 | return Str::snake($this->name); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get description. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | 2 | public function getDescription() |
|
110 | { |
||
111 | 2 | return $this->get('description'); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get alias. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | 4 | public function getAlias() |
|
120 | { |
||
121 | 4 | return $this->get('alias'); |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Get priority. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getPriority() |
||
130 | { |
||
131 | return $this->get('priority'); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get module requirements. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 3 | public function getRequires() |
|
140 | { |
||
141 | 3 | return $this->get('requires'); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Get path. |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 126 | public function getPath() |
|
150 | { |
||
151 | 126 | return $this->path; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set path. |
||
156 | * |
||
157 | * @param string $path |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setPath($path) |
||
162 | { |
||
163 | $this->path = $path; |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Bootstrap the application events. |
||
170 | */ |
||
171 | 2 | public function boot() |
|
172 | { |
||
173 | 2 | if (config('modules.register.translations', true) === true) { |
|
174 | 2 | $this->registerTranslation(); |
|
175 | } |
||
176 | |||
177 | 2 | if ($this->isLoadFilesOnBoot()) { |
|
178 | $this->registerFiles(); |
||
179 | } |
||
180 | |||
181 | 2 | $this->fireEvent('boot'); |
|
182 | 2 | } |
|
183 | |||
184 | /** |
||
185 | * Register module's translation. |
||
186 | * |
||
187 | * @return void |
||
188 | */ |
||
189 | 2 | protected function registerTranslation() |
|
190 | { |
||
191 | 2 | $lowerName = $this->getLowerName(); |
|
192 | |||
193 | 2 | $langPath = $this->getPath() . '/Resources/lang'; |
|
194 | |||
195 | 2 | if (is_dir($langPath)) { |
|
196 | 2 | $this->loadTranslationsFrom($langPath, $lowerName); |
|
197 | } |
||
198 | 2 | } |
|
199 | |||
200 | /** |
||
201 | * Get json contents from the cache, setting as needed. |
||
202 | * |
||
203 | * @param string $file |
||
204 | * |
||
205 | * @return Json |
||
206 | */ |
||
207 | 34 | public function json($file = null) : Json |
|
208 | { |
||
209 | 34 | if ($file === null) { |
|
210 | 32 | $file = 'module.json'; |
|
211 | } |
||
212 | |||
213 | 34 | return array_get($this->moduleJson, $file, function () use ($file) { |
|
214 | 34 | return $this->moduleJson[$file] = new Json($this->getPath() . '/' . $file, $this->app['files']); |
|
215 | 34 | }); |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get a specific data from json file by given the key. |
||
220 | * |
||
221 | * @param string $key |
||
222 | * @param null $default |
||
223 | * |
||
224 | * @return mixed |
||
225 | */ |
||
226 | 24 | public function get(string $key, $default = null) |
|
227 | { |
||
228 | 24 | return $this->json()->get($key, $default); |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Get a specific data from composer.json file by given the key. |
||
233 | * |
||
234 | * @param $key |
||
235 | * @param null $default |
||
236 | * |
||
237 | * @return mixed |
||
238 | */ |
||
239 | 2 | public function getComposerAttr($key, $default = null) |
|
240 | { |
||
241 | 2 | return $this->json('composer.json')->get($key, $default); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Register the module. |
||
246 | */ |
||
247 | public function register() |
||
248 | { |
||
249 | $this->registerAliases(); |
||
250 | |||
251 | $this->registerProviders(); |
||
252 | |||
253 | if ($this->isLoadFilesOnBoot() === false) { |
||
254 | $this->registerFiles(); |
||
255 | } |
||
256 | |||
257 | $this->fireEvent('register'); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Register the module event. |
||
262 | * |
||
263 | * @param string $event |
||
264 | */ |
||
265 | 8 | protected function fireEvent($event) |
|
266 | { |
||
267 | 8 | $this->app['events']->fire(sprintf('modules.%s.' . $event, $this->getLowerName()), [$this]); |
|
268 | 8 | } |
|
269 | /** |
||
270 | * Register the aliases from this module. |
||
271 | */ |
||
272 | abstract public function registerAliases(); |
||
273 | |||
274 | /** |
||
275 | * Register the service providers from this module. |
||
276 | */ |
||
277 | abstract public function registerProviders(); |
||
278 | |||
279 | /** |
||
280 | * Get the path to the cached *_module.php file. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | abstract public function getCachedServicesPath(); |
||
285 | |||
286 | /** |
||
287 | * Register the files from this module. |
||
288 | */ |
||
289 | protected function registerFiles() |
||
290 | { |
||
291 | foreach ($this->get('files', []) as $file) { |
||
292 | include $this->path . '/' . $file; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Handle call __toString. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | 3 | public function __toString() |
|
302 | { |
||
303 | 3 | return $this->getStudlyName(); |
|
304 | } |
||
305 | |||
306 | /** |
||
307 | * Determine whether the given status same with the current module status. |
||
308 | * |
||
309 | * @param $status |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 11 | public function isStatus($status) : bool |
|
314 | { |
||
315 | 11 | return $this->get('active', 0) === $status; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Determine whether the current module activated. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 2 | public function enabled() : bool |
|
324 | { |
||
325 | 2 | return $this->isStatus(1); |
|
326 | } |
||
327 | |||
328 | /** |
||
329 | * Alternate for "enabled" method. |
||
330 | * |
||
331 | * @return bool |
||
332 | * @deprecated |
||
333 | */ |
||
334 | 6 | public function active() |
|
335 | { |
||
336 | 6 | return $this->isStatus(1); |
|
337 | } |
||
338 | |||
339 | /** |
||
340 | * Determine whether the current module not activated. |
||
341 | * |
||
342 | * @return bool |
||
343 | * @deprecated |
||
344 | */ |
||
345 | 2 | public function notActive() |
|
349 | |||
350 | /** |
||
351 | * Determine whether the current module not disabled. |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | 2 | public function disabled() : bool |
|
359 | |||
360 | /** |
||
361 | * Set active state for current module. |
||
362 | * |
||
363 | * @param $active |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | 6 | public function setActive($active) |
|
371 | |||
372 | /** |
||
373 | * Disable the current module. |
||
374 | */ |
||
375 | 3 | public function disable() |
|
383 | |||
384 | /** |
||
385 | * Enable the current module. |
||
386 | */ |
||
387 | 3 | public function enable() |
|
395 | |||
396 | /** |
||
397 | * Delete the current module. |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | 2 | public function delete() |
|
405 | |||
406 | /** |
||
407 | * Get extra path. |
||
408 | * |
||
409 | * @param string $path |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | 3 | public function getExtraPath(string $path) : string |
|
417 | |||
418 | /** |
||
419 | * Handle call to __get method. |
||
420 | * |
||
421 | * @param $key |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | public function __get($key) |
||
429 | |||
430 | /** |
||
431 | * Check if can load files of module on boot method. |
||
432 | * |
||
433 | * @return bool |
||
434 | */ |
||
435 | 2 | protected function isLoadFilesOnBoot() |
|
436 | { |
||
437 | 2 | return config('modules.register.files', 'register') === 'boot' && |
|
438 | // force register method if option == boot && app is AsgardCms |
||
439 | 2 | !class_exists('\Modules\Core\Foundation\AsgardCms'); |
|
440 | } |
||
441 | } |
||
442 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: