| Conditions | 18 |
| Paths | 103 |
| Total Lines | 69 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 74 | private function setConditions(App\Db\Query $query, array $fields): App\Db\Query |
||
| 75 | { |
||
| 76 | $qualifiedModuleName = $this->request->getModule(false); |
||
| 77 | $conditions = ['and']; |
||
| 78 | $users = $groups = $roles = $rolesAndSubordinates = []; |
||
| 79 | foreach ($fields as $fieldModel) { |
||
| 80 | $fieldModelName = $fieldModel->getName(); |
||
| 81 | if ($this->request->has($fieldModelName) && '' !== $this->request->get($fieldModelName)) { |
||
| 82 | $value = $this->moduleModel->getValueFromRequest($fieldModelName, $this->request); |
||
| 83 | switch ($fieldModelName) { |
||
| 84 | case 'groupname': |
||
| 85 | $accessibleGroups = (new \App\Db\Query())->select(['groupid', 'groupname'])->from('vtiger_groups')->createCommand()->queryAllByGroup(0); |
||
| 86 | foreach ($accessibleGroups as $groupId => $groupName) { |
||
| 87 | $accessibleGroups[$groupId] = App\Language::translate($groupName, $qualifiedModuleName); |
||
| 88 | } |
||
| 89 | $groupIdsContainName = preg_grep("/{$value}/i", $accessibleGroups); |
||
| 90 | $conditions[] = [$this->moduleModel->baseTable . '.' . $this->baseIndex => array_keys($groupIdsContainName)]; |
||
| 91 | break; |
||
| 92 | case 'description': |
||
| 93 | $conditions[] = ['like', $fieldModel->getColumnName(), $value]; |
||
| 94 | break; |
||
| 95 | case 'modules': |
||
| 96 | $query->innerJoin('vtiger_group2modules', 'vtiger_group2modules.groupid = vtiger_groups.groupid'); |
||
| 97 | $conditions[] = ['tabid' => $value]; |
||
| 98 | break; |
||
| 99 | case 'members': |
||
| 100 | foreach ($value as $memberTypeId) { |
||
| 101 | [$type, $memberId] = explode(':', $memberTypeId); |
||
| 102 | switch ($type) { |
||
| 103 | case 'Users': |
||
| 104 | $users[] = (int) $memberId; |
||
| 105 | break; |
||
| 106 | case 'Groups': |
||
| 107 | $groups[] = (int) $memberId; |
||
| 108 | break; |
||
| 109 | case 'Roles': |
||
| 110 | $roles[] = $memberId; |
||
| 111 | break; |
||
| 112 | case 'RoleAndSubordinates': |
||
| 113 | $rolesAndSubordinates[] = $memberId; |
||
| 114 | break; |
||
| 115 | default: |
||
| 116 | break; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | if ($users) { |
||
| 120 | $query->innerJoin('vtiger_users2group', 'vtiger_users2group.groupid = vtiger_groups.groupid'); |
||
| 121 | $conditions[] = ['userid' => $users]; |
||
| 122 | } |
||
| 123 | if ($groups) { |
||
| 124 | $query->innerJoin('vtiger_group2grouprel', 'vtiger_group2grouprel.groupid = vtiger_groups.groupid'); |
||
| 125 | $conditions[] = ['vtiger_group2grouprel.groupid' => $groups]; |
||
| 126 | } |
||
| 127 | if ($roles) { |
||
| 128 | $query->innerJoin('vtiger_group2role', 'vtiger_group2role.groupid = vtiger_groups.groupid'); |
||
| 129 | $conditions[] = ['vtiger_group2role.roleid' => $roles]; |
||
| 130 | } |
||
| 131 | if ($rolesAndSubordinates) { |
||
| 132 | $query->innerJoin('vtiger_group2rs', 'vtiger_group2rs.groupid = vtiger_groups.groupid'); |
||
| 133 | $conditions[] = ['vtiger_group2rs.roleandsubid' => $rolesAndSubordinates]; |
||
| 134 | } |
||
| 135 | break; |
||
| 136 | default: |
||
| 137 | $conditions[] = [$fieldModel->getColumnName() => $value]; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $query->where($conditions); |
||
| 142 | return $query; |
||
| 143 | } |
||
| 145 |