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 |
||
37 | class TokenManager { |
||
38 | /** @var IRootFolder */ |
||
39 | private $rootFolder; |
||
40 | /** @var IManager */ |
||
41 | private $shareManager; |
||
42 | /** @var IURLGenerator */ |
||
43 | private $urlGenerator; |
||
44 | /** @var Parser */ |
||
45 | private $wopiParser; |
||
46 | /** @var AppConfig */ |
||
47 | private $appConfig; |
||
48 | /** @var string */ |
||
49 | private $userId; |
||
50 | /** @var WopiMapper */ |
||
51 | private $wopiMapper; |
||
52 | /** @var IL10N */ |
||
53 | private $trans; |
||
54 | /** @var IUserManager */ |
||
55 | private $userManager; |
||
56 | /** @var IGroupManager */ |
||
57 | private $groupManager; |
||
58 | |||
59 | /** |
||
60 | * @param IRootFolder $rootFolder |
||
61 | * @param IManager $shareManager |
||
62 | * @param IURLGenerator $urlGenerator |
||
63 | * @param Parser $wopiParser |
||
64 | * @param AppConfig $appConfig |
||
65 | * @param string $UserId |
||
66 | * @param WopiMapper $wopiMapper |
||
67 | * @param IL10N $trans |
||
68 | */ |
||
69 | View Code Duplication | public function __construct(IRootFolder $rootFolder, |
|
|
|||
70 | IManager $shareManager, |
||
71 | IURLGenerator $urlGenerator, |
||
72 | Parser $wopiParser, |
||
73 | AppConfig $appConfig, |
||
74 | $UserId, |
||
75 | WopiMapper $wopiMapper, |
||
76 | IL10N $trans, |
||
77 | IUserManager $userManager, |
||
78 | IGroupManager $groupManager) { |
||
79 | $this->rootFolder = $rootFolder; |
||
80 | $this->shareManager = $shareManager; |
||
81 | $this->urlGenerator = $urlGenerator; |
||
82 | $this->wopiParser = $wopiParser; |
||
83 | $this->appConfig = $appConfig; |
||
84 | $this->trans = $trans; |
||
85 | $this->userId = $UserId; |
||
86 | $this->wopiMapper = $wopiMapper; |
||
87 | $this->userManager = $userManager; |
||
88 | $this->groupManager = $groupManager; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $fileId |
||
93 | * @param string $shareToken |
||
94 | * @param string $editoruid |
||
95 | * @return array |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | public function getToken($fileId, $shareToken = null, $editoruid = null) { |
||
99 | list($fileId,, $version) = Helper::parseFileId($fileId); |
||
100 | $owneruid = null; |
||
101 | $hideDownload = false; |
||
102 | // if the user is not logged-in do use the sharers storage |
||
103 | if($shareToken !== null) { |
||
104 | /** @var File $file */ |
||
105 | $rootFolder = $this->rootFolder; |
||
106 | $share = $this->shareManager->getShareByToken($shareToken); |
||
107 | $updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE); |
||
108 | $hideDownload = $share->getHideDownload(); |
||
109 | $owneruid = $share->getShareOwner(); |
||
110 | } else if (!is_null($this->userId)) { |
||
111 | try { |
||
112 | $editoruid = $this->userId; |
||
113 | $rootFolder = $this->rootFolder->getUserFolder($editoruid); |
||
114 | |||
115 | $files = $rootFolder->getById((int)$fileId); |
||
116 | $updatable = false; |
||
117 | foreach ($files as $file) { |
||
118 | if ($file->isUpdateable()) { |
||
119 | $updatable = true; |
||
120 | break; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | // Check if the editor (user who is accessing) is in editable group |
||
125 | // UserCanWrite only if |
||
126 | // 1. No edit groups are set or |
||
127 | // 2. if they are set, it is in one of the edit groups |
||
128 | $editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups'))); |
||
129 | $editorUser = $this->userManager->get($editoruid); |
||
130 | View Code Duplication | if ($updatable && count($editGroups) > 0 && $editorUser) { |
|
131 | $updatable = false; |
||
132 | foreach($editGroups as $editGroup) { |
||
133 | $editorGroup = $this->groupManager->get($editGroup); |
||
134 | if ($editorGroup !== null && $editorGroup->inGroup($editorUser)) { |
||
135 | $updatable = true; |
||
136 | break; |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | } catch (\Exception $e) { |
||
141 | throw $e; |
||
142 | } |
||
143 | } else { |
||
144 | // no active user login while generating the token |
||
145 | // this is required during WopiPutRelativeFile |
||
146 | if (is_null($editoruid)) { |
||
147 | \OC::$server->getLogger()->warning('Generating token for SaveAs without editoruid'); |
||
148 | } |
||
149 | $rootFolder = $this->rootFolder; |
||
150 | $updatable = true; |
||
151 | } |
||
152 | /** @var File $file */ |
||
153 | $file = $rootFolder->getById($fileId)[0]; |
||
154 | // If its a public share, use the owner from the share, otherwise check the file object |
||
155 | if (is_null($owneruid)) { |
||
156 | $owner = $file->getOwner(); |
||
157 | if (is_null($owner)) { |
||
158 | // Editor UID instead of owner UID in case owner is null e.g. group folders |
||
159 | $owneruid = $editoruid; |
||
160 | } else { |
||
161 | $owneruid = $owner->getUID(); |
||
162 | } |
||
163 | } |
||
164 | $serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost(); |
||
165 | |||
166 | if ($this->userId === null && isset($_COOKIE['guestUser']) && $_COOKIE['guestUser'] !== '') { |
||
167 | $guest_name = $this->trans->t('%s (Guest)', \OC_Util::sanitizeHTML($_COOKIE['guestUser'])); |
||
168 | } else { |
||
169 | $guest_name = NULL; |
||
170 | } |
||
171 | |||
172 | $wopi = $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, (int)$updatable, $serverHost, $guest_name, 0, $hideDownload); |
||
173 | |||
174 | try { |
||
175 | |||
176 | return [ |
||
177 | $this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'], |
||
178 | $wopi->getToken(), |
||
179 | ]; |
||
180 | } catch (\Exception $e){ |
||
181 | throw $e; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | public function getTokenForTemplate(File $file, $userId, $templateDestination) { |
||
215 | } |
||
216 |
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.