Conditions | 12 |
Paths | 386 |
Total Lines | 126 |
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 |
||
105 | public function searchEngine($classesToSearch, $keywords, $start, $pageLength, $sortBy = "Relevance DESC", |
||
106 | $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false |
||
107 | ) { |
||
108 | if (!class_exists('SiteTree')) { |
||
109 | throw new Exception('MySQLDatabase->searchEngine() requires "SiteTree" class'); |
||
110 | } |
||
111 | if (!class_exists('File')) { |
||
112 | throw new Exception('MySQLDatabase->searchEngine() requires "File" class'); |
||
113 | } |
||
114 | $start = (int)$start; |
||
115 | $pageLength = (int)$pageLength; |
||
116 | |||
117 | $keywords = $this->escapeString($keywords); |
||
118 | $htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES, 'UTF-8'); |
||
119 | |||
120 | $extraFilters = array('SiteTree' => '', 'File' => ''); |
||
121 | |||
122 | if ($booleanSearch) $boolean = "IN BOOLEAN MODE"; |
||
123 | |||
124 | if ($extraFilter) { |
||
125 | $extraFilters['SiteTree'] = " AND $extraFilter"; |
||
126 | |||
127 | if ($alternativeFileFilter) |
||
128 | $extraFilters['File'] = " AND $alternativeFileFilter"; |
||
129 | else $extraFilters['File'] = $extraFilters['SiteTree']; |
||
130 | } |
||
131 | |||
132 | // Always ensure that only pages with ShowInSearch = 1 can be searched |
||
133 | $extraFilters['SiteTree'] .= " AND ShowInSearch <> 0"; |
||
134 | |||
135 | // File.ShowInSearch was added later, keep the database driver backwards compatible |
||
136 | // by checking for its existence first |
||
137 | $fields = $this->fieldList('File'); |
||
|
|||
138 | if (array_key_exists('ShowInSearch', $fields)) |
||
139 | $extraFilters['File'] .= " AND ShowInSearch <> 0"; |
||
140 | |||
141 | $limit = $start . ", " . $pageLength; |
||
142 | |||
143 | $notMatch = $invertedMatch |
||
144 | ? "NOT " |
||
145 | : ""; |
||
146 | if ($keywords) { |
||
147 | $match['SiteTree'] = " |
||
148 | MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$keywords' $boolean) |
||
149 | + MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$htmlEntityKeywords' $boolean) |
||
150 | "; |
||
151 | $match['File'] = "MATCH (Filename, Title, Content) AGAINST ('$keywords' $boolean) AND ClassName = 'File'"; |
||
152 | |||
153 | // We make the relevance search by converting a boolean mode search into a normal one |
||
154 | $relevanceKeywords = str_replace(array('*', '+', '-'), '', $keywords); |
||
155 | $htmlEntityRelevanceKeywords = str_replace(array('*', '+', '-'), '', $htmlEntityKeywords); |
||
156 | $relevance['SiteTree'] = "MATCH (Title, MenuTitle, Content, MetaDescription) " |
||
157 | . "AGAINST ('$relevanceKeywords') " |
||
158 | . "+ MATCH (Title, MenuTitle, Content, MetaDescription) AGAINST ('$htmlEntityRelevanceKeywords')"; |
||
159 | $relevance['File'] = "MATCH (Filename, Title, Content) AGAINST ('$relevanceKeywords')"; |
||
160 | } else { |
||
161 | $relevance['SiteTree'] = $relevance['File'] = 1; |
||
162 | $match['SiteTree'] = $match['File'] = "1 = 1"; |
||
163 | } |
||
164 | |||
165 | // Generate initial DataLists and base table names |
||
166 | $lists = array(); |
||
167 | $baseClasses = array('SiteTree' => '', 'File' => ''); |
||
168 | foreach ($classesToSearch as $class) { |
||
169 | $lists[$class] = DataList::create($class)->where($notMatch . $match[$class] . $extraFilters[$class], ""); |
||
170 | $baseClasses[$class] = '"' . $class . '"'; |
||
171 | } |
||
172 | |||
173 | $charset = Config::inst()->get('MySQLDatabase', 'charset'); |
||
174 | |||
175 | // Make column selection lists |
||
176 | $select = array( |
||
177 | 'SiteTree' => array( |
||
178 | "ClassName", "$baseClasses[SiteTree].\"ID\"", "ParentID", |
||
179 | "Title", "MenuTitle", "URLSegment", "Content", |
||
180 | "LastEdited", "Created", |
||
181 | "Filename" => "_{$charset}''", "Name" => "_{$charset}''", |
||
182 | "Relevance" => $relevance['SiteTree'], "CanViewType" |
||
183 | ), |
||
184 | 'File' => array( |
||
185 | "ClassName", "$baseClasses[File].\"ID\"", "ParentID", |
||
186 | "Title", "MenuTitle" => "_{$charset}''", "URLSegment" => "_{$charset}''", "Content", |
||
187 | "LastEdited", "Created", |
||
188 | "Filename", "Name", |
||
189 | "Relevance" => $relevance['File'], "CanViewType" => "NULL" |
||
190 | ), |
||
191 | ); |
||
192 | |||
193 | // Process and combine queries |
||
194 | $querySQLs = array(); |
||
195 | $queryParameters = array(); |
||
196 | $totalCount = 0; |
||
197 | foreach ($lists as $class => $list) { |
||
198 | $query = $list->dataQuery()->query(); |
||
199 | |||
200 | // There's no need to do all that joining |
||
201 | $query->setFrom(array(str_replace(array('"', '`'), '', $baseClasses[$class]) => $baseClasses[$class])); |
||
202 | $query->setSelect($select[$class]); |
||
203 | $query->setOrderBy(array()); |
||
204 | |||
205 | $querySQLs[] = $query->sql($parameters); |
||
206 | $queryParameters = array_merge($queryParameters, $parameters); |
||
207 | |||
208 | $totalCount += $query->unlimitedRowCount(); |
||
209 | } |
||
210 | $fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy LIMIT $limit"; |
||
211 | |||
212 | // Get records |
||
213 | $records = $this->preparedQuery($fullQuery, $queryParameters); |
||
214 | |||
215 | $objects = array(); |
||
216 | |||
217 | foreach ($records as $record) { |
||
218 | $objects[] = new $record['ClassName']($record); |
||
219 | } |
||
220 | |||
221 | $list = new PaginatedList(new ArrayList($objects)); |
||
222 | $list->setPageStart($start); |
||
223 | $list->setPageLength($pageLength); |
||
224 | $list->setTotalItems($totalCount); |
||
225 | |||
226 | // The list has already been limited by the query above |
||
227 | $list->setLimitItems(false); |
||
228 | |||
229 | return $list; |
||
230 | } |
||
231 | |||
385 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.