Experience::toArray()   C
last analyzed

Complexity

Conditions 15
Paths 19

Size

Total Lines 52
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 40
c 2
b 0
f 0
dl 0
loc 52
rs 5.9166
cc 15
nc 19
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Resources;
4
5
use Illuminate\Http\Resources\Json\JsonResource;
6
use Jenssegers\Date\Date;
7
8
class Experience extends JsonResource
9
{
10
    /**
11
     * Transform the resource into an array.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @return array
15
     */
16
    public function toArray($request)
17
    {
18
        $type = $this->experienceTypeName();
19
        $translations = [];
20
        $dates = [];
21
        switch ($type) {
22
            case 'experience_work':
23
                $dates = [
24
                    'start_date' => $this->start_date ? $this->start_date->format('Y-m-d') : null,
25
                    'end_date' => $this->end_date ? $this->end_date->format('Y-m-d') : null,
26
                ];
27
                break;
28
            case 'experience_award':
29
                $dates = [
30
                    'awarded_date' => $this->awarded_date ? $this->awarded_date->format('Y-m-d') : null,
31
                ];
32
                $translations = [
33
                    'award_recipient_type' => $this->award_recipient_type->getTranslations('name'),
34
                    'award_recognition_type' => $this->award_recognition_type->getTranslations('name'),
35
                ];
36
                break;
37
            case 'experience_community':
38
                $dates = [
39
                    'start_date' => $this->start_date ? $this->start_date->format('Y-m-d') : null,
40
                    'end_date' => $this->end_date ? $this->end_date->format('Y-m-d') : null,
41
                ];
42
                break;
43
            case 'experience_education':
44
                $dates = [
45
                    'start_date' => $this->start_date ? $this->start_date->format('Y-m-d') : null,
46
                    'end_date' => $this->end_date ? $this->end_date->format('Y-m-d') : null,
47
                ];
48
                $translations = [
49
                    'education_status' => $this->education_status->getTranslations('name'),
50
                    'education_type' => $this->education_type->getTranslations('name'),
51
                ];
52
                break;
53
            case 'experience_personal':
54
                $dates = [
55
                    'start_date' => $this->start_date ? $this->start_date->format('Y-m-d') : null,
56
                    'end_date' => $this->end_date ? $this->end_date->format('Y-m-d') : null,
57
                ];
58
                break;
59
        }
60
        return array_merge(
61
            parent::toArray($request),
62
            $translations,
63
            [
64
                'type' => $type,
65
                'experience_skills' => JsonResource::collection($this->whenLoaded('experience_skills')),
66
            ],
67
            $dates
68
        );
69
    }
70
}
71