Total Complexity | 59 |
Total Lines | 345 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like SectionExport 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.
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 SectionExport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class SectionExport |
||
16 | { |
||
17 | private $course; |
||
18 | |||
19 | /** |
||
20 | * Constructor to initialize the course object. |
||
21 | * |
||
22 | * @param object $course The course object to be exported. |
||
23 | */ |
||
24 | public function __construct($course) |
||
25 | { |
||
26 | $this->course = $course; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Export a section and its activities to the specified directory. |
||
31 | */ |
||
32 | public function exportSection(int $sectionId, string $exportDir): void |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get all general items not linked to any lesson (learnpath). |
||
66 | */ |
||
67 | public function getGeneralItems(): array |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the activities for the general section. |
||
106 | */ |
||
107 | public function getActivitiesForGeneral(): array |
||
108 | { |
||
109 | $generalLearnpath = (object) [ |
||
110 | 'items' => $this->getGeneralItems(), |
||
111 | 'source_id' => 0, |
||
112 | ]; |
||
113 | |||
114 | $activities = $this->getActivitiesForSection($generalLearnpath, true); |
||
115 | |||
116 | if (!in_array('folder', array_column($activities, 'modulename'))) { |
||
117 | $activities[] = [ |
||
118 | 'id' => 0, |
||
119 | 'moduleid' => 0, |
||
120 | 'modulename' => 'folder', |
||
121 | 'name' => 'Documents', |
||
122 | 'sectionid' => 0, |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | return $activities; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get the learnpath object by its ID. |
||
131 | */ |
||
132 | public function getLearnpathById(int $sectionId): ?object |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get section data for a learnpath. |
||
145 | */ |
||
146 | public function getSectionData(object $learnpath): array |
||
157 | ]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get the activities for a specific section. |
||
162 | */ |
||
163 | public function getActivitiesForSection(object $learnpath, bool $isGeneral = false): array |
||
164 | { |
||
165 | $activities = []; |
||
166 | $sectionId = $isGeneral ? 0 : $learnpath->source_id; |
||
167 | |||
168 | foreach ($learnpath->items as $item) { |
||
169 | $this->addActivityToList($item, $sectionId, $activities); |
||
170 | } |
||
171 | |||
172 | return $activities; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Export the activities of a section. |
||
177 | */ |
||
178 | private function exportActivities(array $activities, string $exportDir, int $sectionId): void |
||
179 | { |
||
180 | $exportClasses = [ |
||
181 | 'quiz' => QuizExport::class, |
||
182 | 'glossary' => GlossaryExport::class, |
||
183 | 'url' => UrlExport::class, |
||
184 | 'assign' => AssignExport::class, |
||
185 | 'forum' => ForumExport::class, |
||
186 | 'page' => PageExport::class, |
||
187 | 'resource' => ResourceExport::class, |
||
188 | 'folder' => FolderExport::class, |
||
189 | 'feedback' => FeedbackExport::class, |
||
190 | ]; |
||
191 | |||
192 | foreach ($activities as $activity) { |
||
193 | $moduleName = $activity['modulename']; |
||
194 | if (isset($exportClasses[$moduleName])) { |
||
195 | $exportClass = new $exportClasses[$moduleName]($this->course); |
||
196 | $exportClass->export($activity['id'], $exportDir, $activity['moduleid'], $sectionId); |
||
197 | } else { |
||
198 | throw new \Exception("Export for module '$moduleName' is not supported."); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Check if an item is associated with any learnpath. |
||
205 | */ |
||
206 | private function isItemInLearnpath(object $item, string $type): bool |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Add an activity to the activities list. |
||
225 | */ |
||
226 | private function addActivityToList(array $item, int $sectionId, array &$activities): void |
||
304 | ]; |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Determine the document type based on filetype and path. |
||
310 | */ |
||
311 | private function getDocumentType(string $filetype, string $path): ?string |
||
312 | { |
||
313 | if ('html' === pathinfo($path, PATHINFO_EXTENSION)) { |
||
314 | return 'page'; |
||
315 | } elseif ('file' === $filetype) { |
||
316 | return 'resource'; |
||
317 | } /*elseif ('folder' === $filetype) { |
||
318 | return 'folder'; |
||
319 | }*/ |
||
320 | |||
321 | return null; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Create the section.xml file. |
||
326 | */ |
||
327 | private function createSectionXml(array $sectionData, string $destinationDir): void |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Create the inforef.xml file for the section. |
||
346 | */ |
||
347 | private function createInforefXml(array $sectionData, string $destinationDir): void |
||
362 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.