Conditions | 15 |
Paths | 192 |
Total Lines | 105 |
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 |
||
147 | public function Field($properties = array()) |
||
148 | { |
||
149 | $redirectToPage = DataObject::get_one('ProductGroupSearchPage'); |
||
150 | $title = $this->getContent(); |
||
151 | $totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack; |
||
152 | $data = DB::query(' |
||
153 | SELECT COUNT(ID) myCount, "Title" |
||
154 | FROM "SearchHistory" |
||
155 | WHERE Created > ( NOW() - INTERVAL '.$totalNumberOfDaysBack.' DAY ) |
||
156 | AND Created < ( NOW() - INTERVAL '.$this->endingDaysBack." DAY ) |
||
157 | GROUP BY \"Title\" |
||
158 | HAVING COUNT(\"ID\") >= $this->minimumCount |
||
159 | ORDER BY myCount DESC |
||
160 | LIMIT ".$this->maxRows." |
||
161 | "); |
||
162 | if (!$this->minimumCount) { |
||
163 | ++$this->minimumCount; |
||
164 | } |
||
165 | $content = ''; |
||
166 | $tableContent = ''; |
||
167 | if ($title && $this->addTitle) { |
||
168 | $content .= '<h3>'.$title.'</h3>'; |
||
169 | } |
||
170 | $content .= ' |
||
171 | <div id="SearchHistoryTableForCMS"> |
||
172 | <h3> |
||
173 | Search Phrases' |
||
174 | .($this->minimumCount > 1 ? ', entered at least '.$this->minimumCount.' times' : '') |
||
175 | .($this->maxRows < 1000 ? ', limited to '.$this->maxRows.' entries, ' : '') |
||
176 | .' between '.date('j-M-Y', strtotime('-'.$totalNumberOfDaysBack.' days')).' and '.date('j-M-Y', strtotime('-'.$this->endingDaysBack.' days')).' |
||
177 | </h3>'; |
||
178 | $count = 0; |
||
179 | if ($data && count($data)) { |
||
180 | $tableContent .= ' |
||
181 | <table class="highToLow" style="widht: 100%">'; |
||
182 | $list = array(); |
||
183 | foreach ($data as $key => $row) { |
||
184 | $count++; |
||
185 | //for the highest count, we work out a max-width |
||
186 | if (!$key) { |
||
187 | $maxWidth = $row['myCount']; |
||
188 | } |
||
189 | $multipliedWidthInPercentage = floor(($row['myCount'] / $maxWidth) * 100); |
||
190 | $list[$row['myCount'].'-'.$key] = $row['Title']; |
||
191 | $link = $redirectToPage->Link('ProductSearchForm').'?Keyword='.urlencode($row['Title']).'&action_doProductSearchForm=Search'; |
||
192 | $debugLink = $link .'&DebugSearch=1'; |
||
193 | $tableContent .= ' |
||
194 | <tr> |
||
195 | <td style="text-align: right; width: 30%; padding: 5px;"> |
||
196 | <a href="'.$link.'">'.$row['Title'].'</a> |
||
197 | </td> |
||
198 | <td style="background-color: silver; padding: 5px; width: 70%;"> |
||
199 | <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #C51162; color: #fff;">'.$row['myCount'].'</div> |
||
200 | </td> |
||
201 | <td style="background-color: silver; width: 20px"> |
||
202 | <a href="'.$debugLink.'">☕</a> |
||
203 | </td> |
||
204 | </tr>'; |
||
205 | } |
||
206 | $tableContent .= ' |
||
207 | </table>'; |
||
208 | if ($count && $this->addAtoZ) { |
||
209 | asort($list); |
||
210 | $tableContent .= ' |
||
211 | <h3>A - Z</h3> |
||
212 | <table class="aToz" style="widht: 100%">'; |
||
213 | foreach ($list as $key => $title) { |
||
214 | $link = $redirectToPage->Link('ProductSearchForm').'?Keyword='.urlencode($row['Title']).'&action_doProductSearchForm=Search'; |
||
215 | $debugLink = $link .'&DebugSearch=1'; |
||
216 | $array = explode('-', $key); |
||
217 | $multipliedWidthInPercentage = floor(($array[0] / $maxWidth) * 100); |
||
218 | $tableContent .= ' |
||
219 | <tr> |
||
220 | <td style="text-align: right; width: 30%; padding: 5px;"> |
||
221 | <a href="'.$link.'">'.$title.'</a> |
||
222 | </td> |
||
223 | <td style="background-color: silver; padding: 5px; width: 70%"> |
||
224 | <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #004D40; color: #fff;">'.trim($array[0]).'</div> |
||
225 | </td> |
||
226 | <td style="background-color: silver; width: 20px"> |
||
227 | <a href="'.$debugLink.'">☕</a> |
||
228 | </td> |
||
229 | </tr>'; |
||
230 | } |
||
231 | $tableContent .= ' |
||
232 | </table>'; |
||
233 | } |
||
234 | } |
||
235 | if ($count === 0) { |
||
236 | //we replace table content here... |
||
237 | $tableContent = '<p class="warning message">No searches found.</p>'; |
||
238 | } |
||
239 | $content .= $tableContent; |
||
240 | if ($this->showMoreLink) { |
||
241 | $content .= ' |
||
242 | <p> |
||
243 | <a href="/dev/tasks/EcommerceTaskReviewSearches/">Query more resuts</a> |
||
244 | </p>'; |
||
245 | } else { |
||
246 | } |
||
247 | $content .= ' |
||
248 | </div>'; |
||
249 | |||
250 | return $content; |
||
251 | } |
||
252 | } |
||
253 |