Conditions | 26 |
Paths | 1212 |
Total Lines | 165 |
Code Lines | 111 |
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:
1 | <?php |
||
233 | public function getDocumentForm() |
||
234 | { |
||
235 | $fields = $this->getFields(); |
||
236 | |||
237 | $documentForm = new \EWW\Dpf\Domain\Model\DocumentForm(); |
||
238 | $documentForm->setCsrfToken($this->formData['csrfToken']); |
||
239 | $documentForm->setUid($this->documentType->getUid()); |
||
240 | $documentForm->setDisplayName($this->documentType->getDisplayName()); |
||
241 | $documentForm->setName($this->documentType->getName()); |
||
242 | $documentForm->setDocumentUid($this->formData['documentUid']); |
||
243 | $documentForm->setFedoraPid($this->formData['fedoraPid']); |
||
244 | $documentForm->setValid(!empty($this->formData['validDocument'])); |
||
245 | if ($this->formData['comment']) { |
||
246 | $documentForm->setComment($this->formData['comment']); |
||
247 | } |
||
248 | |||
249 | $documentData = array(); |
||
250 | |||
251 | foreach ($fields as $field) { |
||
252 | $pageUid = $field->getPageUid(); |
||
253 | $groupUid = $field->getGroupUid(); |
||
254 | $groupIndex = $field->getGroupIndex(); |
||
255 | $fieldUid = $field->getFieldUid(); |
||
256 | $fieldIndex = $field->getFieldIndex(); |
||
257 | $value = $field->getValue(); |
||
258 | |||
259 | $documentData[$pageUid][$groupUid][$groupIndex][$fieldUid][$fieldIndex] = $value; |
||
260 | } |
||
261 | |||
262 | foreach ($documentData as $pageUid => $page) { |
||
263 | $metadataPage = $this->metadataPageRepository->findByUid($pageUid); |
||
264 | $documentFormPage = new \EWW\Dpf\Domain\Model\DocumentFormPage(); |
||
265 | $documentFormPage->setUid($metadataPage->getUid()); |
||
266 | $documentFormPage->setDisplayName($metadataPage->getDisplayName()); |
||
267 | $documentFormPage->setName($metadataPage->getName()); |
||
268 | |||
269 | $documentFormPage->setAccessRestrictionRoles($metadataPage->getAccessRestrictionRoles()); |
||
270 | |||
271 | foreach ($page as $groupUid => $groupItem) { |
||
272 | foreach ($groupItem as $group) { |
||
273 | $metadataGroup = $this->metadataGroupRepository->findByUid($groupUid); |
||
274 | $documentFormGroup = new \EWW\Dpf\Domain\Model\DocumentFormGroup(); |
||
275 | $documentFormGroup->setUid($metadataGroup->getUid()); |
||
276 | $documentFormGroup->setDisplayName($metadataGroup->getDisplayName()); |
||
277 | $documentFormGroup->setName($metadataGroup->getName()); |
||
278 | $documentFormGroup->setMandatory($metadataGroup->getMandatory()); |
||
279 | |||
280 | $documentFormGroup->setAccessRestrictionRoles($metadataGroup->getAccessRestrictionRoles()); |
||
281 | |||
282 | $documentFormGroup->setMaxIteration($metadataGroup->getMaxIteration()); |
||
283 | |||
284 | $fileLabel = ""; |
||
285 | $fileDownload = ""; |
||
286 | $fileArchive = ""; |
||
287 | |||
288 | $fileIdentifier = ''; |
||
289 | if (array_key_exists('fileIdentifier', $group)) { |
||
290 | $fileIdentifier = array_shift($group['fileIdentifier']); |
||
291 | unset($group['fileIdentifier']); |
||
292 | } |
||
293 | |||
294 | foreach ($group as $objectUid => $objectItem) { |
||
295 | |||
296 | foreach ($objectItem as $objectItem => $object) { |
||
297 | /** @var MetadataObject $metadataObject */ |
||
298 | $metadataObject = $this->metadataObjectRepository->findByUid($objectUid); |
||
299 | |||
300 | /** @var DocumentForm $documentFormField */ |
||
301 | $documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField(); |
||
302 | $documentFormField->setUid($metadataObject->getUid()); |
||
303 | $documentFormField->setDisplayName($metadataObject->getDisplayName()); |
||
304 | $documentFormField->setName($metadataObject->getName()); |
||
305 | $documentFormField->setMandatory($metadataObject->getMandatory()); |
||
306 | |||
307 | $documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles()); |
||
308 | |||
309 | $documentFormField->setConsent($metadataObject->getConsent()); |
||
310 | $documentFormField->setValidation($metadataObject->getValidation()); |
||
311 | $documentFormField->setDataType($metadataObject->getDataType()); |
||
312 | $documentFormField->setMaxIteration($metadataObject->getMaxIteration()); |
||
313 | $documentFormField->setInputOptions($metadataObject->getInputOptionList()); |
||
314 | $documentFormField->setInputField($metadataObject->getInputField()); |
||
315 | $documentFormField->setFillOutService($metadataObject->getFillOutService()); |
||
316 | $documentFormField->setGndFieldUid($metadataObject->getGndFieldUid()); |
||
317 | $documentFormField->setMaxInputLength($metadataObject->getMaxInputLength()); |
||
318 | $documentFormField->setValue($object, $metadataObject->getDefaultValue()); |
||
319 | |||
320 | $documentFormGroup->addItem($documentFormField); |
||
321 | |||
322 | if ($metadataGroup->isFileGroup()) { |
||
323 | |||
324 | // Use the existing file entry |
||
325 | $file = null; |
||
326 | $document = $this->documentRepository->findByUid($this->formData['documentUid']); |
||
327 | if ($document) { |
||
328 | if ($metadataGroup->isPrimaryFileGroup()) { |
||
329 | $file = $document->getPrimaryFile(); |
||
330 | } else { |
||
331 | $file = $document->getFileByFileIdentifier($fileIdentifier); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | if ($metadataObject->isUploadField() ) { |
||
336 | if ($object && is_array($object) && |
||
337 | array_key_exists('error', $object) && |
||
338 | $object['error'] != UPLOAD_ERR_NO_FILE) |
||
339 | { |
||
340 | if (empty($file)) { |
||
341 | $file = $this->objectManager->get(File::class); |
||
342 | $file = $this->getUploadedFile( |
||
343 | $object, |
||
344 | $metadataGroup->isPrimaryFileGroup(), |
||
345 | $file); |
||
346 | $documentFormField->setFile($file); |
||
347 | } else { |
||
348 | $file = $this->getUploadedFile( |
||
349 | $object, |
||
350 | $metadataGroup->isPrimaryFileGroup(), |
||
351 | $file); |
||
352 | $documentFormField->setFile($file); |
||
353 | } |
||
354 | |||
355 | $documentFormField->setValue($file->getUrl()); |
||
356 | $fileIdentifier = $file->getFileIdentifier(); |
||
357 | $documentForm->addFile($file); |
||
358 | |||
359 | } elseif ($object && !is_array($object)) { |
||
360 | $documentFormField->setFile($file); |
||
361 | $fileIdentifier = $file->getFileIdentifier(); |
||
362 | $documentForm->addFile($file); |
||
363 | } |
||
364 | } else { |
||
365 | if ($metadataObject->isFileLabelField()) { |
||
366 | $fileLabel = $object; |
||
367 | } |
||
368 | |||
369 | if ($metadataObject->isFileDownloadField()) { |
||
370 | $fileDownload = !empty($object); |
||
371 | } |
||
372 | |||
373 | if ($metadataObject->isFileArchiveField()) { |
||
374 | $fileArchive = !empty($object); |
||
375 | } |
||
376 | |||
377 | } |
||
378 | |||
379 | if ($file) { |
||
380 | $file->setLabel($fileLabel); |
||
381 | $file->setDownload($fileDownload); |
||
382 | $file->setArchive($fileArchive); |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | } |
||
387 | |||
388 | if (!$metadataGroup->isFileGroup() || $fileIdentifier) { |
||
389 | $documentFormPage->addItem($documentFormGroup); |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | |||
394 | $documentForm->addItem($documentFormPage); |
||
395 | } |
||
396 | |||
397 | return $documentForm; |
||
398 | } |
||
400 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.