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 | ]; |
||
63 | const MIMES_SHEETS = [ |
||
64 | 'application/vnd.oasis.opendocument.spreadsheet-template' |
||
65 | ]; |
||
66 | const MIMES_PRESENTATIONS = [ |
||
67 | 'application/vnd.oasis.opendocument.presentation-template' |
||
68 | ]; |
||
69 | |||
70 | /** @var array Template mime types match */ |
||
71 | static public $tplTypes = [ |
||
72 | 'document' => self::MIMES_DOCUMENTS, |
||
73 | 'spreadsheet' => self::MIMES_SHEETS, |
||
74 | 'presentation' => self::MIMES_PRESENTATIONS |
||
75 | ]; |
||
76 | |||
77 | const TYPE_EXTENTION = [ |
||
78 | 'document' => 'odt', |
||
79 | 'spreadsheet' => 'ods', |
||
80 | 'presentation' => 'odp' |
||
81 | ]; |
||
82 | |||
83 | const TYPE_EXTENSION_OOXML = [ |
||
84 | 'document' => 'docx', |
||
85 | 'spreadsheet' => 'xlsx', |
||
86 | 'presentation' => 'pptx' |
||
87 | ]; |
||
88 | |||
89 | const EMPTY_TEMPLATE_ID_TYPE = [ |
||
90 | -1 => 'document', |
||
91 | -2 => 'spreadsheet', |
||
92 | -3 => 'presentation', |
||
93 | ]; |
||
94 | const EMPTY_TEMPLATE_TYPE_ID = [ |
||
95 | 'document' => -1, |
||
96 | 'spreadsheet' => -2, |
||
97 | 'presentation' => -3, |
||
98 | ]; |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Template manager |
||
103 | * |
||
104 | * @param string $appName |
||
105 | * @param string $userId |
||
106 | * @param IConfig $config |
||
107 | * @param Factory $appDataFactory |
||
|
|||
108 | * @param IURLGenerator $urlGenerator |
||
109 | * @param IRootFolder $rootFolder |
||
110 | * @param IL10N $l |
||
111 | * @throws \OCP\Files\NotPermittedException |
||
112 | */ |
||
113 | public function __construct($appName, |
||
132 | |||
133 | private function createAppDataFolders() { |
||
150 | |||
151 | public function setUserId($userId) { |
||
154 | |||
155 | /** |
||
156 | * Get template ISimpleFile|Node |
||
157 | * |
||
158 | * @param int $fileId |
||
159 | * @return File |
||
160 | */ |
||
161 | public function get($fileId) { |
||
189 | |||
190 | /** |
||
191 | * @param File[] $templates |
||
192 | * @return File[] |
||
193 | */ |
||
194 | private function filterTemplates($templates, $type = null) { |
||
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) { |
||
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) { |
||
302 | |||
303 | /** |
||
304 | * Get all templates |
||
305 | * |
||
306 | * @return File[] |
||
307 | */ |
||
308 | public function getAll($type = 'document') { |
||
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() { |
||
431 | |||
432 | /** |
||
433 | * @return Folder |
||
434 | */ |
||
435 | private function getEmptyTemplateDir() { |
||
440 | |||
441 | /** |
||
442 | * Format template file for json return object |
||
443 | * |
||
444 | * @param File $template |
||
445 | * @return array |
||
446 | */ |
||
447 | public function formatNodeReturn(File $template) { |
||
459 | |||
460 | public function isTemplate($fileId) { |
||
475 | |||
476 | public function formatEmpty(File $template) { |
||
486 | } |
||
487 |
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.