| Conditions | 20 |
| Paths | 512 |
| Total Lines | 60 |
| Code Lines | 32 |
| Lines | 20 |
| Ratio | 33.33 % |
| 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 |
||
| 118 | public function getResults($pageLength = null, $data = null){ |
||
| 119 | // legacy usage: $data was defaulting to $_REQUEST, parameter not passed in doc.silverstripe.org tutorials |
||
| 120 | if(!isset($data) || !is_array($data)) $data = $_REQUEST; |
||
| 121 | |||
| 122 | // set language (if present) |
||
| 123 | View Code Duplication | if(class_exists('Translatable')) { |
|
| 124 | if(SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { |
||
| 125 | if($data['searchlocale'] == "ALL") { |
||
| 126 | Translatable::disable_locale_filter(); |
||
| 127 | } else { |
||
| 128 | $origLocale = Translatable::get_current_locale(); |
||
| 129 | |||
| 130 | Translatable::set_current_locale($data['searchlocale']); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $keywords = $data['Search']; |
||
| 136 | |||
| 137 | $andProcessor = create_function('$matches',' |
||
| 138 | return " +" . $matches[2] . " +" . $matches[4] . " "; |
||
| 139 | '); |
||
| 140 | $notProcessor = create_function('$matches', ' |
||
| 141 | return " -" . $matches[3]; |
||
| 142 | '); |
||
| 143 | |||
| 144 | $keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); |
||
| 145 | $keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); |
||
| 146 | $keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); |
||
| 147 | $keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); |
||
| 148 | |||
| 149 | $keywords = $this->addStarsToKeywords($keywords); |
||
| 150 | |||
| 151 | if(!$pageLength) $pageLength = $this->pageLength; |
||
| 152 | $start = isset($_GET['start']) ? (int)$_GET['start'] : 0; |
||
| 153 | |||
| 154 | if(strpos($keywords, '"') !== false || strpos($keywords, '+') !== false || strpos($keywords, '-') !== false || strpos($keywords, '*') !== false) { |
||
| 155 | $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", true); |
||
| 156 | } else { |
||
| 157 | $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength); |
||
| 158 | } |
||
| 159 | |||
| 160 | // filter by permission |
||
| 161 | if($results) foreach($results as $result) { |
||
| 162 | if(!$result->canView()) $results->remove($result); |
||
| 163 | } |
||
| 164 | |||
| 165 | // reset locale |
||
| 166 | View Code Duplication | if(class_exists('Translatable')) { |
|
| 167 | if(SiteTree::singleton()->hasExtension('Translatable') && isset($data['searchlocale'])) { |
||
| 168 | if($data['searchlocale'] == "ALL") { |
||
| 169 | Translatable::enable_locale_filter(); |
||
| 170 | } else { |
||
| 171 | Translatable::set_current_locale($origLocale); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return $results; |
||
| 177 | } |
||
| 178 | |||
| 237 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.