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 |
||
| 9 | class VacancyManager |
||
| 10 | { |
||
| 11 | protected $business; |
||
| 12 | |||
| 13 | protected $builder; |
||
| 14 | |||
| 15 | 8 | public function __construct(Business $business) |
|
| 16 | { |
||
| 17 | 8 | $this->business = $business; |
|
| 18 | 8 | } |
|
| 19 | |||
| 20 | 1 | public function builder() |
|
| 21 | { |
||
| 22 | 1 | if ($this->builder === null) { |
|
| 23 | 1 | $this->builder = new VacancyTemplateBuilder(); |
|
| 24 | 1 | } |
|
| 25 | |||
| 26 | 1 | return $this->builder; |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Update vacancies from batch statements. |
||
| 31 | * |
||
| 32 | * @param Business $business |
||
| 33 | * @param array $parsedStatements |
||
| 34 | * |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | 4 | public function updateBatch(Business $business, $parsedStatements) |
|
| 38 | { |
||
| 39 | 4 | $changed = false; |
|
| 40 | 4 | $dates = $this->arrayGroupBy('date', $parsedStatements); |
|
| 41 | |||
| 42 | 4 | foreach ($dates as $date => $statements) { |
|
| 43 | 4 | $services = $this->arrayGroupBy('service', $statements); |
|
| 44 | |||
| 45 | 4 | $changed |= $this->processServiceStatements($business, $date, $services); |
|
| 46 | 4 | } |
|
| 47 | |||
| 48 | 4 | return $changed; |
|
| 49 | } |
||
| 50 | |||
| 51 | 4 | protected function processServiceStatements($business, $date, $services) |
|
| 52 | { |
||
| 53 | 4 | $changed = false; |
|
| 54 | 4 | foreach ($services as $serviceSlug => $statements) { |
|
| 55 | 4 | $service = $business->services()->where('slug', $serviceSlug)->get()->first(); |
|
| 56 | |||
| 57 | 4 | if ($service === null) { |
|
| 58 | |||
| 59 | // Invalid services are skipped to avoid user frustration. |
||
| 60 | // TODO: Still, a user-level WARNING should be raised with no fatal error |
||
| 61 | |||
| 62 | 2 | continue; |
|
| 63 | } |
||
| 64 | |||
| 65 | 2 | $vacancy = $business->vacancies()->forDate(Carbon::parse($date))->forService($service->id); |
|
| 66 | |||
| 67 | 2 | if ($vacancy) { |
|
| 68 | 2 | $vacancy->delete(); |
|
| 69 | 2 | } |
|
| 70 | |||
| 71 | 2 | $changed |= $this->processStatements($business, $date, $service, $statements); |
|
| 72 | 4 | } |
|
| 73 | |||
| 74 | 4 | return $changed; |
|
| 75 | } |
||
| 76 | |||
| 77 | 2 | protected function processStatements($business, $date, $service, $statements) |
|
| 86 | |||
| 87 | 2 | protected function publishVacancy($business, $date, $service, $statement) |
|
|
|
|||
| 88 | { |
||
| 89 | 2 | $date = $statement['date']; |
|
| 90 | 2 | $startAt = $statement['startAt']; |
|
| 121 | |||
| 122 | 4 | protected function arrayGroupBy($key, $array) |
|
| 134 | |||
| 135 | 1 | public function publish($date, Carbon $startAt, Carbon $finishAt, $serviceId, $capacity = 1) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * [generateAvailability description]. |
||
| 153 | * |
||
| 154 | * @param Illuminate\Database\Eloquent\Collection $vacancies |
||
| 155 | * @param string $startDate |
||
| 156 | * @param int $futureDays |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | 1 | public static function generateAvailability($vacancies, $startDate = 'today', $futureDays = 10) |
|
| 175 | } |
||
| 176 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.