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:
Complex classes like DocumentController 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 DocumentController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class DocumentController extends Controller { |
||
44 | /** @var string */ |
||
45 | private $uid; |
||
46 | /** @var IL10N */ |
||
47 | private $l10n; |
||
48 | /** @var IConfig */ |
||
49 | private $settings; |
||
50 | /** @var AppConfig */ |
||
51 | private $appConfig; |
||
52 | /** @var ILogger */ |
||
53 | private $logger; |
||
54 | /** @var IManager */ |
||
55 | private $shareManager; |
||
56 | /** @var TokenManager */ |
||
57 | private $tokenManager; |
||
58 | /** @var ISession */ |
||
59 | private $session; |
||
60 | /** @var IRootFolder */ |
||
61 | private $rootFolder; |
||
62 | /** @var \OCA\Richdocuments\TemplateManager */ |
||
63 | private $templateManager; |
||
64 | /** @var FederationService */ |
||
65 | private $federationService; |
||
66 | |||
67 | const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt'; |
||
68 | |||
69 | /** |
||
70 | * @param string $appName |
||
71 | * @param IRequest $request |
||
72 | * @param IConfig $settings |
||
73 | * @param AppConfig $appConfig |
||
74 | * @param IL10N $l10n |
||
75 | * @param IManager $shareManager |
||
76 | * @param TokenManager $tokenManager |
||
77 | * @param IRootFolder $rootFolder |
||
78 | * @param ISession $session |
||
79 | * @param string $UserId |
||
80 | * @param ILogger $logger |
||
81 | */ |
||
82 | public function __construct( |
||
83 | $appName, |
||
84 | IRequest $request, |
||
85 | IConfig $settings, |
||
86 | AppConfig $appConfig, |
||
87 | IL10N $l10n, |
||
88 | IManager $shareManager, |
||
89 | TokenManager $tokenManager, |
||
90 | IRootFolder $rootFolder, |
||
91 | ISession $session, |
||
92 | $UserId, |
||
93 | ILogger $logger, |
||
94 | \OCA\Richdocuments\TemplateManager $templateManager, |
||
95 | FederationService $federationService |
||
96 | ) { |
||
97 | parent::__construct($appName, $request); |
||
98 | $this->uid = $UserId; |
||
99 | $this->l10n = $l10n; |
||
100 | $this->settings = $settings; |
||
101 | $this->appConfig = $appConfig; |
||
102 | $this->shareManager = $shareManager; |
||
103 | $this->tokenManager = $tokenManager; |
||
104 | $this->rootFolder = $rootFolder; |
||
105 | $this->session = $session; |
||
106 | $this->logger = $logger; |
||
107 | $this->templateManager = $templateManager; |
||
108 | $this->federationService = $federationService; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @PublicPage |
||
113 | * @NoCSRFRequired |
||
114 | * |
||
115 | * Returns the access_token and urlsrc for WOPI access for given $fileId |
||
116 | * Requests is accepted only when a secret_token is provided set by admin in |
||
117 | * settings page |
||
118 | * |
||
119 | * @param string $fileId |
||
120 | * @return array access_token, urlsrc |
||
121 | */ |
||
122 | public function extAppGetData($fileId) { |
||
159 | |||
160 | /** |
||
161 | * Strips the path and query parameters from the URL. |
||
162 | * |
||
163 | * @param string $url |
||
164 | * @return string |
||
165 | */ |
||
166 | private function domainOnly($url) { |
||
173 | |||
174 | /** |
||
175 | * Redirect to the files app with proper CSP headers set for federated editing |
||
176 | * This is a workaround since we cannot set a nonce for allowing dynamic URLs in the richdocument iframe |
||
177 | * |
||
178 | * @NoAdminRequired |
||
179 | * @NoCSRFRequired |
||
180 | */ |
||
181 | public function open($fileId) { |
||
213 | |||
214 | /** |
||
215 | * @NoAdminRequired |
||
216 | * |
||
217 | * @param string $fileId |
||
218 | * @return RedirectResponse|TemplateResponse |
||
219 | */ |
||
220 | public function index($fileId) { |
||
276 | |||
277 | /** |
||
278 | * @NoAdminRequired |
||
279 | * |
||
280 | * Create a new file from a template |
||
281 | * |
||
282 | * @param int $templateId |
||
283 | * @param string $fileName |
||
284 | * @param string $dir |
||
285 | * @return TemplateResponse |
||
286 | * @throws NotFoundException |
||
287 | * @throws NotPermittedException |
||
288 | * @throws \OCP\Files\InvalidPathException |
||
289 | */ |
||
290 | public function createFromTemplate($templateId, $fileName, $dir) { |
||
291 | if (!$this->templateManager->isTemplate($templateId)) { |
||
292 | return new TemplateResponse('core', '403', [], 'guest'); |
||
293 | } |
||
294 | |||
295 | $userFolder = $this->rootFolder->getUserFolder($this->uid); |
||
296 | try { |
||
297 | $folder = $userFolder->get($dir); |
||
298 | } catch (NotFoundException $e) { |
||
299 | return new TemplateResponse('core', '403', [], 'guest'); |
||
300 | } |
||
301 | |||
302 | if (!$folder instanceof Folder) { |
||
303 | return new TemplateResponse('core', '403', [], 'guest'); |
||
304 | } |
||
305 | |||
306 | $file = $folder->newFile($fileName); |
||
307 | |||
308 | $template = $this->templateManager->get($templateId); |
||
309 | list($urlSrc, $token) = $this->tokenManager->getTokenForTemplate($template, $this->uid, $file->getId()); |
||
310 | |||
311 | $params = [ |
||
312 | 'permissions' => $template->getPermissions(), |
||
313 | 'title' => $fileName, |
||
314 | 'fileId' => $template->getId() . '-' . $file->getId() . '_' . $this->settings->getSystemValue('instanceid'), |
||
315 | 'token' => $token, |
||
316 | 'urlsrc' => $urlSrc, |
||
317 | 'path' => $userFolder->getRelativePath($file->getPath()), |
||
318 | 'instanceId' => $this->settings->getSystemValue('instanceid'), |
||
319 | 'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'), |
||
320 | 'userId' => $this->uid |
||
321 | ]; |
||
322 | |||
323 | $response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); |
||
324 | $policy = new ContentSecurityPolicy(); |
||
325 | $policy->addAllowedFrameDomain($this->domainOnly($this->appConfig->getAppValue('public_wopi_url'))); |
||
326 | $policy->allowInlineScript(true); |
||
327 | $response->setContentSecurityPolicy($policy); |
||
328 | return $response; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @PublicPage |
||
333 | * @NoCSRFRequired |
||
334 | * |
||
335 | * @param string $shareToken |
||
336 | * @param string $fileName |
||
337 | * @return TemplateResponse |
||
338 | * @throws \Exception |
||
339 | */ |
||
340 | public function publicPage($shareToken, $fileName, $fileId) { |
||
388 | |||
389 | /** |
||
390 | * @PublicPage |
||
391 | * @NoCSRFRequired |
||
392 | * |
||
393 | * @param string $shareToken |
||
394 | * @param $remoteServer |
||
395 | * @param $remoteServerToken |
||
396 | * @param null $filePath |
||
397 | * @return TemplateResponse |
||
398 | */ |
||
399 | public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath = null) { |
||
460 | |||
461 | /** |
||
462 | * @NoAdminRequired |
||
463 | * |
||
464 | * @param string $mimetype |
||
465 | * @param string $filename |
||
466 | * @param string $dir |
||
467 | * @return JSONResponse |
||
468 | * @throws NotPermittedException |
||
469 | * @throws GenericFileException |
||
470 | */ |
||
471 | public function create($mimetype, |
||
561 | } |
||
562 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: