| Conditions | 5 |
| Paths | 8 |
| Total Lines | 51 |
| Code Lines | 30 |
| 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 |
||
| 23 | public function getResults($forumHolderID, $query, $order = null, $offset = 0, $limit = 10) |
||
| 24 | { |
||
| 25 | |||
| 26 | //sanitise the query string to avoid XSS (Using the ORM will also help avoid this too). |
||
| 27 | $query = Convert::raw2sql(trim($query)); |
||
| 28 | |||
| 29 | //sanitise the order/sorting as it can be changed by the user in quesry string. |
||
| 30 | $order = Convert::raw2sql(trim($order)); |
||
| 31 | |||
| 32 | //explode the query into the multiple terms to search, supply as an array to pass into the ORM filter. |
||
| 33 | $terms = explode(' ', $query); |
||
| 34 | //Add the original full query as one of the keywords. |
||
| 35 | $terms[] = $query; |
||
| 36 | |||
| 37 | //Get posts (limitation is that it picks up the whole phase rather than a FULLTEXT SEARCH. |
||
| 38 | //We are aiming to keep this as simple as possible). More complex impementations acheived with Solr. |
||
| 39 | //Rquires the post be moderated, then Checks for any match of Author name or Content partial match. |
||
| 40 | //Author name checks the full query whereas Content checks each term for matches. |
||
| 41 | $posts = Post::get() |
||
| 42 | ->filter(array( |
||
| 43 | 'Status' => 'Moderated', //posts my be moderated/visible. |
||
| 44 | 'Forum.ParentID' => $forumHolderID //posts must be from a particular forum section. |
||
| 45 | )) |
||
| 46 | ->filterAny(array( |
||
| 47 | 'Author.Nickname:PartialMatch:nocase' => $query, |
||
| 48 | 'Author.FirstName:PartialMatch:nocase' => $query, |
||
| 49 | 'Author.Surname:PartialMatch:nocase' => $query, |
||
| 50 | 'Content:PartialMatch:nocase' => $terms |
||
| 51 | )) |
||
| 52 | ->leftJoin('ForumThread', 'Post.ThreadID = ForumThread.ID'); |
||
| 53 | |||
| 54 | // Work out what sorting method |
||
| 55 | switch ($order) { |
||
| 56 | case 'newest': |
||
| 57 | $posts = $posts->sort('Created', 'DESC'); |
||
| 58 | break; |
||
| 59 | case 'oldest': |
||
| 60 | break; |
||
| 61 | case 'title': |
||
| 62 | $posts = $posts->sort(array('Thread.Title'=>'ASC')); |
||
| 63 | break; |
||
| 64 | default: |
||
| 65 | $posts = $posts->sort(array( |
||
| 66 | 'Thread.Title'=>'ASC', |
||
| 67 | 'Created' => 'DESC' |
||
| 68 | )); |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | |||
| 72 | return $posts ? $posts: new DataList(); |
||
|
|
|||
| 73 | } |
||
| 74 | |||
| 83 |
This check looks for function calls that miss required arguments.