Conditions | 66 |
Paths | > 20000 |
Total Lines | 246 |
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 |
||
289 | public function doProductSearchForm($data, $form) |
||
290 | { |
||
291 | $searchHistoryObject = null; |
||
292 | $immediateRedirectLink = ''; |
||
293 | if (! $this->maximumNumberOfResults) { |
||
294 | $this->maximumNumberOfResults = EcommerceConfig::get('ProductGroupSearchPage', 'maximum_number_of_products_to_list_for_search'); |
||
295 | } |
||
296 | if (isset($data['DebugSearch'])) { |
||
297 | $this->debug = $data['DebugSearch'] ? true : false; |
||
298 | } |
||
299 | if ($this->debug) { |
||
300 | $this->debugOutput('<hr /><hr /><hr /><h2>Debugging Search Results</h2>'); |
||
301 | } |
||
302 | |||
303 | //what is the baseclass? |
||
304 | $baseClassName = $this->baseClassForBuyables; |
||
305 | if (!$baseClassName) { |
||
306 | $baseClassName = EcommerceConfig::get('ProductGroup', 'base_buyable_class'); |
||
307 | } |
||
308 | if (!$baseClassName) { |
||
309 | user_error("Can not find $baseClassName (baseClassName)"); |
||
310 | } |
||
311 | //basic get |
||
312 | $singleton = Injector::inst()->get($baseClassName); |
||
313 | $searchableFields = $singleton->stat('searchable_fields'); |
||
314 | $baseList = $baseClassName::get()->filter(array('ShowInSearch' => 1)); |
||
315 | $ecomConfig = EcommerceDBConfig::current_ecommerce_db_config(); |
||
316 | if ($ecomConfig->OnlyShowProductsThatCanBePurchased) { |
||
317 | $baseList->filter(array('AllowPurchase' => 1)); |
||
318 | } |
||
319 | $limitToCurrentSection = false; |
||
320 | if (isset($data['SearchOnlyFieldsInThisSection']) && $data['SearchOnlyFieldsInThisSection']) { |
||
321 | $limitToCurrentSection = true; |
||
322 | if (! $this->productsToSearch) { |
||
323 | $controller = Controller::curr(); |
||
324 | if ($controller) { |
||
325 | $this->productsToSearch = $controller->Products(); |
||
326 | } |
||
327 | } |
||
328 | if ($this->productsToSearch instanceof DataList) { |
||
329 | $this->productsToSearch = $this->productsToSearch->map('ID', 'ID')->toArray(); |
||
330 | } |
||
331 | //last resort |
||
332 | if ($this->productsToSearch) { |
||
333 | $baseList = $baseList->filter(array('ID' => $this->productsToSearch)); |
||
334 | } |
||
335 | } |
||
336 | if (isset($data['MinimumPrice']) && $data['MinimumPrice']) { |
||
337 | $baseList = $baseList->filter(array('Price:GreaterThanOrEqual' => floatval($data['MinimumPrice']))); |
||
338 | } |
||
339 | if (isset($data['MaximumPrice']) && $data['MaximumPrice']) { |
||
340 | $baseList = $baseList->filter(array('Price:LessThanOrEqual' => floatval($data['MaximumPrice']))); |
||
341 | } |
||
342 | //defining some variables |
||
343 | $isKeywordSearch = false; |
||
344 | if ($this->debug) { |
||
345 | if ($this->productsToSearch) { |
||
346 | $this->debugOutput('<hr /><h3>PRODUCTS TO SEARCH</h3><pre>'.print_r($this->productsToSearch, 1).'</pre>'); |
||
347 | } |
||
348 | $this->debugOutput('<hr /><h3>BASE LIST</h3><pre>'.str_replace($this->sqlWords, array_flip($this->sqlWords), $baseList->sql()).'</pre>'); |
||
349 | } |
||
350 | //KEYWORD SEARCH - only bother if we have any keywords and results at all ... |
||
351 | if (isset($data['ShortKeyword']) && !isset($data['Keyword'])) { |
||
352 | $data['Keyword'] = $data['ShortKeyword']; |
||
353 | } |
||
354 | if (isset($data['Keyword']) && $keywordPhrase = $data['Keyword']) { |
||
355 | if ($baseList->count()) { |
||
356 | if (strlen($keywordPhrase) > 1) { |
||
357 | $isKeywordSearch = true; |
||
358 | $immediateRedirectLink = ''; |
||
359 | $this->resultArrayPos = 0; |
||
360 | $this->resultArray = []; |
||
361 | if ($this->debug) { |
||
362 | $this->debugOutput('<hr /><h3>Raw Keyword '.$keywordPhrase.'</h3><pre>'); |
||
363 | } |
||
364 | $keywordPhrase = Convert::raw2sql($keywordPhrase); |
||
365 | $keywordPhrase = strtolower($keywordPhrase); |
||
366 | |||
367 | $searchHistoryObjectID = SearchHistory::add_entry($keywordPhrase); |
||
368 | if ($searchHistoryObjectID) { |
||
369 | $searchHistoryObject = SearchHistory::get()->byID($searchHistoryObjectID); |
||
370 | } |
||
371 | |||
372 | // 1) Exact search by code |
||
373 | $count = 0; |
||
374 | if ($this->debug) { |
||
375 | $this->debugOutput('<hr /><h2>SEARCH BY CODE</h2>'); |
||
376 | } |
||
377 | $list1 = $baseList->filter(array('InternalItemID' => $keywordPhrase)); |
||
378 | $count = $list1->count(); |
||
379 | if ($count == 1) { |
||
380 | $immediateRedirectLink = $list1->First()->Link(); |
||
381 | $this->controller->redirect($immediateRedirectLink); |
||
382 | $this->debugOutput('<p style="color: red">Found one answer for potential immediate redirect: '.$immediateRedirectLink.'</p>'); |
||
383 | } |
||
384 | if ($count > 0) { |
||
385 | if ($this->addToResults($list1)) { |
||
386 | //break; |
||
387 | } |
||
388 | } |
||
389 | if ($this->debug) { |
||
390 | $this->debugOutput("<h3>SEARCH BY CODE RESULT: $count</h3>"); |
||
391 | } |
||
392 | |||
393 | // 2) Search for the entire keyword phrase and its replacements |
||
394 | $count = 0; |
||
395 | if ($this->debug) { |
||
396 | $this->debugOutput('<hr /><h3>FULL KEYWORD SEARCH</h3>'); |
||
397 | } |
||
398 | if ($this->resultArrayPos < $this->maximumNumberOfResults) { |
||
399 | $keywordPhrase = $this->replaceSearchPhraseOrWord($keywordPhrase); |
||
400 | //now we are going to look for synonyms |
||
401 | $words = explode(' ', trim(preg_replace('!\s+!', ' ', $keywordPhrase))); |
||
402 | foreach ($words as $wordKey => $word) { |
||
403 | $keywordPhrase = $this->replaceSearchPhraseOrWord($keywordPhrase); |
||
404 | } |
||
405 | if ($this->debug) { |
||
406 | $this->debugOutput('<pre>WORD ARRAY: '.print_r($keywordPhrase, 1).'</pre>'); |
||
407 | } |
||
408 | |||
409 | //work out searches |
||
410 | foreach ($this->extraBuyableFieldsToSearchFullText as $tempClassName => $fieldArrayTemp) { |
||
411 | if ($singleton instanceof $tempClassName) { |
||
412 | $fieldArray = $fieldArrayTemp; |
||
413 | break; |
||
414 | } |
||
415 | } |
||
416 | if ($this->debug) { |
||
417 | $this->debugOutput('<pre>FIELD ARRAY: '.print_r($fieldArray, 1).'</pre>'); |
||
418 | } |
||
419 | |||
420 | $searches = $this->getSearchArrays($keywordPhrase, $fieldArray); |
||
421 | //if($this->debug) { $this->debugOutput("<pre>SEARCH ARRAY: ".print_r($searches, 1)."</pre>");} |
||
422 | |||
423 | //we search exact matches first then other matches ... |
||
424 | foreach ($searches as $search) { |
||
425 | $list2 = $baseList->where($search); |
||
426 | $count = $list2->count(); |
||
427 | if ($this->debug) { |
||
428 | $this->debugOutput("<p>$search: $count</p>"); |
||
429 | } |
||
430 | if ($count > 0) { |
||
431 | if ($this->addToResults($list2)) { |
||
432 | break; |
||
433 | } |
||
434 | } |
||
435 | if ($this->resultArrayPos >= $this->maximumNumberOfResults) { |
||
436 | break; |
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | if ($this->debug) { |
||
441 | $this->debugOutput("<h3>FULL KEYWORD SEARCH: $count</h3>"); |
||
442 | } |
||
443 | |||
444 | if ($this->debug) { |
||
445 | $this->debugOutput('<hr /><h3>PRODUCT GROUP SEARCH</h3>'); |
||
446 | } |
||
447 | // 3) Do the same search for Product Group names |
||
448 | $count = 0; |
||
449 | if ($limitToCurrentSection) { |
||
450 | //cant search other sections in this case... |
||
451 | } else { |
||
452 | $searches = $this->getSearchArrays($keywordPhrase); |
||
453 | if ($this->debug) { |
||
454 | $this->debugOutput('<pre>SEARCH ARRAY: '.print_r($searches, 1).'</pre>'); |
||
455 | } |
||
456 | |||
457 | foreach ($searches as $search) { |
||
458 | $productGroups = ProductGroup::get()->where($search)->filter(array('ShowInSearch' => 1)); |
||
459 | $count = $productGroups->count(); |
||
460 | //redirect if we find exactly one match and we have no matches so far... |
||
461 | if ($count == 1 && !$this->resultArrayPos && !$limitToCurrentSection) { |
||
462 | $immediateRedirectLink = $productGroups->First()->Link(); |
||
463 | $this->debugOutput('<p style="color: red">Found one answer for potential immediate redirect: '.$immediateRedirectLink.'</p>'); |
||
464 | } |
||
465 | if ($count > 0) { |
||
466 | foreach ($productGroups as $productGroup) { |
||
467 | //we add them like this because we like to keep them in order! |
||
468 | if (!in_array($productGroup->ID, $this->productGroupIDs)) { |
||
469 | $this->productGroupIDs[] = $productGroup->ID; |
||
470 | } |
||
471 | } |
||
472 | } |
||
473 | } |
||
474 | if ($this->debug) { |
||
475 | $this->debugOutput("<h3>PRODUCT GROUP SEARCH: $count</h3>"); |
||
476 | } |
||
477 | } |
||
478 | } |
||
479 | } |
||
480 | } |
||
481 | if (! $isKeywordSearch) { |
||
482 | $this->addToResults($baseList); |
||
483 | } |
||
484 | $redirectToPage = null; |
||
485 | //if no specific section is being searched then we redirect to search page: |
||
486 | if (!$limitToCurrentSection) { |
||
487 | $redirectToPage = DataObject::get_one('ProductGroupSearchPage'); |
||
488 | } |
||
489 | if (!$redirectToPage) { |
||
490 | // for section specific search, |
||
491 | // redirect to the specific section (basically where we came from) |
||
492 | $redirectToPage = $this->controller->dataRecord; |
||
493 | } |
||
494 | |||
495 | $sessionNameProducts = $redirectToPage->SearchResultsSessionVariable(false); |
||
496 | $sessionNameGroups = $redirectToPage->SearchResultsSessionVariable(true); |
||
497 | |||
498 | if ($this->debug) { |
||
499 | $this->debugOutput( |
||
500 | '<hr />'. |
||
501 | '<h3>Previous Search Products: '.$sessionNameProducts.'</h3><p>'.print_r(Session::get($sessionNameProducts), 1).'</p>'. |
||
502 | '<h3>Previous Search Groups: '.$sessionNameGroups.'</h3><p>'.print_r(Session::get($sessionNameGroups), 1).'</p>' |
||
503 | ); |
||
504 | } |
||
505 | Session::set($sessionNameProducts, implode(',', $this->resultArray)); |
||
506 | Session::set($sessionNameGroups, implode(',', $this->productGroupIDs)); |
||
507 | Session::save(); |
||
508 | if ($searchHistoryObject) { |
||
509 | $searchHistoryObject->ProductCount = count($this->resultArray); |
||
510 | $searchHistoryObject->GroupCount = count($this->productGroupIDs); |
||
511 | $searchHistoryObject->write(); |
||
512 | } |
||
513 | if ($this->debug) { |
||
514 | |||
515 | $this->debugOutput( |
||
516 | '<hr />'. |
||
517 | '<h3>SAVING Products to session: '.$sessionNameProducts.'</h3><p>'.print_r(explode(',', Session::get($sessionNameProducts)), 1).'</p>'. |
||
518 | '<h3>SAVING Groups to session: '.$sessionNameGroups.'</h3><p>'.print_r(explode(',', Session::get($sessionNameGroups)), 1).'</p>'. |
||
519 | '<h3>Internal Item IDs for Products</h3><p>'.print_r($this->resultArrayPerIternalItemID, 1).'</p>' |
||
520 | ); |
||
521 | } |
||
522 | if ($immediateRedirectLink) { |
||
523 | $link = $immediateRedirectLink; |
||
524 | } else { |
||
525 | $link = $redirectToPage->Link($this->controllerSearchResultDisplayMethod); |
||
526 | } |
||
527 | if ($this->additionalGetParameters) { |
||
528 | $link .= '?'.$this->additionalGetParameters; |
||
529 | } |
||
530 | if ($this->debug) { |
||
531 | die('<a href="'.$link.'">see results</a>'); |
||
532 | } |
||
533 | $this->controller->redirect($link); |
||
534 | } |
||
535 | |||
737 |
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.