PickupPoint::__construct()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 119
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 58
nc 3
nop 35
dl 0
loc 119
rs 8.9163
c 1
b 0
f 1

How to fix   Long Method    Many Parameters   

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:

Many Parameters

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
2
3
namespace DansMaCulotte\Colissimo\Resources;
4
5
use Spatie\OpeningHours\OpeningHours;
6
7
class PickupPoint
8
{
9
    public $data;
10
11
    /**
12
     * PickupPoint constructor.
13
     * @param string $accesPersonneMobiliteReduite
14
     * @param string $adresse1
15
     * @param string $adresse2
16
     * @param string $adresse3
17
     * @param string $codePostal
18
     * @param string $congesPartiel
19
     * @param string $congesTotal
20
     * @param string $coordGeolocalisationLatitude
21
     * @param string $coordGeolocalisationLongitude
22
     * @param string $distanceEnMetre
23
     * @param string $horairesOuvertureLundi
24
     * @param string $horairesOuvertureMardi
25
     * @param string $horairesOuvertureMercredi
26
     * @param string $horairesOuvertureJeudi
27
     * @param string $horairesOuvertureVendredi
28
     * @param string $horairesOuvertureSamedi
29
     * @param string $horairesOuvertureDimanche
30
     * @param string $identifiant
31
     * @param string $indiceDeLocalisation
32
     * @param mixed $listeConges
33
     * @param string $localite
34
     * @param string $nom
35
     * @param string $periodeActiviteHoraireDeb
36
     * @param string $periodeActiviteHoraireFin
37
     * @param string $poidsMaxi
38
     * @param string $typeDePoint
39
     * @param string $codePays
40
     * @param string $langue
41
     * @param string $libellePays
42
     * @param string $loanOfHandlingTool
43
     * @param string $parking
44
     * @param string $reseau
45
     * @param string $distributionSort
46
     * @param string $lotAcheminement
47
     * @param string $versionPlanTri
48
     * @param string $calendarDeDebut
49
     * @param string $calendarDeFin
50
     * @param string $numero
51
     */
52
    public function __construct(
53
        string $accesPersonneMobiliteReduite,
54
        string $adresse1,
55
        string $adresse2,
56
        string $adresse3,
57
        string $codePostal,
58
        string $congesPartiel,
59
        string $congesTotal,
60
        string $coordGeolocalisationLatitude,
61
        string $coordGeolocalisationLongitude,
62
        string $distanceEnMetre,
63
        string $horairesOuvertureLundi,
64
        string $horairesOuvertureMardi,
65
        string $horairesOuvertureMercredi,
66
        string $horairesOuvertureJeudi,
67
        string $horairesOuvertureVendredi,
68
        string $horairesOuvertureSamedi,
69
        string $horairesOuvertureDimanche,
70
        string $identifiant,
71
        string $indiceDeLocalisation,
72
        object $listeConges,
73
        string $localite,
74
        string $nom,
75
        string $periodeActiviteHoraireDeb,
76
        string $periodeActiviteHoraireFin,
77
        string $poidsMaxi,
78
        string $typeDePoint,
79
        string $codePays,
80
        string $langue,
81
        string $libellePays,
82
        string $loanOfHandlingTool,
83
        string $parking,
84
        string $reseau,
85
        string $distributionSort,
86
        string $lotAcheminement,
87
        string $versionPlanTri
88
    ) {
89
        $this->data['id'] = $identifiant;
90
        $this->data['name'] = $nom;
91
        $this->data['disabledPersonAccess'] = $accesPersonneMobiliteReduite;
92
        $this->data['streetName'] = $adresse1;
93
        $this->data['premise'] = $adresse2;
94
        $this->data['locality'] = $adresse3;
95
        $this->data['city'] = $localite;
96
        $this->data['postalCode'] = $codePostal;
97
        $this->data['countryCode'] = $codePays;
98
        $this->data['partialClosed'] = $congesPartiel;
99
        $this->data['closed'] = $congesTotal;
100
        $this->data['latGeoCoord'] = $coordGeolocalisationLatitude;
101
        $this->data['longGeoCoord'] = $coordGeolocalisationLongitude;
102
        $this->data['range'] = $distanceEnMetre;
103
        $this->data['locationHelp'] = $indiceDeLocalisation;
104
105
        $this->data['openingsDateStart'] = $periodeActiviteHoraireDeb;
106
        $this->data['openingsDateEnd'] = $periodeActiviteHoraireFin;
107
        
108
        $this->data['openings'] = OpeningHours::create(
109
            [
110
                'monday' => $this->_formatRangeTime(
111
                    $horairesOuvertureLundi
112
                ),
113
                'tuesday' => $this->_formatRangeTime(
114
                    $horairesOuvertureMardi
115
                ),
116
                'wednesday' => $this->_formatRangeTime(
117
                    $horairesOuvertureMercredi
118
                ),
119
                'thursday' => $this->_formatRangeTime(
120
                    $horairesOuvertureJeudi
121
                ),
122
                'friday' => $this->_formatRangeTime(
123
                    $horairesOuvertureVendredi
124
                ),
125
                'saturday' => $this->_formatRangeTime(
126
                    $horairesOuvertureSamedi
127
                ),
128
                'sunday' => $this->_formatRangeTime(
129
                    $horairesOuvertureDimanche
130
                ),
131
            ]
132
        );
133
134
        if (isset($listeConges)) {
135
            $holidays = [];
136
            if (is_object($listeConges)) {
137
                array_push(
138
                    $holidays,
139
                    [
140
                        'start' => $listeConges->calendarDeDebut,
141
                        'end' => $listeConges->calendarDeFin,
142
                        'number' => $listeConges->numero,
143
                    ]
144
                );
145
            } else {
146
                foreach ($listeConges as $conges) {
147
                    array_push(
148
                        $holidays,
149
                        [
150
                            'start' => $conges->calendarDeDebut,
151
                            'end' => $conges->calendarDeFin,
152
                            'number' => $conges->numero,
153
                        ]
154
                    );
155
                }
156
            }
157
158
            $this->data['holidays'] = $holidays;
159
        }
160
161
        $this->data['maxWeight'] = $poidsMaxi;
162
        $this->data['pointType'] = $typeDePoint;
163
        $this->data['language'] = $langue;
164
        $this->data['countryLabel'] = $libellePays;
165
        $this->data['handlingTool'] = $loanOfHandlingTool;
166
        $this->data['parkingArea'] = $parking;
167
        $this->data['linkCode'] = $reseau;
168
        $this->data['distributionSort'] = $distributionSort;
169
        $this->data['pickupParcel'] = $lotAcheminement;
170
        $this->data['sortPlanVersion'] = $versionPlanTri;
171
    }
172
173
    /**
174
     * Split Range datetime in two datetimes
175
     *
176
     * @param string $hours Range datetime e.g. 09:45-12:30 14:00-18:30
177
     *
178
     * @return array
179
     */
180
    private function _formatRangeTime($hours)
181
    {
182
        $partialOpenings = explode(' ', $hours);
183
184
        if (count($partialOpenings) != 2) {
185
            return [];
186
        }
187
188
        $validOpenings = array_filter(
189
            $partialOpenings,
190
            function ($partial) {
191
                return $partial != '00:00-00:00';
192
            }
193
        );
194
195
        if (count($validOpenings) == 2 && $validOpenings[0] == $validOpenings[1]) {
196
            return [
197
                $validOpenings[0]
198
            ];
199
        }
200
201
        return $validOpenings;
202
    }
203
}
204