Conditions | 12 |
Paths | 40 |
Total Lines | 89 |
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 |
||
153 | public function createShare($shareWith, |
||
154 | $name, |
||
155 | $description, |
||
|
|||
156 | $providerId, |
||
157 | $owner, |
||
158 | $ownerDisplayName, |
||
159 | $sender, |
||
160 | $senderDisplayName, |
||
161 | $shareType, |
||
162 | $resourceType, |
||
163 | $protocol |
||
164 | |||
165 | ) { |
||
166 | try { |
||
167 | $hasMissingParams = $this->hasNull( |
||
168 | [$shareWith, $name, $providerId, $owner, $shareType, $resourceType] |
||
169 | ); |
||
170 | if ($hasMissingParams |
||
171 | || !is_array($protocol) |
||
172 | || !isset($protocol['name']) |
||
173 | || !isset($protocol['options']) |
||
174 | || !is_array($protocol['options']) |
||
175 | || !isset($protocol['options']['sharedSecret']) |
||
176 | ) { |
||
177 | throw new InvalidShareException( |
||
178 | 'server can not add remote share, missing parameter' |
||
179 | ); |
||
180 | } |
||
181 | if (!\OCP\Util::isValidFileName($name)) { |
||
182 | throw new InvalidShareException( |
||
183 | 'The mountpoint name contains invalid characters.' |
||
184 | ); |
||
185 | } |
||
186 | // FIXME this should be a method in the user management instead |
||
187 | $this->logger->debug( |
||
188 | "shareWith before, $shareWith", |
||
189 | ['app' => $this->appName] |
||
190 | ); |
||
191 | \OCP\Util::emitHook( |
||
192 | '\OCA\Files_Sharing\API\Server2Server', |
||
193 | 'preLoginNameUsedAsUserName', |
||
194 | ['uid' => &$shareWith] |
||
195 | ); |
||
196 | $this->logger->debug( |
||
197 | "shareWith after, $shareWith", |
||
198 | ['app' => $this->appName] |
||
199 | ); |
||
200 | |||
201 | if (!$this->userManager->userExists($shareWith)) { |
||
202 | throw new InvalidShareException('User does not exist'); |
||
203 | } |
||
204 | |||
205 | $ownerAddress = new Address($owner, $ownerDisplayName); |
||
206 | $sharedByAddress = new Address($sender, $senderDisplayName); |
||
207 | |||
208 | $this->fedShareManager->createShare( |
||
209 | $ownerAddress, |
||
210 | $sharedByAddress, |
||
211 | $shareWith, |
||
212 | $providerId, |
||
213 | $name, |
||
214 | $protocol['options']['sharedSecret'] |
||
215 | ); |
||
216 | } catch (InvalidShareException $e) { |
||
217 | return new JSONResponse( |
||
218 | null, |
||
219 | Http::STATUS_BAD_REQUEST, |
||
220 | $e->getMessage() |
||
221 | ); |
||
222 | } catch (NotSupportedException $e) { |
||
223 | return new JSONResponse( |
||
224 | null, |
||
225 | Http::STATUS_SERVICE_UNAVAILABLE, |
||
226 | 'Server does not support federated cloud sharing' |
||
227 | ); |
||
228 | } catch (\Exception $e) { |
||
229 | \OCP\Util::writeLog( |
||
230 | 'files_sharing', |
||
231 | 'server can not add remote share, ' . $e->getMessage(), |
||
232 | \OCP\Util::ERROR |
||
233 | ); |
||
234 | return new JSONResponse( |
||
235 | null, |
||
236 | Http::STATUS_INTERNAL_SERVER_ERROR, |
||
237 | 'internal server error, was not able to add share from ' . $remote |
||
238 | ); |
||
239 | } |
||
240 | return new JSONResponse(); |
||
241 | } |
||
242 | |||
282 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.