Conditions | 24 |
Paths | 453 |
Total Lines | 95 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
124 | public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) { |
||
125 | |||
126 | // check if all required parameters are set |
||
127 | if ($shareWith === null || |
||
128 | $name === null || |
||
129 | $providerId === null || |
||
130 | $owner === null || |
||
131 | $resourceType === null || |
||
132 | $shareType === null || |
||
133 | !is_array($protocol) || |
||
134 | !isset($protocol['name']) || |
||
135 | !isset ($protocol['options']) || |
||
136 | !is_array($protocol['options']) || |
||
137 | !isset($protocol['options']['sharedSecret']) |
||
138 | ) { |
||
139 | return new JSONResponse( |
||
140 | ['message' => 'Missing arguments'], |
||
141 | Http::STATUS_BAD_REQUEST |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $supportedShareTypes = $this->config->getSupportedShareTypes($resourceType); |
||
146 | if (!in_array($shareType, $supportedShareTypes)) { |
||
147 | return new JSONResponse( |
||
148 | ['message' => 'Share type "' . $shareType . '" not implemented'], |
||
149 | Http::STATUS_NOT_IMPLEMENTED |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | $cloudId = $this->cloudIdManager->resolveCloudId($shareWith); |
||
154 | $shareWith = $cloudId->getUser(); |
||
155 | |||
156 | if ($shareType === 'user') { |
||
157 | $shareWith = $this->mapUid($shareWith); |
||
158 | |||
159 | if (!$this->userManager->userExists($shareWith)) { |
||
160 | return new JSONResponse( |
||
161 | ['message' => 'User "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()], |
||
162 | Http::STATUS_BAD_REQUEST |
||
163 | ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if ($shareType === 'group') { |
||
168 | if(!$this->groupManager->groupExists($shareWith)) { |
||
169 | return new JSONResponse( |
||
170 | ['message' => 'Group "' . $shareWith . '" does not exists at ' . $this->urlGenerator->getBaseUrl()], |
||
171 | Http::STATUS_BAD_REQUEST |
||
172 | ); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | // if no explicit display name is given, we use the uid as display name |
||
177 | $ownerDisplayName = $ownerDisplayName === null ? $owner : $ownerDisplayName; |
||
178 | $sharedByDisplayName = $sharedByDisplayName === null ? $sharedBy : $sharedByDisplayName; |
||
179 | |||
180 | // sharedBy* parameter is optional, if nothing is set we assume that it is the same user as the owner |
||
181 | if ($sharedBy === null) { |
||
182 | $sharedBy = $owner; |
||
183 | $sharedByDisplayName = $ownerDisplayName; |
||
184 | } |
||
185 | |||
186 | try { |
||
187 | $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType); |
||
188 | $share = $this->factory->getCloudFederationShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, '', $shareType, $resourceType); |
||
189 | $share->setProtocol($protocol); |
||
190 | $provider->shareReceived($share); |
||
191 | } catch (ProviderDoesNotExistsException $e) { |
||
192 | return new JSONResponse( |
||
193 | ['message' => $e->getMessage()], |
||
194 | Http::STATUS_NOT_IMPLEMENTED |
||
195 | ); |
||
196 | } catch (ProviderCouldNotAddShareException $e) { |
||
197 | return new JSONResponse( |
||
198 | ['message' => $e->getMessage()], |
||
199 | $e->getCode() |
||
200 | ); |
||
201 | } catch (\Exception $e) { |
||
202 | return new JSONResponse( |
||
203 | ['message' => 'Internal error at ' . $this->urlGenerator->getBaseUrl()], |
||
204 | Http::STATUS_BAD_REQUEST |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | $user = $this->userManager->get($shareWith); |
||
209 | $recipientDisplayName = ''; |
||
210 | if($user) { |
||
211 | $recipientDisplayName = $user->getDisplayName(); |
||
212 | } |
||
213 | |||
214 | return new JSONResponse( |
||
215 | ['recipientDisplayName' => $recipientDisplayName], |
||
216 | Http::STATUS_CREATED); |
||
217 | |||
218 | } |
||
219 | |||
301 |