| Conditions | 13 |
| Paths | 408 |
| Total Lines | 77 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 64 | public function actionDashboard( |
||
| 65 | string $sort = 'hitCount|desc', |
||
| 66 | int $page = 1, |
||
| 67 | int $per_page = 20, |
||
| 68 | $filter = '', |
||
| 69 | $siteId = 0, |
||
| 70 | $handled = 'all' |
||
| 71 | ): Response { |
||
| 72 | PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
||
| 73 | $data = []; |
||
| 74 | $sortField = 'hitCount'; |
||
| 75 | $sortType = 'DESC'; |
||
| 76 | // Figure out the sorting type |
||
| 77 | if ($sort !== '') { |
||
| 78 | if (strpos($sort, '|') === false) { |
||
| 79 | $sortField = $sort; |
||
| 80 | } else { |
||
| 81 | list($sortField, $sortType) = explode('|', $sort); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | // Query the db table |
||
| 85 | $offset = ($page - 1) * $per_page; |
||
| 86 | $query = (new Query()) |
||
| 87 | ->from(['{{%retour_stats}}']) |
||
| 88 | ->offset($offset) |
||
| 89 | ->limit($per_page) |
||
| 90 | ->orderBy("{$sortField} {$sortType}"); |
||
| 91 | if ((int)$siteId !== 0) { |
||
| 92 | $query->where(['siteId' => $siteId]); |
||
| 93 | } |
||
| 94 | if ($handled !== 'all') { |
||
| 95 | $query->where(['handledByRetour' => self::HANDLED_MAP[$handled]]); |
||
| 96 | } |
||
| 97 | if ($filter !== '') { |
||
| 98 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
| 99 | $query->orWhere(['like', 'referrerUrl', $filter]); |
||
| 100 | } |
||
| 101 | $stats = $query->all(); |
||
| 102 | if ($stats) { |
||
|
|
|||
| 103 | // Add in the `addLink` field |
||
| 104 | foreach ($stats as &$stat) { |
||
| 105 | $stat['addLink'] = ''; |
||
| 106 | if (!$stat['handledByRetour']) { |
||
| 107 | $encodedUrl = urlencode('/'.ltrim($stat['redirectSrcUrl'], '/')); |
||
| 108 | $stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [ |
||
| 109 | 'defaultUrl' => $encodedUrl |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | // Format the data for the API |
||
| 114 | $data['data'] = $stats; |
||
| 115 | $query = (new Query()) |
||
| 116 | ->from(['{{%retour_stats}}']); |
||
| 117 | if ((int)$siteId !== 0) { |
||
| 118 | $query->where(['siteId' => $siteId]); |
||
| 119 | } |
||
| 120 | if ($handled !== 'all') { |
||
| 121 | $query->where(['handledByRetour' => self::HANDLED_MAP[$handled]]); |
||
| 122 | } |
||
| 123 | if ($filter !== '') { |
||
| 124 | $query->where(['like', 'redirectSrcUrl', $filter]); |
||
| 125 | $query->orWhere(['like', 'referrerUrl', $filter]); |
||
| 126 | } |
||
| 127 | $count = $query->count(); |
||
| 128 | $data['links']['pagination'] = [ |
||
| 129 | 'total' => $count, |
||
| 130 | 'per_page' => $per_page, |
||
| 131 | 'current_page' => $page, |
||
| 132 | 'last_page' => ceil($count / $per_page), |
||
| 133 | 'next_page_url' => null, |
||
| 134 | 'prev_page_url' => null, |
||
| 135 | 'from' => $offset + 1, |
||
| 136 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 137 | ]; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $this->asJson($data); |
||
| 141 | } |
||
| 240 |
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.