Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
10 | class VacancyManager |
||
11 | { |
||
12 | protected $business; |
||
13 | |||
14 | 4 | public function __construct(Business $business) |
|
18 | |||
19 | /** |
||
20 | * Update vacancies from batch statements. |
||
21 | * |
||
22 | * @param Business $business |
||
23 | * @param array $parsedStatements |
||
24 | * |
||
25 | * @return bool |
||
26 | */ |
||
27 | 2 | public function updateBatch(Business $business, $parsedStatements) |
|
28 | { |
||
29 | 2 | $changed = false; |
|
30 | 2 | $dates = $this->arrayGroupBy('date', $parsedStatements); |
|
31 | |||
32 | 2 | foreach ($dates as $date => $statements) { |
|
33 | 2 | $services = $this->arrayGroupBy('service', $statements); |
|
34 | |||
35 | 2 | $changed |= $this->processServiceStatements($business, $date, $services); |
|
36 | 2 | } |
|
37 | |||
38 | 2 | return $changed; |
|
39 | } |
||
40 | |||
41 | 2 | protected function processServiceStatements($business, $date, $services) |
|
42 | { |
||
43 | 2 | $changed = false; |
|
44 | 2 | foreach ($services as $serviceSlug => $statements) { |
|
45 | 2 | $service = Service::where('slug', $serviceSlug)->get()->first(); |
|
46 | |||
47 | 2 | if ($service === null) { |
|
48 | |||
49 | // Invalid services are skipped to avoid user frustration. |
||
50 | // TODO: Still, a user-level WARNING should be raised with no fatal error |
||
51 | |||
52 | 1 | continue; |
|
53 | } |
||
54 | |||
55 | 1 | $vacancy = $business->vacancies()->forDate(Carbon::parse($date))->forService($service->id); |
|
56 | |||
57 | 1 | if ($vacancy) { |
|
58 | 1 | $vacancy->delete(); |
|
59 | 1 | } |
|
60 | |||
61 | 1 | $changed |= $this->processStatements($business, $date, $service, $statements); |
|
62 | 2 | } |
|
63 | |||
64 | 2 | return $changed; |
|
65 | } |
||
66 | |||
67 | 1 | protected function processStatements($business, $date, $service, $statements) |
|
68 | { |
||
69 | 1 | $changed = false; |
|
70 | 1 | foreach ($statements as $statement) { |
|
71 | 1 | $changed |= $this->publishVacancy($business, $date, $service, $statement); |
|
72 | 1 | } |
|
73 | |||
74 | 1 | return $changed; |
|
75 | } |
||
76 | |||
77 | 1 | protected function publishVacancy($business, $date, $service, $statement) |
|
|
|||
78 | { |
||
79 | 1 | $date = $statement['date']; |
|
80 | 1 | $startAt = $statement['startAt']; |
|
81 | 1 | $finishAt = $statement['finishAt']; |
|
82 | |||
83 | 1 | $startAt = Carbon::parse("{$date} {$startAt} {$business->timezone}")->timezone('UTC'); |
|
84 | 1 | $finishAt = Carbon::parse("{$date} {$finishAt} {$business->timezone}")->timezone('UTC'); |
|
85 | |||
86 | $vacancyValues = [ |
||
87 | 1 | 'business_id' => $business->id, |
|
88 | 1 | 'service_id' => $service->id, |
|
89 | 1 | 'date' => $statement['date'], |
|
90 | 1 | 'capacity' => intval($statement['capacity']), |
|
91 | 1 | 'start_at' => $startAt, |
|
92 | 1 | 'finish_at' => $finishAt, |
|
93 | 1 | ]; |
|
94 | |||
95 | 1 | $vacancy = Vacancy::create($vacancyValues); |
|
96 | |||
97 | 1 | return $vacancy !== null; |
|
98 | } |
||
99 | |||
100 | 2 | protected function arrayGroupBy($key, $array) |
|
101 | { |
||
102 | 2 | $grouped = []; |
|
103 | 2 | foreach ($array as $hash => $item) { |
|
104 | 2 | if (!array_key_exists($item[$key], $grouped)) { |
|
105 | 2 | $grouped[$item[$key]] = []; |
|
106 | 2 | } |
|
107 | 2 | $grouped[$item[$key]][] = $item; |
|
108 | 2 | } |
|
109 | |||
110 | 2 | return $grouped; |
|
111 | } |
||
112 | |||
113 | 1 | public function publish($date, Carbon $startAt, Carbon $finishAt, $serviceId, $capacity = 1) |
|
128 | |||
129 | /** |
||
130 | * [generateAvailability description]. |
||
131 | * |
||
132 | * @param Illuminate\Database\Eloquent\Collection $vacancies |
||
133 | * @param string $startDate |
||
134 | * @param int $futureDays |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | 1 | public static function generateAvailability($vacancies, $startDate = 'today', $futureDays = 10) |
|
153 | } |
||
154 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.