| Conditions | 19 |
| Paths | 600 |
| Total Lines | 93 |
| Code Lines | 60 |
| 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 | if (!in_array($sort, array('name', 'downloads', 'newest', 'relative'))) { |
||
| 106 | $sort = null; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Proxy out a search to elastic if any parameters are set. |
||
| 110 | if ($search || $type || $compat || $tags) { |
||
| 111 | |||
| 112 | $bool = new Query\BoolQuery(); |
||
| 113 | |||
| 114 | $query = new Query(); |
||
| 115 | $query->setQuery($bool); |
||
| 116 | $query->setSize(count($list)); |
||
| 117 | |||
| 118 | |||
| 119 | if ($search) { |
||
| 120 | $match = new Match(); |
||
| 121 | $match->setField('_all', $search); |
||
| 122 | |||
| 123 | $bool->addMust($match); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($type) { |
||
| 127 | $bool->addMust(new Query\Term(array('type' => $type))); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($compat) { |
||
| 131 | $bool->addMust(new Query\Terms('compatibility', (array)$compat)); |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($tags) { |
||
| 135 | $bool->addMust(new Query\Terms('tag', (array)$tags)); |
||
| 136 | } |
||
| 137 | |||
| 138 | $list = new ResultList($this->elastica->getIndex(), $query); |
||
| 139 | |||
| 140 | if ($sort) { |
||
| 141 | $ids = $list->column('ID'); |
||
| 142 | |||
| 143 | if ($ids) { |
||
|
|
|||
| 144 | $list = Addon::get()->byIDs($ids); |
||
| 145 | } else { |
||
| 146 | $list = new ArrayList(); |
||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $list = $list->toArrayList(); |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | if (!$sort) { |
||
| 153 | $sort = 'relative'; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | switch ($sort) { |
||
| 158 | case 'name': |
||
| 159 | $list = $list->sort('Name'); |
||
| 160 | break; |
||
| 161 | case 'newest': |
||
| 162 | $list = $list->sort('Released', 'DESC'); |
||
| 163 | break; |
||
| 164 | case 'downloads': |
||
| 165 | $list = $list->sort('Downloads', 'DESC'); |
||
| 166 | break; |
||
| 167 | case 'relative': |
||
| 168 | if (!$list instanceof ArrayList) { |
||
| 169 | /** @var ArrayList|Addon[] $unsorted */ |
||
| 170 | $unsorted = ArrayList::create($list->toArray()); |
||
| 171 | } else { |
||
| 172 | $unsorted = $list; |
||
| 173 | } |
||
| 174 | foreach ($unsorted as $item) { |
||
| 175 | $item->Score = $item->relativePopularityFormatted() . ' per day'; |
||
| 176 | } |
||
| 177 | $list = $unsorted->sort('relativePopularity DESC'); |
||
| 178 | break; |
||
| 179 | default: |
||
| 180 | $list = $list->sort('Downloads', 'DESC'); |
||
| 181 | } |
||
| 182 | |||
| 183 | $list = new PaginatedList($list, $this->request); |
||
| 184 | $list->setPageLength(16); |
||
| 185 | |||
| 186 | return $list; |
||
| 187 | } |
||
| 188 | |||
| 247 |
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.