Conditions | 22 |
Paths | 2 |
Total Lines | 75 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 | if ($this->name !== 'feed' && $this->name !== 'catalog') { |
||
44 | return []; |
||
45 | } |
||
46 | |||
47 | $model = new $this->model; |
||
48 | |||
49 | $search_rows = ['id', $this->search_title]; |
||
50 | |||
51 | if (isset($this->rows['url'])) { |
||
52 | $search_rows[] = 'url'; |
||
53 | } |
||
54 | |||
55 | if (isset($this->rows['category']) && ! $this->rows['category'] instanceof FormTags) { |
||
56 | $search_rows[] = 'category'; |
||
57 | } |
||
58 | |||
59 | if ($admin) { |
||
60 | if (isset($this->rows['category'])) { |
||
61 | $model = $model::with(['getCategory']); |
||
62 | } |
||
63 | } else { |
||
64 | if (isset($this->rows['category'])) { |
||
65 | $model = $model::with(['getCategoryActive']); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | $items = $model->get($search_rows); |
||
70 | |||
71 | foreach ($items as $item) { |
||
72 | if (empty($item->{$this->search_title})) { |
||
73 | unset($data[$item->id]); |
||
74 | } else { |
||
75 | $data[$item->id]['id'] = $item->id; |
||
76 | $data[$item->id]['title'] = $item->{$this->search_title}; |
||
77 | $data[$item->id]['full_url'] = $item->full_url; |
||
78 | $data[$item->id]['component'] = $this->name; |
||
79 | $data[$item->id]['category'] = null; |
||
80 | $data[$item->id]['admin_url'] = $item->admin_url; |
||
81 | if ($admin) { |
||
82 | if ($item->getCategory) { |
||
83 | if (!isset($item->getCategory->id) && \count($item->getCategory) > 0) { |
||
84 | $data[$item->id]['category'] = $item->getCategory->first()->title; |
||
85 | } elseif (isset($item->getCategory->title)) { |
||
86 | $data[$item->id]['category'] = $item->getCategory->title; |
||
87 | } else { |
||
88 | unset($data[$item->id]); |
||
89 | } |
||
90 | } |
||
91 | } else { |
||
92 | if ($item->getCategoryActive) { |
||
93 | if (!isset($item->getCategoryActive->id) && \count($item->getCategoryActive) > 0) { |
||
94 | $data[$item->id]['category'] = $item->getCategoryActive->first()->title; |
||
95 | } elseif (isset($item->getCategoryActive->title)) { |
||
96 | $data[$item->id]['category'] = $item->getCategoryActive->title; |
||
97 | } else { |
||
98 | unset($data[$item->id]); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | if (\count($data) === 0) { |
||
105 | return null; |
||
106 | } |
||
107 | |||
108 | return $data; |
||
109 | }); |
||
112 |