Conditions | 17 |
Paths | 4112 |
Total Lines | 104 |
Code Lines | 62 |
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 |
||
77 | protected function &getListingResults(\Base $f3, \FFCMS\Mappers\Mapper $m): array |
||
78 | { |
||
79 | // set up paging limits |
||
80 | $minPerPage = 5; |
||
81 | $maxPerPage = 1024; |
||
82 | $perPage = (int) $f3->get('REQUEST.per_page'); |
||
83 | if ($perPage < $minPerPage) { |
||
84 | $perPage = $minPerPage; |
||
85 | } |
||
86 | if ($perPage > $maxPerPage) { |
||
87 | $perPage = $maxPerPage; |
||
88 | } |
||
89 | |||
90 | $page = $f3->get('REQUEST.page'); |
||
91 | if ($page < 1) { |
||
92 | $page = 1; |
||
93 | } |
||
94 | |||
95 | // validate order field |
||
96 | $order = $f3->get('REQUEST.order'); |
||
97 | |||
98 | // fetch data (paging is 0 based) |
||
99 | $allFields = $m->fields(); |
||
100 | |||
101 | // validate order field |
||
102 | $order = self::checkOrderField($f3->get('REQUEST.order'), $allFields); |
||
103 | |||
104 | // validated fields to return |
||
105 | $validFields = self::checkFieldsExist([$f3->get('REQUEST.fields')], $allFields); |
||
106 | $fields = empty($validFields['fields']) ? join(',', $allFields) : $validFields['fields']; |
||
107 | |||
108 | // count rows |
||
109 | $data = []; |
||
110 | $rows = $m->count(); |
||
111 | if ($rows < 1) { |
||
112 | return $data; |
||
113 | } |
||
114 | |||
115 | // if fewer results than per page, set per_page |
||
116 | if ($page == 1 && $perPage > $rows) { |
||
117 | $perPage = $rows; |
||
118 | } |
||
119 | |||
120 | $pagination = []; |
||
121 | $pagination['count'] = ceil($rows / $perPage); |
||
122 | |||
123 | // too high page number? |
||
124 | if ($page > $pagination['count']) { |
||
125 | $page = $pagination['count']; |
||
126 | } |
||
127 | |||
128 | // set up page URLs |
||
129 | $url = $f3->get('PATH'); |
||
130 | $urlParams = [ |
||
131 | 'per_page' => $perPage, |
||
132 | ]; |
||
133 | if (!empty($order)) { |
||
134 | $urlParams['order'] = $order; |
||
135 | } |
||
136 | if (!empty($fields)) { |
||
137 | $urlParams['fields'] = $fields; |
||
138 | } |
||
139 | ksort($urlParams); |
||
140 | |||
141 | // next/previous page url |
||
142 | $prevPage = (1 > $page - 1 ) ? null : $page - 1; |
||
143 | $nextPage = (1 + $page > $pagination['count']) ? null : $page + 1; |
||
144 | |||
145 | $resultsFrom = round($page * ($rows / $pagination['count'])) - $perPage + 1; |
||
146 | $resultsTo = $resultsFrom + $perPage - 1; |
||
147 | |||
148 | // return data |
||
149 | $data['pagination'] = [ |
||
150 | 'url_base' => $this->url($url, $urlParams), |
||
151 | 'url_current' => $this->url($url, $urlParams + ['page' => $page]), |
||
152 | 'url_first' => $this->url($url, $urlParams + ['page' => 1]), |
||
153 | 'url_last' => $this->url($url, $urlParams + ['page' => $pagination['count']]), |
||
154 | 'url_next' => (null == $nextPage) ? null : $this->url($url, $urlParams + ['page' => $nextPage]), |
||
155 | 'url_previous' => (null == $prevPage) ? null : $this->url($url, $urlParams + ['page' => $prevPage]), |
||
156 | 'results' => $rows, |
||
157 | 'results_from' => $resultsFrom, |
||
158 | 'results_to' => $resultsTo, |
||
159 | 'per_page' => $perPage, |
||
160 | 'pages' => $pagination['count'], |
||
161 | 'page' => $page, |
||
162 | 'object' => $m->table(), |
||
163 | 'fields' => preg_split("/[,]/", $fields), |
||
164 | 'view' => $f3->get('REQUEST.view') |
||
165 | ]; |
||
166 | |||
167 | // fetch results |
||
168 | $m->load('', [ |
||
169 | 'order' => $order, |
||
170 | 'offset' => (1 == $page) ? 0 : ($page - 1) * $perPage, |
||
171 | 'limit' => $perPage |
||
172 | ]); |
||
173 | |||
174 | do { |
||
175 | $data['objects'][] = $m->castFields($fields); |
||
176 | } |
||
177 | while ($m->skip()); |
||
178 | |||
179 | return $data; |
||
180 | } |
||
181 | |||
321 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.