Complex classes like ModuleInstaller 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 ModuleInstaller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | abstract class ModuleInstaller |
||
26 | { |
||
27 | /** |
||
28 | * Database connection instance |
||
29 | * |
||
30 | * @var SpoonDatabase |
||
31 | */ |
||
32 | private $database; |
||
33 | |||
34 | /** |
||
35 | * The module name. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $module; |
||
40 | |||
41 | /** |
||
42 | * The default extras that have to be added to every page. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $defaultExtras = []; |
||
47 | |||
48 | /** |
||
49 | * The frontend language(s) |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $languages = []; |
||
54 | |||
55 | /** |
||
56 | * The backend language(s) |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $interfaceLanguages = []; |
||
61 | |||
62 | /** |
||
63 | * Cached modules |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private static $modules = []; |
||
68 | |||
69 | /** |
||
70 | * The variables passed by the installer |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $variables = []; |
||
75 | |||
76 | /** |
||
77 | * Should example data be installed. |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $example; |
||
82 | |||
83 | /** |
||
84 | * @var \Symfony\Component\Console\Input\InputInterface|null |
||
85 | */ |
||
86 | private $input; |
||
87 | |||
88 | 1 | /** |
|
89 | * @var \Symfony\Component\Console\Output\OutputInterface|null |
||
90 | */ |
||
91 | private $output; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | 1 | */ |
|
96 | 1 | protected $promptVariables = []; |
|
97 | 1 | ||
98 | 1 | /** |
|
99 | 1 | * @param SpoonDatabase $database The database-connection. |
|
100 | 1 | * @param array $languages The selected frontend languages. |
|
101 | * @param array $interfaceLanguages The selected backend languages. |
||
102 | * @param bool $example Should example data be installed. |
||
103 | * @param array $variables The passed variables. |
||
104 | */ |
||
105 | public function __construct( |
||
118 | |||
119 | 1 | /** |
|
120 | * @return null|\Symfony\Component\Console\Input\InputInterface |
||
121 | 1 | */ |
|
122 | public function getInput(): ?InputInterface |
||
126 | |||
127 | 1 | /** |
|
128 | 1 | * @param null|\Symfony\Component\Console\Input\InputInterface $input |
|
129 | */ |
||
130 | public function setInput($input): void |
||
134 | 1 | ||
135 | /** |
||
136 | * @return null|\Symfony\Component\Console\Output\OutputInterface |
||
137 | */ |
||
138 | public function getOutput(): ?OutputInterface |
||
142 | |||
143 | /** |
||
144 | * @param null|\Symfony\Component\Console\Output\OutputInterface $output |
||
145 | */ |
||
146 | public function setOutput($output): void |
||
150 | |||
151 | /** |
||
152 | 1 | * @return array |
|
153 | */ |
||
154 | public function getPromptVariables(): array |
||
158 | |||
159 | /** |
||
160 | * Adds a default extra to the stack of extras |
||
161 | 1 | * |
|
162 | * @param int $extraId The extra id to add to every page. |
||
163 | * @param string $position The position to put the default extra. |
||
164 | */ |
||
165 | protected function addDefaultExtra(int $extraId, string $position): void |
||
169 | |||
170 | /** |
||
171 | 1 | * Inserts a new module. |
|
172 | * The getModule method becomes available after using addModule and returns $module parameter. |
||
173 | 1 | * |
|
174 | * @param string $module The name of the module. |
||
175 | */ |
||
176 | 1 | protected function addModule(string $module): void |
|
197 | |||
198 | /** |
||
199 | * Add a search index |
||
200 | * |
||
201 | * @param string $module The module wherein will be searched. |
||
202 | * @param int $otherId The id of the record. |
||
203 | * @param array $fields A key/value pair of fields to index. |
||
204 | * @param string $language The frontend language for this entry. |
||
205 | */ |
||
206 | 1 | protected function addSearchIndex(string $module, int $otherId, array $fields, string $language): void |
|
249 | |||
250 | /** |
||
251 | * Method that will be overridden by the specific installers |
||
252 | */ |
||
253 | protected function execute(): void |
||
257 | 1 | ||
258 | /** |
||
259 | 1 | * Get the database-handle |
|
260 | * |
||
261 | * @return SpoonDatabase |
||
262 | */ |
||
263 | protected function getDatabase(): SpoonDatabase |
||
267 | 1 | ||
268 | /** |
||
269 | 1 | * Get the module name |
|
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | protected function getModule(): string |
||
277 | |||
278 | /** |
||
279 | * Set the module name |
||
280 | * |
||
281 | * @param string $module |
||
282 | */ |
||
283 | 1 | protected function setModule(string $module): void |
|
287 | |||
288 | /** |
||
289 | * Get the default extras. |
||
290 | 1 | * |
|
291 | 1 | * @return array |
|
292 | */ |
||
293 | public function getDefaultExtras(): array |
||
297 | 1 | ||
298 | /** |
||
299 | * Get the default user |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | protected function getDefaultUserID(): int |
||
318 | |||
319 | /** |
||
320 | * Get the selected cms interface languages |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function getInterfaceLanguages(): array |
||
328 | 1 | ||
329 | /** |
||
330 | * Get the selected languages |
||
331 | 1 | * |
|
332 | 1 | * @return array |
|
333 | */ |
||
334 | protected function getLanguages(): array |
||
338 | |||
339 | /** |
||
340 | * Get a locale item. |
||
341 | 1 | * |
|
342 | 1 | * @param string $name |
|
343 | * @param string $module |
||
344 | * @param string $language The language abbreviation. |
||
345 | * @param string $type The type of locale. |
||
346 | 1 | * @param string $application |
|
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function getLocale( |
||
366 | |||
367 | /** |
||
368 | 1 | * Get a setting |
|
369 | * |
||
370 | * @param string $module The name of the module. |
||
371 | 1 | * @param string $name The name of the setting. |
|
372 | * |
||
373 | * @return mixed |
||
374 | 1 | */ |
|
375 | protected function getSetting(string $module, string $name) |
||
386 | |||
387 | 1 | /** |
|
388 | 1 | * Get the id of the requested template of the active theme. |
|
389 | 1 | * |
|
390 | 1 | * @param string $template |
|
391 | 1 | * @param string $theme |
|
392 | 1 | * |
|
393 | 1 | * @return int |
|
394 | */ |
||
395 | 1 | protected function getTemplateId(string $template, string $theme = null): int |
|
416 | |||
417 | /** |
||
418 | 1 | * Get a variable |
|
419 | * |
||
420 | * @param string $name |
||
421 | 1 | * |
|
422 | 1 | * @return mixed |
|
423 | 1 | */ |
|
424 | public function getVariable(string $name) |
||
428 | |||
429 | /** |
||
430 | * Set a variables |
||
431 | 1 | * |
|
432 | * @param array $variables |
||
433 | 1 | */ |
|
434 | public function setVariables(array $variables): void |
||
438 | |||
439 | 1 | /** |
|
440 | * Imports the locale XML file |
||
441 | * |
||
442 | 1 | * @param string $filename The full path for the XML-file. |
|
443 | 1 | * @param bool $overwriteConflicts Should we overwrite when there is a conflict? |
|
444 | 1 | */ |
|
445 | 1 | protected function importLocale(string $filename, bool $overwriteConflicts = false): void |
|
473 | |||
474 | 1 | /** |
|
475 | 1 | * Imports the sql file |
|
476 | 1 | * |
|
477 | * @param string $filename The full path for the SQL-file. |
||
478 | */ |
||
479 | protected function importSQL(string $filename): void |
||
491 | |||
492 | protected function insertDashboardWidget(string $module, string $widget): void |
||
547 | 1 | ||
548 | 1 | private function getNextSequenceForModule(string $module): int |
|
565 | |||
566 | /** |
||
567 | * Insert an extra |
||
568 | * |
||
569 | * @param string $module The module for the extra. |
||
570 | * @param ModuleExtraType $type The type, possible values are: homepage, widget, block. |
||
571 | * @param string $label The label for the extra. |
||
572 | 1 | * @param string|null $action The action. |
|
573 | * @param array|null $data data, will be passed in the extra. |
||
574 | * @param bool $hidden Is this extra hidden? |
||
575 | * @param int|null $sequence The sequence for the extra. |
||
576 | * |
||
577 | * @return int |
||
578 | */ |
||
579 | protected function insertExtra( |
||
603 | |||
604 | /** |
||
605 | * @param string $module |
||
606 | * @param ModuleExtraType $type |
||
607 | * @param string $label |
||
608 | * @param array|null $data |
||
609 | * |
||
610 | * @return int |
||
611 | */ |
||
612 | 1 | private function findModuleExtraId(string $module, ModuleExtraType $type, string $label, array $data = null): int |
|
630 | 1 | ||
631 | /** |
||
632 | 1 | * Insert a meta item |
|
633 | * |
||
634 | 1 | * @param string $keywords The keyword of the item. |
|
635 | 1 | * @param string $description A description of the item. |
|
636 | 1 | * @param string $title The page title for the item. |
|
637 | * @param string $url The unique URL. |
||
638 | * @param bool $keywordsOverwrite Should the keywords be overwritten? |
||
639 | 1 | * @param bool $descriptionOverwrite Should the descriptions be overwritten? |
|
640 | * @param bool $titleOverwrite Should the page title be overwritten? |
||
641 | * @param bool $urlOverwrite Should the URL be overwritten? |
||
642 | * @param string $custom Any custom meta-data. |
||
643 | * @param string $seoFollow Any custom meta-data. |
||
644 | * @param string $seoIndex Any custom meta-data. |
||
645 | * @param array $data Any custom meta-data. |
||
646 | * |
||
647 | * @return int |
||
648 | */ |
||
649 | protected function insertMeta( |
||
681 | 1 | ||
682 | 1 | /** |
|
683 | 1 | * Looks for the next page id, if it is the first page it will default to 1 |
|
684 | 1 | * |
|
685 | * @param string $language |
||
686 | * |
||
687 | * @return int |
||
688 | 1 | */ |
|
689 | private function getNextPageIdForLanguage(string $language): int |
||
698 | 1 | ||
699 | 1 | private function archiveAllRevisionsOfAPageForLanguage(int $pageId, string $language): void |
|
708 | 1 | ||
709 | 1 | private function getNextPageSequence(string $language, int $parentId, string $type): int |
|
718 | 1 | ||
719 | /** |
||
720 | * Add the missing data to the meta record |
||
721 | 1 | * |
|
722 | * @param array $meta |
||
723 | * @param string $defaultValue |
||
724 | * |
||
725 | * @return array |
||
726 | */ |
||
727 | private function completeMetaRecord(array $meta, string $defaultValue): array |
||
744 | |||
745 | private function getNewMetaId(array $meta, string $defaultValue): int |
||
764 | |||
765 | 1 | private function completePageRevisionRecord(array $revision, array $meta = []): array |
|
800 | |||
801 | /** |
||
802 | * Insert a page |
||
803 | * |
||
804 | * @param array $revision An array with the revision data. |
||
805 | * @param array $meta The meta-data. |
||
806 | 1 | * @param array[] $blocks The blocks. |
|
807 | * |
||
808 | 1 | * @throws \SpoonDatabaseException |
|
809 | * @throws \SpoonException |
||
810 | * |
||
811 | * @return int |
||
812 | */ |
||
813 | protected function insertPage(array $revision, array $meta = null, array ...$blocks): int |
||
844 | |||
845 | private function completePageBlockRecords(array $blocks, int $defaultRevisionId): array |
||
877 | 1 | ||
878 | /** |
||
879 | * Should example data be installed |
||
880 | * |
||
881 | 1 | * @return bool |
|
882 | 1 | */ |
|
883 | protected function installExample(): bool |
||
887 | |||
888 | 1 | /** |
|
889 | * Make a module searchable |
||
890 | 1 | * |
|
891 | * @param string $module The module to make searchable. |
||
892 | * @param bool $searchable Enable/disable search for this module by default? |
||
893 | 1 | * @param int $weight Set default search weight for this module. |
|
894 | 1 | */ |
|
895 | protected function makeSearchable(string $module, bool $searchable = true, int $weight = 1): void |
||
903 | |||
904 | /** |
||
905 | * Set the rights for an action |
||
906 | * |
||
907 | * @param int $groupId The group wherefore the rights will be set. |
||
908 | * @param string $module The module wherein the action appears. |
||
909 | * @param string $action The action wherefore the rights have to set. |
||
910 | * @param int $level The level, default is 7 (max). |
||
911 | */ |
||
912 | protected function setActionRights(int $groupId, string $module, string $action, int $level = 7): void |
||
937 | |||
938 | /** |
||
939 | * Sets the rights for a module |
||
940 | 1 | * |
|
941 | 1 | * @param int $groupId The group wherefore the rights will be set. |
|
942 | * @param string $module The module too set the rights for. |
||
943 | 1 | */ |
|
944 | 1 | protected function setModuleRights(int $groupId, string $module): void |
|
966 | 1 | ||
967 | private function getNextBackendNavigationSequence(int $parentId): int |
||
979 | |||
980 | /** |
||
981 | 1 | * Set a new navigation item. |
|
982 | * |
||
983 | * @param int|null $parentId Id of the navigation item under we should add this. |
||
984 | 1 | * @param string $label Label for the item. |
|
985 | * @param string|null $url Url for the item. If omitted the first child is used. |
||
986 | * @param array $selectedFor Set selected when these actions are active. |
||
987 | * @param int $sequence Sequence to use for this item. |
||
988 | 1 | * |
|
989 | 1 | * @return int |
|
990 | */ |
||
991 | 1 | protected function setNavigation( |
|
1028 | |||
1029 | /** |
||
1030 | * Stores a module specific setting in the database. |
||
1031 | * |
||
1032 | * @param string $module The module wherefore the setting will be set. |
||
1033 | * @param string $name The name of the setting. |
||
1034 | * @param mixed $value The optional value. |
||
1035 | * @param bool $overwrite Overwrite no matter what. |
||
1036 | */ |
||
1037 | protected function setSetting(string $module, string $name, $value = null, bool $overwrite = false): void |
||
1074 | |||
1075 | private function getAndCopyRandomImage(): string |
||
1095 | } |
||
1096 |