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 |
||
| 13 | trait OpeningHoursScopes |
||
| 14 | { |
||
| 15 | protected $openingHoursRelationName = 'dayOpenTimeRanges'; |
||
| 16 | |||
| 17 | public function scopeWithOpeningHours(Builder $query) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get only open models. |
||
| 24 | * If no time and day are provided current time and day are used. |
||
| 25 | * If only time is provided current day is used. |
||
| 26 | * |
||
| 27 | * @param Builder $query |
||
| 28 | * @param Time|string|null $time defaults to current time |
||
| 29 | * @param string|null $day defaults to current day |
||
| 30 | * @return Builder|static |
||
|
|
|||
| 31 | * @throws InvalidDayName |
||
| 32 | * @throws InvalidTimeString |
||
| 33 | */ |
||
| 34 | 4 | View Code Duplication | public function scopeOpen(Builder $query, $time = null, string $day = null) |
| 58 | |||
| 59 | /** |
||
| 60 | * Get only closed models. |
||
| 61 | * If no time and day are provided current time and day are used. |
||
| 62 | * If only time is provided current day is used. |
||
| 63 | * |
||
| 64 | * @param Builder $query |
||
| 65 | * @param Time|string|null $time defaults to current time |
||
| 66 | * @param string|null $day defaults to current day |
||
| 67 | * @return Builder|static |
||
| 68 | * @throws InvalidDayName |
||
| 69 | * @throws InvalidTimeString |
||
| 70 | */ |
||
| 71 | 4 | View Code Duplication | public function scopeClosed(Builder $query, $time = null, string $day = null) |
| 95 | |||
| 96 | /** |
||
| 97 | * Get all models open at the specified day at random time |
||
| 98 | * |
||
| 99 | * @param Builder $query |
||
| 100 | * @param string $day |
||
| 101 | * @return Builder|static |
||
| 102 | * @throws InvalidDayName |
||
| 103 | */ |
||
| 104 | 1 | View Code Duplication | public function scopeOpenOn(Builder $query, string $day) |
| 114 | |||
| 115 | /** |
||
| 116 | * Get all models closed at the specified day |
||
| 117 | * |
||
| 118 | * @param Builder $query |
||
| 119 | * @param string $day |
||
| 120 | * @return Builder|static |
||
| 121 | * @throws InvalidDayName |
||
| 122 | */ |
||
| 123 | 1 | View Code Duplication | public function scopeClosedOn(Builder $query, string $day) |
| 133 | |||
| 134 | /** |
||
| 135 | * Get all models open at the specified DateTime |
||
| 136 | * |
||
| 137 | * @param Builder $query |
||
| 138 | * @param DateTimeInterface $date |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | 1 | public function scopeOpenAt(Builder $query, DateTimeInterface $date) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Get all models closed at the specified DateTime |
||
| 148 | * |
||
| 149 | * @param Builder $query |
||
| 150 | * @param DateTimeInterface $date |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | 1 | public function scopeClosedAt(Builder $query, DateTimeInterface $date) |
|
| 157 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.