Conditions | 20 |
Paths | 700 |
Total Lines | 102 |
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 |
||
95 | public function Addons() |
||
96 | { |
||
97 | $list = Addon::get(); |
||
98 | |||
99 | $search = $this->request->getVar('search'); |
||
100 | $type = $this->request->getVar('type'); |
||
101 | $compat = $this->request->getVar('compatibility'); |
||
102 | $tags = $this->request->getVar('tags'); |
||
103 | $sort = $this->request->getVar('sort'); |
||
104 | |||
105 | $useElasticOrderAsDefault = false; |
||
106 | |||
107 | if (!in_array($sort, array('name', 'downloads', 'newest', 'relative'))) { |
||
108 | $sort = null; |
||
109 | } |
||
110 | |||
111 | // Proxy out a search to elastic if any parameters are set. |
||
112 | if ($search || $type || $compat || $tags) { |
||
113 | $bool = new Query\BoolQuery(); |
||
114 | |||
115 | $query = new Query(); |
||
116 | $query->setQuery($bool); |
||
117 | $query->setSize(count($list)); |
||
118 | |||
119 | if ($search) { |
||
120 | $match = new Query\MultiMatch(); |
||
121 | $match->setQuery($search); |
||
122 | $match->setFields([ |
||
123 | 'name^12', |
||
124 | 'description^3', |
||
125 | 'readme', |
||
126 | ]); |
||
127 | $match->setType('phrase_prefix'); |
||
128 | |||
129 | $bool->addMust($match); |
||
130 | $useElasticOrderAsDefault = true; |
||
131 | } |
||
132 | |||
133 | if ($type) { |
||
134 | $bool->addMust(new Query\Term(array('type' => $type))); |
||
135 | } |
||
136 | |||
137 | if ($compat) { |
||
138 | $bool->addMust(new Query\Terms('compatibility', (array)$compat)); |
||
139 | } |
||
140 | |||
141 | if ($tags) { |
||
142 | $bool->addMust(new Query\Terms('tags', (array)$tags)); |
||
143 | } |
||
144 | |||
145 | $list = new ResultList($this->elastica->getIndex(), $query); |
||
146 | |||
147 | if ($sort) { |
||
148 | $ids = $list->column('ID'); |
||
149 | |||
150 | if ($ids) { |
||
|
|||
151 | $list = Addon::get()->byIDs($ids); |
||
152 | } else { |
||
153 | $list = new ArrayList(); |
||
154 | } |
||
155 | } else { |
||
156 | $list = $list->toArrayList(); |
||
157 | } |
||
158 | } else { |
||
159 | if (!$sort) { |
||
160 | $sort = 'relative'; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | switch ($sort) { |
||
165 | case 'name': |
||
166 | $list = $list->sort('Name'); |
||
167 | break; |
||
168 | case 'newest': |
||
169 | $list = $list->sort('Released', 'DESC'); |
||
170 | break; |
||
171 | case 'downloads': |
||
172 | $list = $list->sort('Downloads', 'DESC'); |
||
173 | break; |
||
174 | case 'relative': |
||
175 | if (!$list instanceof ArrayList) { |
||
176 | /** @var ArrayList|Addon[] $unsorted */ |
||
177 | $unsorted = ArrayList::create($list->toArray()); |
||
178 | } else { |
||
179 | $unsorted = $list; |
||
180 | } |
||
181 | foreach ($unsorted as $item) { |
||
182 | $item->Score = $item->relativePopularityFormatted() . ' per day'; |
||
183 | } |
||
184 | $list = $unsorted->sort('relativePopularity DESC'); |
||
185 | break; |
||
186 | default: |
||
187 | if (!$useElasticOrderAsDefault) { |
||
188 | $list = $list->sort('Downloads', 'DESC'); |
||
189 | } |
||
190 | } |
||
191 | |||
192 | $list = new PaginatedList($list, $this->request); |
||
193 | $list->setPageLength(16); |
||
194 | |||
195 | return $list; |
||
196 | } |
||
197 | |||
255 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.