Conditions | 7 |
Paths | 6 |
Total Lines | 53 |
Code Lines | 31 |
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 |
||
129 | final public function search($keyword) |
||
130 | { |
||
131 | if (!is_string($keyword)) { |
||
132 | throw new InvalidArgumentException( |
||
133 | 'Search keyword must be a string.' |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | if ($keyword == '') { |
||
138 | throw new InvalidArgumentException( |
||
139 | 'Keyword can not be empty.' |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | // Reset results |
||
144 | $this->results = []; |
||
145 | |||
146 | $searchConfig = $this->searchConfig(); |
||
147 | |||
148 | if (!isset($searchConfig['searches'])) { |
||
149 | throw new InvalidArgumentException( |
||
150 | 'No searches defined in search config.' |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | $numResults = 0; |
||
155 | $searchObjects = $searchConfig['searches']; |
||
156 | |||
157 | foreach ($searchObjects as $searchIdent => $searchObj) { |
||
158 | if ($searchObj instanceof SearchInterface) { |
||
159 | // Run search from Search object |
||
160 | $results = $searchObj->search($keyword); |
||
161 | } else { |
||
162 | $searchOptions = array_merge($searchObj, [ |
||
163 | 'logger' => $this->logger, |
||
164 | 'model_factory' => $this->modelFactory() |
||
165 | ]); |
||
166 | $search = new CustomSearch($searchOptions); |
||
167 | $results = $search->search($keyword); |
||
168 | } |
||
169 | $this->results[$searchIdent] = $results; |
||
170 | $numResults += count($results); |
||
171 | } |
||
172 | |||
173 | $this->searchLog = $this->createLog([ |
||
174 | 'search_ident' => isset($searchConfig['ident']) ? $searchConfig['ident'] : '', |
||
175 | 'keyword' => $keyword, |
||
176 | 'num_results' => $numResults, |
||
177 | 'results' => $this->results |
||
178 | ]); |
||
179 | |||
180 | return $this->results; |
||
181 | } |
||
182 | |||
199 |
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.