Conditions | 10 |
Paths | 4 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
143 | public static function getEvents($filters, $itemPerPage) |
||
144 | { |
||
145 | if (! $filters['startDate']) { |
||
146 | $filters['startDate'] = Carbon::now()->format('Y-m-d'); |
||
|
|||
147 | } |
||
148 | |||
149 | // Sub-Query Joins - https://laravel.com/docs/5.7/queries |
||
150 | $lastestEventsRepetitionsQuery = EventRepetition::getLastestEventsRepetitionsQuery($filters['startDate'], $filters['endDate']); |
||
151 | |||
152 | // Retrieve the events that correspond to the selected filters |
||
153 | if ($filters['keywords'] || $filters['category'] || $filters['city'] || $filters['country'] || $filters['continent'] || $filters['teacher'] || $filters['venue'] || $filters['endDate']) { |
||
154 | |||
155 | //DB::enableQueryLog(); |
||
156 | $ret = self:: |
||
157 | when($filters['keywords'], function ($query, $keywords) { |
||
158 | return $query->where('title', 'like', '%'.$keywords.'%'); |
||
159 | }) |
||
160 | ->when($filters['category'], function ($query, $category) { |
||
161 | return $query->where('category_id', '=', $category); |
||
162 | }) |
||
163 | ->when($filters['teacher'], function ($query, $teacher) { |
||
164 | return $query->whereRaw('json_contains(sc_teachers_id, \'["'.$teacher.'"]\')'); |
||
165 | }) |
||
166 | ->when($filters['country'], function ($query, $country) { |
||
167 | return $query->where('sc_country_id', '=', $country); |
||
168 | }) |
||
169 | ->when($filters['continent'], function ($query, $continent) { |
||
170 | return $query->where('sc_continent_id', '=', $continent); |
||
171 | }) |
||
172 | ->when($filters['city'], function ($query, $city) { |
||
173 | return $query->where('sc_city_name', 'like', '%'.$city.'%'); |
||
174 | }) |
||
175 | ->when($filters['venue'], function ($query, $venue) { |
||
176 | return $query->where('sc_venue_name', 'like', '%'.$venue.'%'); |
||
177 | }) |
||
178 | ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
||
179 | $join->on('events.id', '=', 'event_repetitions.event_id'); |
||
180 | }) |
||
181 | ->orderBy('event_repetitions.start_repeat', 'asc') |
||
182 | ->paginate($itemPerPage); |
||
183 | //dd(DB::getQueryLog()); |
||
184 | } |
||
185 | // If no filter selected retrieve all the events |
||
186 | else { |
||
187 | $ret = self:: |
||
188 | joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) { |
||
189 | $join->on('events.id', '=', 'event_repetitions.event_id'); |
||
190 | }) |
||
191 | ->orderBy('event_repetitions.start_repeat', 'asc') |
||
192 | ->paginate($itemPerPage); |
||
193 | |||
194 | // It works, but I don't use it now to develop |
||
195 | /*$cacheExpireMinutes = 15; |
||
196 | $events = Cache::remember('all_events', $cacheExpireTime, function () { |
||
197 | return DB::table('events')->latest()->paginate(20); |
||
198 | });*/ |
||
199 | } |
||
200 | |||
201 | return $ret; |
||
202 | } |
||
203 | |||
247 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: