| Conditions | 11 |
| Paths | 1104 |
| Total Lines | 123 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 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 |
||
| 37 | public function similar() { |
||
| 38 | //FIXME double check security, ie if escaping needed |
||
| 39 | $class = $this->request->param('ID'); |
||
| 40 | $instanceID = $this->request->param('OtherID'); |
||
| 41 | |||
| 42 | $data = array( |
||
| 43 | 'Content' => $this->Content, |
||
| 44 | 'Title' => $this->Title, |
||
| 45 | 'SearchPerformed' => false |
||
| 46 | ); |
||
| 47 | |||
| 48 | // record the time |
||
| 49 | $startTime = microtime(true); |
||
| 50 | |||
| 51 | //instance of ElasticPage associated with this controller |
||
| 52 | $ep = Controller::curr()->dataRecord; |
||
| 53 | |||
| 54 | // use an Elastic Searcher, which needs primed from URL params |
||
| 55 | $es = new ElasticSearcher(); |
||
| 56 | |||
| 57 | $this->setStartParamsFromRequest($es); |
||
| 58 | |||
| 59 | |||
| 60 | $es->setMinTermFreq($this->MinTermFreq); |
||
| 61 | $es->setMaxTermFreq($this->MaxTermFreq); |
||
| 62 | $es->setMinDocFreq($this->MinDocFreq); |
||
| 63 | $es->setMaxDocFreq($this->MaxDocFreq); |
||
| 64 | $es->setMinWordLength($this->MinWordLength); |
||
| 65 | $es->setMaxWordLength($this->MaxWordLength); |
||
| 66 | $es->setMinShouldMatch($this->MinShouldMatch); |
||
| 67 | $es->setSimilarityStopWords($this->SimilarityStopWords); |
||
| 68 | |||
| 69 | |||
| 70 | // filter by class or site tree |
||
| 71 | if($ep->SiteTreeOnly) { |
||
| 72 | T7; //FIXME test missing |
||
| 73 | $es->addFilter('IsInSiteTree', true); |
||
| 74 | } else { |
||
| 75 | $es->setClasses($ep->ClassesToSearch); |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | // get the edited fields to search from the database for this search page |
||
| 80 | // Convert this into a name => weighting array |
||
| 81 | $fieldsToSearch = array(); |
||
| 82 | $editedSearchFields = $this->ElasticaSearchableFields()->filter(array( |
||
| 83 | 'Active' => true, |
||
| 84 | 'SimilarSearchable' => true |
||
| 85 | )); |
||
| 86 | |||
| 87 | foreach($editedSearchFields->getIterator() as $searchField) { |
||
| 88 | $fieldsToSearch[$searchField->Name] = $searchField->Weight; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Use the standard field for more like this, ie not stemmed |
||
| 92 | foreach($fieldsToSearch as $field => $value) { |
||
| 93 | $fieldsToSearch[$field . '.standard'] = $value; |
||
| 94 | unset($fieldsToSearch[$field]); |
||
| 95 | } |
||
| 96 | |||
| 97 | try { |
||
| 98 | // Simulate server being down for testing purposes |
||
| 99 | if($this->request->getVar('ServerDown')) { |
||
| 100 | throw new Elastica\Exception\Connection\HttpException('Unable to reach search server'); |
||
| 101 | } |
||
| 102 | if(class_exists($class)) { |
||
| 103 | $instance = \DataObject::get_by_id($class, $instanceID); |
||
| 104 | |||
| 105 | $paginated = $es->moreLikeThis($instance, $fieldsToSearch); |
||
| 106 | |||
| 107 | $this->Aggregations = $es->getAggregations(); |
||
| 108 | $data['SearchResults'] = $paginated; |
||
| 109 | $data['SearchPerformed'] = true; |
||
| 110 | $data['SearchPageLink'] = $ep->Link(); |
||
| 111 | $data['SimilarTo'] = $instance; |
||
| 112 | $data['NumberOfResults'] = $paginated->getTotalItems(); |
||
| 113 | |||
| 114 | |||
| 115 | $moreLikeThisTerms = $paginated->getList()->MoreLikeThisTerms; |
||
| 116 | $fieldToTerms = new ArrayList(); |
||
| 117 | foreach(array_keys($moreLikeThisTerms) as $fieldName) { |
||
| 118 | $readableFieldName = str_replace('.standard', '', $fieldName); |
||
| 119 | $fieldTerms = new ArrayList(); |
||
| 120 | foreach($moreLikeThisTerms[$fieldName] as $value) { |
||
| 121 | $do = new DataObject(); |
||
| 122 | $do->Term = $value; |
||
| 123 | $fieldTerms->push($do); |
||
| 124 | } |
||
| 125 | |||
| 126 | $do = new DataObject(); |
||
| 127 | $do->FieldName = $readableFieldName; |
||
| 128 | $do->Terms = $fieldTerms; |
||
| 129 | $fieldToTerms->push($do); |
||
| 130 | } |
||
| 131 | |||
| 132 | $data['SimilarSearchTerms'] = $fieldToTerms; |
||
| 133 | } else { |
||
| 134 | // class does not exist |
||
| 135 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
| 136 | } |
||
| 137 | } catch (\InvalidArgumentException $e) { |
||
| 138 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
| 139 | } catch (Elastica\Exception\Connection\HttpException $e) { |
||
| 140 | $data['ErrorMessage'] = 'Unable to connect to search server'; |
||
| 141 | $data['SearchPerformed'] = false; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | // calculate time |
||
| 146 | $endTime = microtime(true); |
||
| 147 | $elapsed = round(100 * ($endTime - $startTime)) / 100; |
||
| 148 | |||
| 149 | // store variables for the template to use |
||
| 150 | $data['ElapsedTime'] = $elapsed; |
||
| 151 | $data['Elapsed'] = $elapsed; |
||
| 152 | |||
| 153 | // allow the optional use of overriding the search result page, e.g. for photos, maps or facets |
||
| 154 | if($this->hasExtension('PageControllerTemplateOverrideExtension')) { |
||
| 155 | return $this->useTemplateOverride($data); |
||
| 156 | } else { |
||
| 157 | return $data; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 370 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.