| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 119 |
| Code Lines | 73 |
| 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 |
||
| 92 | protected function &getListingResults(\Base $f3, \FFCMS\Mappers\Mapper $m, string $users_uuid = null): array |
||
| 93 | { |
||
| 94 | // set up paging limits |
||
| 95 | $minPerPage = 5; |
||
| 96 | $maxPerPage = 1024; |
||
| 97 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
| 98 | if ($perPage < $minPerPage) { |
||
| 99 | $perPage = $minPerPage; |
||
| 100 | } |
||
| 101 | if ($perPage > $maxPerPage) { |
||
| 102 | $perPage = $maxPerPage; |
||
| 103 | } |
||
| 104 | |||
| 105 | $page = $f3->get('REQUEST.page'); |
||
| 106 | if ($page < 1) { |
||
| 107 | $page = 1; |
||
| 108 | } |
||
| 109 | |||
| 110 | // fetch data (paging is 0 based) |
||
| 111 | $allFields = $m->fields(); |
||
| 112 | |||
| 113 | // validate order field |
||
| 114 | $order = self::checkOrderField($f3->get('REQUEST.order'), $allFields); |
||
| 115 | |||
| 116 | // validated fields to return |
||
| 117 | $validFields = self::checkFieldsExist([$f3->get('REQUEST.fields')], $allFields); |
||
| 118 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
| 119 | |||
| 120 | // count rows |
||
| 121 | $data = []; |
||
| 122 | |||
| 123 | // count rows |
||
| 124 | $isAdmin = $f3->get('isAdmin'); |
||
| 125 | $rows = 0; |
||
| 126 | if (in_array('users_uuid', $allFields) && !empty($users_uuid)) { |
||
| 127 | $rows = $m->count(['users_uuid = ?', $users_uuid]); |
||
| 128 | } elseif ($isAdmin && empty($users_uuid)) { |
||
| 129 | $rows = $m->count(); |
||
| 130 | } |
||
| 131 | if ($rows < 1) { |
||
| 132 | return $data; |
||
| 133 | } |
||
| 134 | |||
| 135 | // if fewer results than per page, set per_page |
||
| 136 | if ($page == 1 && $perPage > $rows) { |
||
| 137 | $perPage = $rows; |
||
| 138 | } |
||
| 139 | |||
| 140 | $pagination = []; |
||
| 141 | $pagination['count'] = ceil($rows / $perPage); |
||
| 142 | |||
| 143 | // too high page number? |
||
| 144 | if ($page > $pagination['count']) { |
||
| 145 | $page = $pagination['count']; |
||
| 146 | } |
||
| 147 | |||
| 148 | // set up page URLs |
||
| 149 | $url = $f3->get('PATH'); |
||
| 150 | $urlParams = [ |
||
| 151 | 'per_page' => $perPage, |
||
| 152 | ]; |
||
| 153 | if (!empty($order)) { |
||
| 154 | $urlParams['order'] = $order; |
||
| 155 | } |
||
| 156 | if (!empty($fields)) { |
||
| 157 | $urlParams['fields'] = $fields; |
||
| 158 | } |
||
| 159 | ksort($urlParams); |
||
| 160 | |||
| 161 | // next/previous page url |
||
| 162 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
| 163 | $nextPage = (1 + $page > $pagination['count']) ? null : $page + 1; |
||
| 164 | |||
| 165 | $resultsFrom = round($page * ($rows / $pagination['count'])) - $perPage + 1; |
||
| 166 | $resultsTo = $resultsFrom + $perPage - 1; |
||
| 167 | |||
| 168 | // return data |
||
| 169 | $data['pagination'] = [ |
||
| 170 | 'url_base' => $this->url($url, $urlParams), |
||
| 171 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
| 172 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
| 173 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
| 174 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
| 175 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
| 176 | 'results' => $rows, |
||
| 177 | 'results_from' => $resultsFrom, |
||
| 178 | 'results_to' => $resultsTo, |
||
| 179 | 'per_page' => $perPage, |
||
| 180 | 'pages' => $pagination['count'], |
||
| 181 | 'page' => $page, |
||
| 182 | 'object' => $m->table(), |
||
| 183 | 'fields' => preg_split("/[,]/", $fields), |
||
| 184 | 'view' => $f3->get('REQUEST.view') |
||
| 185 | ]; |
||
| 186 | |||
| 187 | |||
| 188 | // fetch results |
||
| 189 | if ($isAdmin && empty($users_uuid)) { |
||
| 190 | $m->load('', [ |
||
| 191 | 'order' => $order, |
||
| 192 | 'offset' => (1 == $page) ? 0 : ($page - 1) * $perPage, |
||
| 193 | 'limit' => $perPage |
||
| 194 | ]); |
||
| 195 | } else { |
||
| 196 | $m->load(['users_uuid = ?', $users_uuid], [ |
||
| 197 | 'order' => $order, |
||
| 198 | 'offset' => (1 == $page) ? 0 : ($page - 1) * $perPage, |
||
| 199 | 'limit' => $perPage |
||
| 200 | ]); |
||
| 201 | } |
||
| 202 | |||
| 203 | $adminView = $isAdmin || ($isAdmin && 'admin' == $f3->get('REQUEST.view')); |
||
| 204 | do { |
||
| 205 | $data['objects'][] = $adminView ? $m->castFields($fields) : $m->exportArray($fields); |
||
| 206 | } |
||
| 207 | while ($m->skip()); |
||
| 208 | |||
| 209 | return $data; |
||
| 210 | } |
||
| 211 | |||
| 368 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.