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:
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 | View Code Duplication | private function getLocaleDefinitions(array $locales) |
|
|
|||
75 | { |
||
76 | $localeDefinitions = []; |
||
77 | |||
78 | foreach ($locales as $locale) { |
||
79 | $localeDefinitions[$locale->locale] = $this->getLocaleDefinition($locale); |
||
80 | } |
||
81 | |||
82 | return $localeDefinitions; |
||
83 | } |
||
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) |
||
206 | |||
207 | /** |
||
208 | * @param SectionModel $section |
||
209 | * @param array $entryTypeDefinitions |
||
210 | * @param bool $force |
||
211 | */ |
||
212 | private function importEntryTypes(SectionModel $section, array $entryTypeDefinitions, $force) |
||
238 | |||
239 | /** |
||
240 | * Save the section manually if it is new to prevent craft from creating the default entry type |
||
241 | * In case of a single we do want the default entry type and do a normal save |
||
242 | * Todo: This method is a bit hackish, find a better way. |
||
243 | * |
||
244 | * @param SectionModel $section |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | private function preSaveSection(SectionModel $section) |
||
271 | |||
272 | /** |
||
273 | * Populate section. |
||
274 | * |
||
275 | * @param SectionModel $section |
||
276 | * @param array $sectionDefinition |
||
277 | * @param string $sectionHandle |
||
278 | */ |
||
279 | private function populateSection(SectionModel $section, array $sectionDefinition, $sectionHandle) |
||
293 | |||
294 | /** |
||
295 | * Populate section locales. |
||
296 | * |
||
297 | * @param SectionModel $section |
||
298 | * @param $localeDefinitions |
||
299 | */ |
||
300 | View Code Duplication | private function populateSectionLocales(SectionModel $section, $localeDefinitions) |
|
325 | |||
326 | /** |
||
327 | * Populate entry type. |
||
328 | * |
||
329 | * @param EntryTypeModel $entryType |
||
330 | * @param array $entryTypeDefinition |
||
331 | * @param string $entryTypeHandle |
||
332 | * @param int $sectionId |
||
333 | */ |
||
334 | View Code Duplication | private function populateEntryType(EntryTypeModel $entryType, array $entryTypeDefinition, $entryTypeHandle, $sectionId) |
|
348 | |||
349 | /** |
||
350 | * Reset craft section model cache using reflection |
||
351 | * @param SectionModel $section |
||
352 | */ |
||
353 | View Code Duplication | private function resetCraftFieldsSectionModelCache(SectionModel $section) |
|
361 | } |
||
362 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.