| Conditions | 3 |
| Paths | 1 |
| Total Lines | 60 |
| Code Lines | 37 |
| 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 |
||
| 26 | public static function boot(): void |
||
| 27 | { |
||
| 28 | App::$Event->on(Main::SEARCH_EVENT_NAME, function ($model) { |
||
| 29 | /** @var CollectionSearchResults $model */ |
||
| 30 | $limit = $model->getLimit(); |
||
| 31 | $query = $model->getQuery(); |
||
| 32 | |||
| 33 | // relevant search by string query |
||
| 34 | $records = Content::search($query) |
||
| 35 | ->take($limit) |
||
| 36 | ->get(); |
||
| 37 | |||
| 38 | /** @var Content[]|Collection $records */ |
||
| 39 | $records->each(function($item) use ($model) { |
||
| 40 | /** @var Content $item */ |
||
| 41 | $title = $item->getLocaled('title'); |
||
| 42 | $text = App::$Security->strip_tags($item->getLocaled('text')); |
||
| 43 | $snippet = Text::snippet($text); |
||
|
|
|||
| 44 | // prevent empty items |
||
| 45 | if (Str::likeEmpty($title)) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | // initialize abstract response pattern |
||
| 50 | $res = new AbstractSearchItem(); |
||
| 51 | $res->setTitle($title); |
||
| 52 | $res->setSnippet($snippet); |
||
| 53 | $res->setDate($item->created_at); |
||
| 54 | $res->setRelevance((int)$item->relevance); |
||
| 55 | $res->setUrl('content/update', [$item->id]); |
||
| 56 | $res->setMarker('Content'); |
||
| 57 | |||
| 58 | $model->add($res); |
||
| 59 | }); |
||
| 60 | // search in categories |
||
| 61 | $records = ContentCategory::search($query) |
||
| 62 | ->take($limit) |
||
| 63 | ->get(); |
||
| 64 | |||
| 65 | /** @var ContentCategory[]|Collection $records */ |
||
| 66 | $records->each(function($item) use ($model) { |
||
| 67 | /** @var ContentCategory $item */ |
||
| 68 | $title = $item->getLocaled('title'); |
||
| 69 | $text = App::$Security->strip_tags($item->getLocaled('description')); |
||
| 70 | $snippet = Text::snippet($text); |
||
| 71 | // prevent empty items |
||
| 72 | if (Str::likeEmpty($title)) { |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | // initialize abstract response pattern |
||
| 77 | $res = new AbstractSearchItem(); |
||
| 78 | $res->setTitle($title); |
||
| 79 | $res->setSnippet($snippet); |
||
| 80 | $res->setDate($item->created_at); |
||
| 81 | $res->setRelevance((int)$item->relevance); |
||
| 82 | $res->setUrl('content/categoryupdate', [$item->id]); |
||
| 83 | $res->setMarker('Content'); |
||
| 84 | |||
| 85 | $model->add($res); |
||
| 86 | }); |
||
| 89 | } |