Conditions | 12 |
Paths | 386 |
Total Lines | 142 |
Code Lines | 83 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
157 | public function searchEngine( |
||
158 | $classesToSearch, |
||
159 | $keywords, |
||
160 | $start, |
||
161 | $pageLength, |
||
162 | $sortBy = "Relevance DESC", |
||
163 | $extraFilter = "", |
||
164 | $booleanSearch = false, |
||
165 | $alternativeFileFilter = "", |
||
166 | $invertedMatch = false |
||
167 | ) { |
||
168 | $pageClass = SiteTree::class; |
||
169 | $fileClass = File::class; |
||
170 | if (!class_exists($pageClass)) { |
||
171 | throw new Exception('MySQLDatabase->searchEngine() requires "SiteTree" class'); |
||
172 | } |
||
173 | if (!class_exists($fileClass)) { |
||
174 | throw new Exception('MySQLDatabase->searchEngine() requires "File" class'); |
||
175 | } |
||
176 | |||
177 | $keywords = $this->escapeString($keywords); |
||
178 | $htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES, 'UTF-8'); |
||
179 | |||
180 | $extraFilters = array($pageClass => '', $fileClass => ''); |
||
181 | |||
182 | $boolean = ''; |
||
183 | if ($booleanSearch) { |
||
184 | $boolean = "IN BOOLEAN MODE"; |
||
185 | } |
||
186 | |||
187 | if ($extraFilter) { |
||
188 | $extraFilters[$pageClass] = " AND $extraFilter"; |
||
189 | |||
190 | if ($alternativeFileFilter) { |
||
191 | $extraFilters[$fileClass] = " AND $alternativeFileFilter"; |
||
192 | } else { |
||
193 | $extraFilters[$fileClass] = $extraFilters[$pageClass]; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // Always ensure that only pages with ShowInSearch = 1 can be searched |
||
198 | $extraFilters[$pageClass] .= " AND ShowInSearch <> 0"; |
||
199 | |||
200 | // File.ShowInSearch was added later, keep the database driver backwards compatible |
||
201 | // by checking for its existence first |
||
202 | $fileTable = DataObject::getSchema()->tableName($fileClass); |
||
203 | $fields = $this->getSchemaManager()->fieldList($fileTable); |
||
204 | if (array_key_exists('ShowInSearch', $fields)) { |
||
205 | $extraFilters[$fileClass] .= " AND ShowInSearch <> 0"; |
||
206 | } |
||
207 | |||
208 | $limit = (int)$start . ", " . (int)$pageLength; |
||
209 | |||
210 | $notMatch = $invertedMatch |
||
211 | ? "NOT " |
||
212 | : ""; |
||
213 | if ($keywords) { |
||
214 | $match[$pageClass] = " |
||
215 | MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$keywords' $boolean) |
||
216 | + MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$htmlEntityKeywords' $boolean) |
||
217 | "; |
||
218 | $fileClassSQL = Convert::raw2sql($fileClass); |
||
219 | $match[$fileClass] = "MATCH (Name, Title) AGAINST ('$keywords' $boolean) AND ClassName = '$fileClassSQL'"; |
||
220 | |||
221 | // We make the relevance search by converting a boolean mode search into a normal one |
||
222 | $relevanceKeywords = str_replace(array('*', '+', '-'), '', $keywords); |
||
223 | $htmlEntityRelevanceKeywords = str_replace(array('*', '+', '-'), '', $htmlEntityKeywords); |
||
224 | $relevance[$pageClass] = "MATCH (Title, MenuTitle, Content, MetaDescription) " |
||
225 | . "AGAINST ('$relevanceKeywords') " |
||
226 | . "+ MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$htmlEntityRelevanceKeywords')"; |
||
227 | $relevance[$fileClass] = "MATCH (Name, Title) AGAINST ('$relevanceKeywords')"; |
||
228 | } else { |
||
229 | $relevance[$pageClass] = $relevance[$fileClass] = 1; |
||
230 | $match[$pageClass] = $match[$fileClass] = "1 = 1"; |
||
231 | } |
||
232 | |||
233 | // Generate initial DataLists and base table names |
||
234 | $lists = array(); |
||
235 | $sqlTables = array($pageClass => '', $fileClass => ''); |
||
236 | foreach ($classesToSearch as $class) { |
||
237 | $lists[$class] = DataList::create($class)->where($notMatch . $match[$class] . $extraFilters[$class]); |
||
238 | $sqlTables[$class] = '"' . DataObject::getSchema()->tableName($class) . '"'; |
||
239 | } |
||
240 | |||
241 | $charset = static::config()->get('charset'); |
||
242 | |||
243 | // Make column selection lists |
||
244 | $select = array( |
||
245 | $pageClass => array( |
||
246 | "ClassName", "{$sqlTables[$pageClass]}.\"ID\"", "ParentID", |
||
247 | "Title", "MenuTitle", "URLSegment", "Content", |
||
248 | "LastEdited", "Created", |
||
249 | "Name" => "_{$charset}''", |
||
250 | "Relevance" => $relevance[$pageClass], "CanViewType" |
||
251 | ), |
||
252 | $fileClass => array( |
||
253 | "ClassName", "{$sqlTables[$fileClass]}.\"ID\"", "ParentID", |
||
254 | "Title", "MenuTitle" => "_{$charset}''", "URLSegment" => "_{$charset}''", "Content" => "_{$charset}''", |
||
255 | "LastEdited", "Created", |
||
256 | "Name", |
||
257 | "Relevance" => $relevance[$fileClass], "CanViewType" => "NULL" |
||
258 | ), |
||
259 | ); |
||
260 | |||
261 | // Process and combine queries |
||
262 | $querySQLs = array(); |
||
263 | $queryParameters = array(); |
||
264 | $totalCount = 0; |
||
265 | foreach ($lists as $class => $list) { |
||
266 | /** @var SQLSelect $query */ |
||
267 | $query = $list->dataQuery()->query(); |
||
268 | |||
269 | // There's no need to do all that joining |
||
270 | $query->setFrom($sqlTables[$class]); |
||
271 | $query->setSelect($select[$class]); |
||
272 | $query->setOrderBy(array()); |
||
273 | |||
274 | $querySQLs[] = $query->sql($parameters); |
||
275 | $queryParameters = array_merge($queryParameters, $parameters); |
||
276 | |||
277 | $totalCount += $query->unlimitedRowCount(); |
||
278 | } |
||
279 | $fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy LIMIT $limit"; |
||
280 | |||
281 | // Get records |
||
282 | $records = $this->preparedQuery($fullQuery, $queryParameters); |
||
283 | |||
284 | $objects = array(); |
||
285 | |||
286 | foreach ($records as $record) { |
||
287 | $objects[] = new $record['ClassName']($record); |
||
288 | } |
||
289 | |||
290 | $list = new PaginatedList(new ArrayList($objects)); |
||
291 | $list->setPageStart($start); |
||
292 | $list->setPageLength($pageLength); |
||
293 | $list->setTotalItems($totalCount); |
||
294 | |||
295 | // The list has already been limited by the query above |
||
296 | $list->setLimitItems(false); |
||
297 | |||
298 | return $list; |
||
299 | } |
||
527 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths