Complex classes like Sections 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 Sections, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Sections extends Base |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Export sections. |
||
| 26 | * |
||
| 27 | * @param SectionModel[] $sections |
||
| 28 | * @param array|null $allowedEntryTypeIds |
||
| 29 | * |
||
| 30 | * @return array |
||
| 31 | */ |
||
| 32 | public function export(array $sections = [], array $allowedEntryTypeIds = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get section definition. |
||
| 47 | * |
||
| 48 | * @param SectionModel $section |
||
| 49 | * @param $allowedEntryTypeIds |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | private function getSectionDefinition(SectionModel $section, $allowedEntryTypeIds) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get locale definitions. |
||
| 69 | * |
||
| 70 | * @param SectionLocaleModel[] $locales |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | private function getLocaleDefinitions(array $locales) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get locale definition. |
||
| 87 | * |
||
| 88 | * @param SectionLocaleModel $locale |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | private function getLocaleDefinition(SectionLocaleModel $locale) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get entry type definitions. |
||
| 103 | * |
||
| 104 | * @param array $entryTypes |
||
| 105 | * @param $allowedEntryTypeIds |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | private function getEntryTypeDefinitions(array $entryTypes, $allowedEntryTypeIds) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get entry type definition. |
||
| 124 | * |
||
| 125 | * @param EntryTypeModel $entryType |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | private function getEntryTypeDefinition(EntryTypeModel $entryType) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Attempt to import sections. |
||
| 142 | * |
||
| 143 | * @param array $sectionDefinitions |
||
| 144 | * @param bool $force If set to true sections not included in the import will be deleted |
||
| 145 | * |
||
| 146 | * @return Result |
||
| 147 | */ |
||
| 148 | public function import(array $sectionDefinitions, $force = false) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param SectionModel $section |
||
| 210 | * @param array $entryTypeDefinitions |
||
| 211 | * @param bool $force |
||
| 212 | */ |
||
| 213 | private function importEntryTypes(SectionModel $section, array $entryTypeDefinitions, $force) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Save the section manually if it is new to prevent craft from creating the default entry type |
||
| 242 | * In case of a single we do want the default entry type and do a normal save |
||
| 243 | * Todo: This method is a bit hackish, find a better way. |
||
| 244 | * |
||
| 245 | * @param SectionModel $section |
||
| 246 | * |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | private function preSaveSection(SectionModel $section) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Populate section. |
||
| 275 | * |
||
| 276 | * @param SectionModel $section |
||
| 277 | * @param array $sectionDefinition |
||
| 278 | * @param string $sectionHandle |
||
| 279 | */ |
||
| 280 | private function populateSection(SectionModel $section, array $sectionDefinition, $sectionHandle) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Populate section locales. |
||
| 297 | * |
||
| 298 | * @param SectionModel $section |
||
| 299 | * @param $localeDefinitions |
||
| 300 | */ |
||
| 301 | private function populateSectionLocales(SectionModel $section, $localeDefinitions) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Populate entry type. |
||
| 329 | * |
||
| 330 | * @param EntryTypeModel $entryType |
||
| 331 | * @param array $entryTypeDefinition |
||
| 332 | * @param string $entryTypeHandle |
||
| 333 | * @param int $sectionId |
||
| 334 | */ |
||
| 335 | private function populateEntryType(EntryTypeModel $entryType, array $entryTypeDefinition, $entryTypeHandle, $sectionId) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Reset craft sections service cache using reflection. |
||
| 352 | */ |
||
| 353 | private function resetCraftSectionsServiceCache() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Reset craft section model cache using reflection. |
||
| 371 | * |
||
| 372 | * @param SectionModel $section |
||
| 373 | */ |
||
| 374 | private function resetCraftFieldsSectionModelCache(SectionModel $section) |
||
| 382 | } |
||
| 383 |