| Conditions | 21 |
| Paths | 701 |
| Total Lines | 120 |
| 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 |
||
| 100 | public function Addons() |
||
| 101 | { |
||
| 102 | $list = Addon::get(); |
||
| 103 | |||
| 104 | if ($this->addons !== null) { |
||
| 105 | return $this->addons; |
||
| 106 | } |
||
| 107 | |||
| 108 | $search = $this->request->getVar('search'); |
||
| 109 | $type = $this->request->getVar('type'); |
||
| 110 | $compat = $this->request->getVar('compatibility'); |
||
| 111 | $tags = $this->request->getVar('tags'); |
||
| 112 | $sort = $this->request->getVar('sort'); |
||
| 113 | |||
| 114 | $useElasticOrderAsDefault = false; |
||
| 115 | |||
| 116 | if (!in_array($sort, array('name', 'downloads', 'newest', 'relative'))) { |
||
| 117 | $sort = null; |
||
| 118 | } |
||
| 119 | |||
| 120 | // Proxy out a search to elastic if any parameters are set. |
||
| 121 | if ($search || $type || $compat || $tags) { |
||
| 122 | $bool = new Query\BoolQuery(); |
||
| 123 | |||
| 124 | $query = new Query(); |
||
| 125 | $query->setQuery($bool); |
||
| 126 | $query->setSize(count($list)); |
||
| 127 | |||
| 128 | if ($search) { |
||
| 129 | $match = new Query\MultiMatch(); |
||
| 130 | $match->setQuery($search); |
||
| 131 | $match->setFields([ |
||
| 132 | 'name^12', |
||
| 133 | 'description^3', |
||
| 134 | 'readme', |
||
| 135 | ]); |
||
| 136 | $match->setType('phrase_prefix'); |
||
| 137 | $match->setUseDisMax(false); |
||
| 138 | |||
| 139 | $query->setSort([ |
||
| 140 | '_score', |
||
| 141 | 'downloads', |
||
| 142 | ]); |
||
| 143 | |||
| 144 | $scorer = new Query\FunctionScore(); |
||
| 145 | $scorer->setQuery($match); |
||
| 146 | $scorer->addFieldValueFactorFunction( |
||
| 147 | 'downloads', |
||
| 148 | 0.01, |
||
| 149 | Query\FunctionScore::FIELD_VALUE_FACTOR_MODIFIER_LN2P |
||
| 150 | ); |
||
| 151 | |||
| 152 | $bool->addMust($scorer); |
||
| 153 | $useElasticOrderAsDefault = true; |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($type) { |
||
| 157 | $bool->addMust(new Query\Term(array('type' => $type))); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($compat) { |
||
| 161 | $bool->addMust(new Query\Terms('compatibility', (array)$compat)); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($tags) { |
||
| 165 | $bool->addMust(new Query\Terms('tags', (array)$tags)); |
||
| 166 | } |
||
| 167 | |||
| 168 | $list = new ResultList($this->elastica->getIndex(), $query); |
||
| 169 | |||
| 170 | if ($sort) { |
||
| 171 | $ids = $list->column('ID'); |
||
| 172 | |||
| 173 | if ($ids) { |
||
|
|
|||
| 174 | $list = Addon::get()->byIDs($ids); |
||
| 175 | } else { |
||
| 176 | $list = new ArrayList(); |
||
| 177 | } |
||
| 178 | } else { |
||
| 179 | $list = $list->toArrayList(); |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | if (!$sort) { |
||
| 183 | $sort = 'relative'; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | switch ($sort) { |
||
| 188 | case 'name': |
||
| 189 | $list = $list->sort('Name'); |
||
| 190 | break; |
||
| 191 | case 'newest': |
||
| 192 | $list = $list->sort('Released', 'DESC'); |
||
| 193 | break; |
||
| 194 | case 'downloads': |
||
| 195 | $list = $list->sort('Downloads', 'DESC'); |
||
| 196 | break; |
||
| 197 | case 'relative': |
||
| 198 | if (!$list instanceof ArrayList) { |
||
| 199 | /** @var ArrayList|Addon[] $unsorted */ |
||
| 200 | $unsorted = ArrayList::create($list->toArray()); |
||
| 201 | } else { |
||
| 202 | $unsorted = $list; |
||
| 203 | } |
||
| 204 | foreach ($unsorted as $item) { |
||
| 205 | $item->Score = $item->relativePopularityFormatted() . ' per day'; |
||
| 206 | } |
||
| 207 | $list = $unsorted->sort('relativePopularity DESC'); |
||
| 208 | break; |
||
| 209 | default: |
||
| 210 | if (!$useElasticOrderAsDefault) { |
||
| 211 | $list = $list->sort('Downloads', 'DESC'); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $list = $this->addons = new PaginatedList($list, $this->request); |
||
| 216 | $list->setPageLength(16); |
||
| 217 | |||
| 218 | return $list; |
||
| 219 | } |
||
| 220 | |||
| 278 |
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.