Conditions | 9 |
Paths | 27 |
Total Lines | 67 |
Code Lines | 39 |
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 |
||
131 | final public function search($keyword, array $searchOptions = [], array $logOptions = []) |
||
132 | { |
||
133 | if (!is_string($keyword)) { |
||
134 | throw new InvalidArgumentException( |
||
135 | 'Search query must be a string.' |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | if ($keyword == '') { |
||
140 | throw new InvalidArgumentException( |
||
141 | 'Keyword can not be empty.' |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | // Reset results |
||
146 | $this->results = []; |
||
147 | |||
148 | $searchConfig = $this->searchConfig(); |
||
149 | |||
150 | if (!isset($searchConfig['searches'])) { |
||
151 | throw new InvalidArgumentException( |
||
152 | 'No searches defined in search config.' |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | $numResults = 0; |
||
157 | $searchObjects = $searchConfig['searches']; |
||
158 | $searchDeps = [ |
||
159 | 'logger' => $this->logger, |
||
160 | 'model_factory' => $this->modelFactory() |
||
161 | ]; |
||
162 | |||
163 | foreach ($searchObjects as $searchIdent => $searchObj) { |
||
164 | if ($searchObj instanceof SearchInterface) { |
||
165 | $results = $searchObj->search($keyword, $searchObjects); |
||
166 | } else { |
||
167 | $search = new CustomSearch(array_merge($searchObj, $searchDeps)); |
||
168 | $results = $search->search($keyword, $searchObjects); |
||
169 | } |
||
170 | |||
171 | $this->results[$searchIdent] = $results; |
||
172 | $numResults += count($results); |
||
173 | } |
||
174 | |||
175 | |||
176 | $logResults = []; |
||
177 | foreach($this->results as $k=>$v) { |
||
178 | $logResults[$k] = $this->searchResultsToLogResults($v); |
||
179 | } |
||
180 | |||
181 | $logData = [ |
||
182 | 'search_ident' => isset($searchConfig['ident']) ? $searchConfig['ident'] : '', |
||
183 | 'search_options' => $searchOptions, |
||
184 | 'keyword' => $keyword, |
||
185 | 'num_results' => $numResults, |
||
186 | 'results' => $logResults |
||
187 | ]; |
||
188 | |||
189 | if ($logOptions) { |
||
190 | $logOptions = array_diff_key($logOptions, array_keys($logData)); |
||
191 | $logData = array_merge($logData, $logOptions); |
||
192 | } |
||
193 | |||
194 | $this->searchLog = $this->createLog($logData); |
||
195 | |||
196 | return $this->results; |
||
197 | } |
||
198 | |||
233 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.