| Conditions | 11 |
| Paths | 49 |
| Total Lines | 91 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 28 | protected function main() |
||
| 29 | { |
||
| 30 | $this->setHtmlTitle('Search'); |
||
| 31 | |||
| 32 | $database = $this->getDatabase(); |
||
| 33 | $currentUser = User::getCurrent($database); |
||
| 34 | |||
| 35 | $this->assign('canSearchByComment', $this->barrierTest('byComment', $currentUser)); |
||
| 36 | $this->assign('canSearchByEmail', $this->barrierTest('byEmail', $currentUser)); |
||
| 37 | $this->assign('canSearchByIp', $this->barrierTest('byIp', $currentUser)); |
||
| 38 | $this->assign('canSearchByName', $this->barrierTest('byName', $currentUser)); |
||
| 39 | $this->assign('canSeeNonConfirmed', $this->barrierTest('allowNonConfirmed', $currentUser)); |
||
| 40 | |||
| 41 | $this->setTemplate('search/main.tpl'); |
||
| 42 | |||
| 43 | // Dual-mode page |
||
| 44 | if (WebRequest::getString('type') !== null) { |
||
| 45 | $searchType = WebRequest::getString('type'); |
||
| 46 | $searchTerm = WebRequest::getString('term'); |
||
| 47 | |||
| 48 | $excludeNonConfirmed = true; |
||
| 49 | if ($this->barrierTest('allowNonConfirmed', $currentUser)) { |
||
| 50 | $excludeNonConfirmed = WebRequest::getBoolean('excludeNonConfirmed'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $formParameters = [ |
||
| 54 | 'term' => $searchTerm, |
||
| 55 | 'type' => $searchType, |
||
| 56 | ]; |
||
| 57 | |||
| 58 | if ($excludeNonConfirmed) { |
||
| 59 | $formParameters['excludeNonConfirmed'] = true; |
||
| 60 | } |
||
| 61 | |||
| 62 | $requestSearch = RequestSearchHelper::get($database); |
||
| 63 | $this->setSearchHelper($requestSearch); |
||
| 64 | $this->setupLimits(); |
||
| 65 | |||
| 66 | $validationError = ""; |
||
| 67 | if (!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) { |
||
| 68 | SessionAlert::error($validationError, "Search error"); |
||
| 69 | |||
| 70 | $this->setupPageData(0, $formParameters); |
||
| 71 | $this->assign('hasResultset', false); |
||
| 72 | |||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | // searchType known to be sane from the validate step above |
||
| 77 | if (!$this->barrierTest('by' . ucfirst($searchType), User::getCurrent($this->getDatabase()))) { |
||
|
|
|||
| 78 | // only accessible by url munging, don't care about the UX |
||
| 79 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($excludeNonConfirmed) { |
||
| 83 | $requestSearch->withConfirmedEmail(); |
||
| 84 | } |
||
| 85 | |||
| 86 | switch ($searchType) { |
||
| 87 | case 'name': |
||
| 88 | $this->getNameSearchResults($requestSearch, $searchTerm); |
||
| 89 | break; |
||
| 90 | case 'email': |
||
| 91 | $this->getEmailSearchResults($requestSearch, $searchTerm); |
||
| 92 | break; |
||
| 93 | case 'ip': |
||
| 94 | $this->getIpSearchResults($requestSearch, $searchTerm); |
||
| 95 | break; |
||
| 96 | case 'comment': |
||
| 97 | $this->getCommentSearchResults($requestSearch, $searchTerm); |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** @var Request[] $results */ |
||
| 102 | $results = $requestSearch->getRecordCount($count)->fetch(); |
||
| 103 | $this->setupPageData($count, $formParameters); |
||
| 104 | |||
| 105 | // deal with results |
||
| 106 | $this->assign('requests', $this->prepareRequestData($results)); |
||
| 107 | $this->assign('resultCount', count($results)); |
||
| 108 | $this->assign('hasResultset', true); |
||
| 109 | |||
| 110 | list($defaultSort, $defaultSortDirection) = WebRequest::requestListDefaultSort(); |
||
| 111 | $this->assign('defaultSort', $defaultSort); |
||
| 112 | $this->assign('defaultSortDirection', $defaultSortDirection); |
||
| 113 | } |
||
| 114 | else { |
||
| 115 | $this->assign('type', 'name'); |
||
| 116 | $this->assign('hasResultset', false); |
||
| 117 | $this->assign('limit', 50); |
||
| 118 | $this->assign('excludeNonConfirmed', true); |
||
| 119 | } |
||
| 218 |