Complex classes like TemplateManager 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 TemplateManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class TemplateManager { |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $appName; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $userId; |
||
46 | |||
47 | /** @var IConfig */ |
||
48 | private $config; |
||
49 | |||
50 | /** @var IURLGenerator */ |
||
51 | private $urlGenerator; |
||
52 | |||
53 | /** @var IRootFolder */ |
||
54 | private $rootFolder; |
||
55 | |||
56 | /** @var IL10N */ |
||
57 | private $l; |
||
58 | |||
59 | /** Accepted templates mime types */ |
||
60 | const MIMES_DOCUMENTS = [ |
||
61 | 'application/vnd.oasis.opendocument.text-template', |
||
62 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
||
63 | 'application/msword' |
||
64 | ]; |
||
65 | const MIMES_SHEETS = [ |
||
66 | 'application/vnd.oasis.opendocument.spreadsheet-template', |
||
67 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
||
68 | 'application/vnd.ms-excel' |
||
69 | ]; |
||
70 | const MIMES_PRESENTATIONS = [ |
||
71 | 'application/vnd.oasis.opendocument.presentation-template', |
||
72 | 'application/vnd.openxmlformats-officedocument.presentationml.template', |
||
73 | 'application/vnd.ms-powerpoint' |
||
74 | ]; |
||
75 | |||
76 | /** @var array Template mime types match */ |
||
77 | static public $tplTypes = [ |
||
78 | 'document' => self::MIMES_DOCUMENTS, |
||
79 | 'spreadsheet' => self::MIMES_SHEETS, |
||
80 | 'presentation' => self::MIMES_PRESENTATIONS |
||
81 | ]; |
||
82 | |||
83 | const TYPE_EXTENTION = [ |
||
84 | 'document' => 'odt', |
||
85 | 'spreadsheet' => 'ods', |
||
86 | 'presentation' => 'odp' |
||
87 | ]; |
||
88 | |||
89 | const TYPE_EXTENSION_OOXML = [ |
||
90 | 'document' => 'docx', |
||
91 | 'spreadsheet' => 'xlsx', |
||
92 | 'presentation' => 'pptx' |
||
93 | ]; |
||
94 | |||
95 | const EMPTY_TEMPLATE_ID_TYPE = [ |
||
96 | -1 => 'document', |
||
97 | -2 => 'spreadsheet', |
||
98 | -3 => 'presentation', |
||
99 | ]; |
||
100 | const EMPTY_TEMPLATE_TYPE_ID = [ |
||
101 | 'document' => -1, |
||
102 | 'spreadsheet' => -2, |
||
103 | 'presentation' => -3, |
||
104 | ]; |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Template manager |
||
109 | * |
||
110 | * @param string $appName |
||
111 | * @param string $userId |
||
112 | * @param IConfig $config |
||
113 | * @param Factory $appDataFactory |
||
|
|||
114 | * @param IURLGenerator $urlGenerator |
||
115 | * @param IRootFolder $rootFolder |
||
116 | * @param IL10N $l |
||
117 | * @throws \OCP\Files\NotPermittedException |
||
118 | */ |
||
119 | public function __construct($appName, |
||
120 | $userId, |
||
121 | IConfig $config, |
||
122 | IAppData $appData, |
||
123 | IURLGenerator $urlGenerator, |
||
124 | IRootFolder $rootFolder, |
||
125 | IL10N $l) { |
||
126 | $this->appName = $appName; |
||
127 | $this->userId = $userId; |
||
128 | $this->config = $config; |
||
129 | $this->rootFolder = $rootFolder; |
||
130 | $this->urlGenerator = $urlGenerator; |
||
131 | |||
132 | |||
133 | $this->appData = $appData; |
||
134 | $this->createAppDataFolders(); |
||
135 | |||
136 | $this->l = $l; |
||
137 | } |
||
138 | |||
139 | private function createAppDataFolders() { |
||
140 | /* |
||
141 | * Init the appdata folder |
||
142 | * We need an actual folder for the fileid and previews. |
||
143 | * TODO: Fix this at some point |
||
144 | */ |
||
145 | try { |
||
146 | $this->appData->getFolder('templates'); |
||
147 | } catch (NotFoundException $e) { |
||
148 | $this->appData->newFolder('templates'); |
||
149 | } |
||
150 | try { |
||
151 | $this->appData->getFolder('empty_templates'); |
||
152 | } catch (NotFoundException $e) { |
||
153 | $this->appData->newFolder('empty_templates'); |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function setUserId($userId) { |
||
158 | $this->userId = $userId; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get template ISimpleFile|Node |
||
163 | * |
||
164 | * @param int $fileId |
||
165 | * @return File |
||
166 | */ |
||
167 | public function get($fileId) { |
||
168 | // is this a global template ? |
||
169 | $files = $this->getEmptyTemplateDir()->getDirectoryListing(); |
||
170 | |||
171 | foreach ($files as $file) { |
||
172 | if ($file->getId() === $fileId) { |
||
173 | return $file; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // is this a global template ? |
||
178 | $files = $this->getSystemTemplateDir()->getDirectoryListing(); |
||
179 | |||
180 | foreach ($files as $file) { |
||
181 | if ($file->getId() === $fileId) { |
||
182 | return $file; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $templateDir = $this->getUserTemplateDir(); |
||
187 | // finally get the template file |
||
188 | $files = $templateDir->getById($fileId); |
||
189 | if ($files !== []) { |
||
190 | return $files[0]; |
||
191 | } |
||
192 | |||
193 | throw new NotFoundException(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param File[] $templates |
||
198 | * @return File[] |
||
199 | */ |
||
200 | private function filterTemplates($templates, $type = null) { |
||
201 | return array_filter($templates, function (Node $templateFile) use ($type) { |
||
202 | if (!($templateFile instanceof File)) { |
||
203 | return false; |
||
204 | } |
||
205 | |||
206 | return $this->isValidTemplateMime($templateFile->getMimeType(), $type); |
||
207 | }); |
||
208 | } |
||
209 | |||
210 | private function getEmpty($type = null) { |
||
232 | |||
233 | /** |
||
234 | * Remove empty_templates in appdata and recreate it from the apps templates |
||
235 | */ |
||
236 | public function updateEmptyTemplates() { |
||
245 | |||
246 | /** |
||
247 | * Get all global templates |
||
248 | * |
||
249 | * @return File[] |
||
250 | */ |
||
251 | public function getSystem($type = null) { |
||
257 | |||
258 | /** |
||
259 | * @return array |
||
260 | */ |
||
261 | public function getSystemFormatted($type = null) { |
||
262 | $empty = $this->getEmpty($type); |
||
263 | $system = $this->getSystem($type); |
||
264 | |||
265 | $emptyFormatted = array_map(function(File $file) { |
||
266 | return $this->formatEmpty($file); |
||
267 | }, $empty); |
||
268 | |||
269 | $systemFormatted = array_map(function(File $file) { |
||
270 | return $this->formatNodeReturn($file); |
||
271 | }, $system); |
||
272 | |||
273 | return array_merge($emptyFormatted, $systemFormatted); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Get all user templates |
||
278 | * |
||
279 | * @return File[] |
||
280 | */ |
||
281 | public function getUser($type = null) { |
||
291 | |||
292 | /** |
||
293 | * @return array |
||
294 | */ |
||
295 | public function getUserFormatted($type) { |
||
296 | $templates = $this->getUser($type); |
||
297 | |||
298 | return array_map(function(File $file) { |
||
299 | return $this->formatNodeReturn($file); |
||
300 | }, $templates); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Get all templates |
||
305 | * |
||
306 | * @return File[] |
||
307 | */ |
||
308 | public function getAll($type = 'document') { |
||
309 | $system = $this->getSystem(); |
||
310 | $user = $this->getUser(); |
||
311 | |||
312 | if (!array_key_exists($type, self::$tplTypes)) { |
||
313 | return []; |
||
314 | } |
||
315 | |||
316 | return array_values(array_filter(array_merge($user, $system), function (File $template) use ($type) { |
||
317 | foreach (self::$tplTypes[$type] as $mime) { |
||
318 | if ($template->getMimeType() === $mime) { |
||
319 | return true; |
||
320 | } |
||
321 | } |
||
322 | return false; |
||
323 | })); |
||
324 | } |
||
325 | |||
326 | public function getAllFormatted($type) { |
||
336 | |||
337 | /** |
||
338 | * Add a template to the global template folder |
||
339 | * |
||
340 | * @param string $templateName |
||
341 | * @param string $templateFile |
||
342 | * @return array |
||
343 | */ |
||
344 | public function add($templateName, $templateFile) { |
||
356 | |||
357 | /** |
||
358 | * Delete a template to the global template folder |
||
359 | * |
||
360 | * @param int $fileId |
||
361 | * @return boolean |
||
362 | * @throws NotFoundException |
||
363 | */ |
||
364 | public function delete($fileId) { |
||
375 | |||
376 | /** |
||
377 | * Flip $tplTypes to retrieve types by mime |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | private function flipTypes() { |
||
389 | |||
390 | /** |
||
391 | * Get the user template directory |
||
392 | * |
||
393 | * @return Folder |
||
394 | * @throws NotFoundException |
||
395 | */ |
||
396 | private function getUserTemplateDir() { |
||
422 | |||
423 | /** |
||
424 | * @return Folder |
||
425 | */ |
||
426 | private function getSystemTemplateDir() { |
||
427 | $path = 'appdata_' . $this->config->getSystemValue('instanceid', null) . '/richdocuments/templates'; |
||
428 | return $this->rootFolder->get($path); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @return Folder |
||
433 | */ |
||
434 | private function getEmptyTemplateDir() { |
||
438 | |||
439 | /** |
||
440 | * Format template file for json return object |
||
441 | * |
||
442 | * @param File $template |
||
443 | * @return array |
||
444 | */ |
||
445 | public function formatNodeReturn(File $template) { |
||
457 | |||
458 | public function isTemplate($fileId) { |
||
473 | |||
474 | public function formatEmpty(File $template) { |
||
484 | |||
485 | public function isValidTemplateMime($mime, $type = null) { |
||
499 | } |
||
500 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.