Conditions | 12 |
Paths | 2208 |
Total Lines | 127 |
Code Lines | 77 |
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 | // start, and page length, i.e. pagination |
||
58 | $startParam = $this->request->getVar('start'); |
||
59 | $start = isset($startParam) ? $startParam : 0; |
||
60 | $es->setStart($start); |
||
61 | $es->setPageLength($ep->ResultsPerPage); |
||
62 | |||
63 | |||
64 | $es->setMinTermFreq($this->MinTermFreq); |
||
65 | $es->setMaxTermFreq($this->MaxTermFreq); |
||
66 | $es->setMinDocFreq($this->MinDocFreq); |
||
67 | $es->setMaxDocFreq($this->MaxDocFreq); |
||
68 | $es->setMinWordLength($this->MinWordLength); |
||
69 | $es->setMaxWordLength($this->MaxWordLength); |
||
70 | $es->setMinShouldMatch($this->MinShouldMatch); |
||
71 | $es->setSimilarityStopWords($this->SimilarityStopWords); |
||
72 | |||
73 | |||
74 | // filter by class or site tree |
||
75 | if($ep->SiteTreeOnly) { |
||
76 | T7; //FIXME test missing |
||
77 | $es->addFilter('IsInSiteTree', true); |
||
|
|||
78 | } else { |
||
79 | $es->setClasses($ep->ClassesToSearch); |
||
80 | } |
||
81 | |||
82 | |||
83 | // get the edited fields to search from the database for this search page |
||
84 | // Convert this into a name => weighting array |
||
85 | $fieldsToSearch = array(); |
||
86 | $editedSearchFields = $this->ElasticaSearchableFields()->filter(array( |
||
87 | 'Active' => true, |
||
88 | 'SimilarSearchable' => true |
||
89 | )); |
||
90 | |||
91 | foreach($editedSearchFields->getIterator() as $searchField) { |
||
92 | $fieldsToSearch[$searchField->Name] = $searchField->Weight; |
||
93 | } |
||
94 | |||
95 | // Use the standard field for more like this, ie not stemmed |
||
96 | foreach($fieldsToSearch as $field => $value) { |
||
97 | $fieldsToSearch[$field . '.standard'] = $value; |
||
98 | unset($fieldsToSearch[$field]); |
||
99 | } |
||
100 | |||
101 | try { |
||
102 | // Simulate server being down for testing purposes |
||
103 | if($this->request->getVar('ServerDown')) { |
||
104 | throw new Elastica\Exception\Connection\HttpException('Unable to reach search server'); |
||
105 | } |
||
106 | if(class_exists($class)) { |
||
107 | $instance = \DataObject::get_by_id($class, $instanceID); |
||
108 | |||
109 | $paginated = $es->moreLikeThis($instance, $fieldsToSearch); |
||
110 | |||
111 | $this->Aggregations = $es->getAggregations(); |
||
112 | $data['SearchResults'] = $paginated; |
||
113 | $data['SearchPerformed'] = true; |
||
114 | $data['SearchPageLink'] = $ep->Link(); |
||
115 | $data['SimilarTo'] = $instance; |
||
116 | $data['NumberOfResults'] = $paginated->getTotalItems(); |
||
117 | |||
118 | |||
119 | $moreLikeThisTerms = $paginated->getList()->MoreLikeThisTerms; |
||
1 ignored issue
–
show
|
|||
120 | $fieldToTerms = new ArrayList(); |
||
121 | foreach(array_keys($moreLikeThisTerms) as $fieldName) { |
||
122 | $readableFieldName = str_replace('.standard', '', $fieldName); |
||
123 | $fieldTerms = new ArrayList(); |
||
124 | foreach($moreLikeThisTerms[$fieldName] as $value) { |
||
125 | $do = new DataObject(); |
||
126 | $do->Term = $value; |
||
127 | $fieldTerms->push($do); |
||
128 | } |
||
129 | |||
130 | $do = new DataObject(); |
||
131 | $do->FieldName = $readableFieldName; |
||
132 | $do->Terms = $fieldTerms; |
||
133 | $fieldToTerms->push($do); |
||
134 | } |
||
135 | |||
136 | $data['SimilarSearchTerms'] = $fieldToTerms; |
||
137 | } else { |
||
138 | // class does not exist |
||
139 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
140 | } |
||
141 | } catch (\InvalidArgumentException $e) { |
||
142 | $data['ErrorMessage'] = "Class $class is either not found or not searchable\n"; |
||
143 | } catch (Elastica\Exception\Connection\HttpException $e) { |
||
144 | $data['ErrorMessage'] = 'Unable to connect to search server'; |
||
145 | $data['SearchPerformed'] = false; |
||
146 | } |
||
147 | |||
148 | |||
149 | // calculate time |
||
150 | $endTime = microtime(true); |
||
151 | $elapsed = round(100 * ($endTime - $startTime)) / 100; |
||
152 | |||
153 | // store variables for the template to use |
||
154 | $data['ElapsedTime'] = $elapsed; |
||
155 | $data['Elapsed'] = $elapsed; |
||
156 | |||
157 | // allow the optional use of overriding the search result page, e.g. for photos, maps or facets |
||
158 | if($this->hasExtension('PageControllerTemplateOverrideExtension')) { |
||
159 | return $this->useTemplateOverride($data); |
||
160 | } else { |
||
161 | return $data; |
||
162 | } |
||
163 | } |
||
164 | |||
363 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: