Conditions | 16 |
Paths | 2 |
Total Lines | 63 |
Code Lines | 41 |
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 |
||
34 | public function search($admin = null) |
||
35 | { |
||
36 | if ($this->searchable !== true) { |
||
37 | return []; |
||
|
|||
38 | } |
||
39 | |||
40 | return \Cache::rememberForever('search'.$this->name.$admin, function () use ($admin) { |
||
41 | $data = []; |
||
42 | |||
43 | $model = new $this->model; |
||
44 | |||
45 | $search_rows = ['id', $this->search_title]; |
||
46 | |||
47 | if (isset($this->rows['category']) && !$this->rows['category'] instanceof FormTags) { |
||
48 | $search_rows[] = 'category'; |
||
49 | } |
||
50 | |||
51 | if ($admin) { |
||
52 | if (isset($this->rows['category'])) { |
||
53 | $model = $model::with(['getCategory']); |
||
54 | } |
||
55 | } else { |
||
56 | if (isset($this->rows['category'])) { |
||
57 | $model = $model::with(['getCategoryActive']); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | $items = $model->get($search_rows); |
||
62 | |||
63 | foreach ($items as $item) { |
||
64 | $data[$item->id]['id'] = $item->id; |
||
65 | $data[$item->id]['title'] = $item->{$this->search_title}; |
||
66 | $data[$item->id]['full_url'] = $item->full_url; |
||
67 | $data[$item->id]['component'] = $this->name; |
||
68 | $data[$item->id]['category'] = null; |
||
69 | $data[$item->id]['admin_url'] = $item->admin_url; |
||
70 | if ($admin) { |
||
71 | if ($item->getCategory) { |
||
72 | if (\count($item->getCategory) > 0) { |
||
73 | $data[$item->id]['category'] = $item->getCategory->first()->title; |
||
74 | } elseif (isset($item->getCategory->title)) { |
||
75 | $data[$item->id]['category'] = $item->getCategory->title; |
||
76 | } else { |
||
77 | unset($data[$item->id]); |
||
78 | } |
||
79 | } |
||
80 | } else { |
||
81 | if ($item->getCategoryActive) { |
||
82 | if (\count($item->getCategoryActive) > 0) { |
||
83 | $data[$item->id]['category'] = $item->getCategoryActive->first()->title; |
||
84 | } elseif (isset($item->getCategoryActive->title)) { |
||
85 | $data[$item->id]['category'] = $item->getCategoryActive->title; |
||
86 | } else { |
||
87 | unset($data[$item->id]); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | if (\count($data) === 0) { |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | return $data; |
||
97 | }); |
||
99 | } |