Conditions | 12 |
Paths | 18 |
Total Lines | 108 |
Code Lines | 73 |
Lines | 6 |
Ratio | 5.56 % |
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 |
||
199 | public function renderSearchResult(PMF_Search_Resultset $resultSet, $currentPage) |
||
200 | { |
||
201 | $html = ''; |
||
202 | $confPerPage = $this->_config->get('records.numberOfRecordsPerPage'); |
||
203 | $numOfResults = $resultSet->getNumberOfResults(); |
||
204 | |||
205 | $totalPages = ceil($numOfResults / $confPerPage); |
||
206 | $lastPage = $currentPage * $confPerPage; |
||
207 | $firstPage = $lastPage - $confPerPage; |
||
208 | if ($lastPage > $numOfResults) { |
||
209 | $lastPage = $numOfResults; |
||
210 | } |
||
211 | |||
212 | if (0 < $numOfResults) { |
||
213 | $html .= sprintf( |
||
214 | "<p role=\"heading\" aria-level=\"1\">%s</p>\n", |
||
215 | $this->plurals->GetMsg('plmsgSearchAmount', $numOfResults) |
||
216 | ); |
||
217 | |||
218 | if (1 < $totalPages) { |
||
219 | $html .= sprintf( |
||
220 | "<p><strong>%s%d %s %s</strong></p>\n", |
||
221 | $this->translation['msgPage'], |
||
222 | $currentPage, |
||
223 | $this->translation['msgVoteFrom'], |
||
224 | $this->plurals->GetMsg('plmsgPagesTotal', $totalPages) |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | $html .= "<ul class=\"phpmyfaq-search-results list-unstyled\">\n"; |
||
229 | |||
230 | $counter = $displayedCounter = 0; |
||
231 | $faqHelper = new PMF_Helper_Faq($this->_config); |
||
232 | foreach ($resultSet->getResultset() as $result) { |
||
233 | if ($displayedCounter >= $confPerPage) { |
||
234 | break; |
||
235 | } |
||
236 | ++$counter; |
||
237 | if ($counter <= $firstPage) { |
||
238 | continue; |
||
239 | } |
||
240 | ++$displayedCounter; |
||
241 | |||
242 | // Set language for current category to fetch the correct category name |
||
243 | $this->Category->setLanguage($result->lang); |
||
244 | |||
245 | $categoryInfo = $this->Category->getCategoriesFromArticle($result->id); |
||
246 | $categoryInfo = array_values($categoryInfo); //Reset the array keys |
||
247 | $question = PMF_Utils::chopString($result->question, 15); |
||
248 | $answerPreview = $faqHelper->renderAnswerPreview($result->answer, 25); |
||
249 | |||
250 | $searchterm = str_replace( |
||
251 | ['^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']', '"'], |
||
252 | '', |
||
253 | $this->searchterm |
||
254 | ); |
||
255 | $searchterm = preg_quote($searchterm, '/'); |
||
256 | $searchItems = explode(' ', $searchterm); |
||
257 | |||
258 | if ($this->_config->get('search.enableHighlighting') && PMF_String::strlen($searchItems[0]) > 1) { |
||
259 | View Code Duplication | foreach ($searchItems as $item) { |
|
260 | if (PMF_String::strlen($item) > 2) { |
||
261 | $question = PMF_Utils::setHighlightedString($question, $item); |
||
|
|||
262 | $answerPreview = PMF_Utils::setHighlightedString($answerPreview, $item); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | // Build the link to the faq record |
||
268 | $currentUrl = sprintf( |
||
269 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s&highlight=%s', |
||
270 | PMF_Link::getSystemRelativeUri(), |
||
271 | $this->sessionId, |
||
272 | $result->category_id, |
||
273 | $result->id, |
||
274 | $result->lang, |
||
275 | urlencode($searchterm) |
||
276 | ); |
||
277 | |||
278 | $oLink = new PMF_Link($currentUrl, $this->_config); |
||
279 | $oLink->text = $question; |
||
280 | $oLink->itemTitle = $oLink->tooltip = $result->question; |
||
281 | |||
282 | $html .= '<li>'; |
||
283 | $html .= $this->renderScore($result->score * 33); |
||
284 | $html .= sprintf('<strong>%s</strong>: %s<br />', |
||
285 | $categoryInfo[0]['name'], |
||
286 | $oLink->toHtmlAnchor() |
||
287 | ); |
||
288 | $html .= sprintf( |
||
289 | "<small class=\"searchpreview\"><strong>%s</strong> %s...</small>\n", |
||
290 | $this->translation['msgSearchContent'], |
||
291 | $answerPreview |
||
292 | ); |
||
293 | $html .= '</li>'; |
||
294 | } |
||
295 | |||
296 | $html .= "</ul>\n"; |
||
297 | |||
298 | if (1 < $totalPages) { |
||
299 | $html .= $this->pagination->render(); |
||
300 | } |
||
301 | } else { |
||
302 | $html = $this->translation['err_noArticles']; |
||
303 | } |
||
304 | |||
305 | return $html; |
||
306 | } |
||
307 | |||
398 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.