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