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 | public function scopeOpen(Builder $query, $time = null, string $day = null) |
|
35 | { |
||
36 | 4 | $now = Carbon::now(); |
|
37 | 4 | if ($time == null) { |
|
38 | 1 | $time = Time::fromDateTime($now); |
|
39 | 3 | } elseif (is_string($time)) { |
|
40 | 2 | $time = Time::fromString($time); |
|
41 | } |
||
42 | |||
43 | 4 | if ($day == null) { |
|
44 | 2 | $day = Day::onDateTime($now); |
|
45 | } else { |
||
46 | 2 | if (! Day::isValid($day)) { |
|
47 | throw new InvalidDayName(); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | 4 | return $query->whereHas($this->openingHoursRelationName, function (Builder $subquery) use ($day, $time) { |
|
52 | $subquery |
||
53 | 4 | ->where('day', $day) |
|
54 | 4 | ->where('start', '<=', $time) |
|
55 | 4 | ->where('end', '>=', $time); |
|
56 | 4 | }); |
|
57 | } |
||
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 | public function scopeClosed(Builder $query, $time = null, string $day = null) |
|
72 | { |
||
73 | 4 | $now = Carbon::now(); |
|
74 | 4 | if ($time == null) { |
|
75 | 1 | $time = Time::fromDateTime($now); |
|
76 | 3 | } elseif (is_string($time)) { |
|
77 | 2 | $time = Time::fromString($time); |
|
78 | } |
||
79 | |||
80 | 4 | if ($day == null) { |
|
81 | 2 | $day = Day::onDateTime($now); |
|
82 | } else { |
||
83 | 2 | if (! Day::isValid($day)) { |
|
84 | throw new InvalidDayName(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 4 | return $query->whereDoesntHave($this->openingHoursRelationName, function (Builder $subquery) use ($day, $time) { |
|
89 | $subquery |
||
90 | 4 | ->where('day', $day) |
|
91 | 4 | ->where('start', '<=', $time) |
|
92 | 4 | ->where('end', '>=', $time); |
|
93 | 4 | }); |
|
94 | } |
||
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 | } |
||
158 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.