Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 21 | class ProjectPermissionController extends Controller |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Show all permissions. |
||
| 25 | * |
||
| 26 | * @param array $values |
||
| 27 | * @param array $errors |
||
| 28 | * |
||
| 29 | * @throws AccessForbiddenException |
||
| 30 | */ |
||
| 31 | public function index(array $values = [], array $errors = []) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Allow everybody. |
||
| 52 | */ |
||
| 53 | public function allowEverybody() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Add user to the project. |
||
| 69 | */ |
||
| 70 | public function addUser() |
||
| 71 | { |
||
| 72 | $project = $this->getProject(); |
||
| 73 | $values = $this->request->getValues(); |
||
| 74 | |||
| 75 | View Code Duplication | if (empty($values['user_id'])) { |
|
| 76 | $this->flash->failure(t('User not found.')); |
||
| 77 | } elseif ($this->projectUserRoleModel->addUser($values['project_id'], $values['user_id'], $values['role'])) { |
||
| 78 | $this->flash->success(t('Project updated successfully.')); |
||
| 79 | } else { |
||
| 80 | $this->flash->failure(t('Unable to update this project.')); |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->response->redirect($this->helper->url->to('Manage/ProjectPermissionController', 'index', ['project_id' => $project['id']])); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Revoke user access. |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function removeUser() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Change user role. |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function changeUserRole() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Add group to the project. |
||
| 130 | */ |
||
| 131 | public function addGroup() |
||
| 132 | { |
||
| 133 | $project = $this->getProject(); |
||
| 134 | $values = $this->request->getValues(); |
||
| 135 | |||
| 136 | if (empty($values['group_id']) && !empty($values['external_id'])) { |
||
| 137 | $values['group_id'] = $this->groupModel->getOrCreateExternalGroupId($values['name'], $values['external_id']); |
||
| 138 | } |
||
| 139 | |||
| 140 | View Code Duplication | if (!empty($values['group_id']) && $this->projectGroupRoleModel->addGroup($project['id'], $values['group_id'], $values['role'])) { |
|
| 141 | $this->flash->success(t('Project updated successfully.')); |
||
| 142 | } else { |
||
| 143 | $this->flash->failure(t('Unable to update this project.')); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->response->redirect($this->helper->url->to('Manage/ProjectPermissionController', 'index', ['project_id' => $project['id']])); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Revoke group access. |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function removeGroup() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Change group role. |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function changeGroupRole() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Permissions are only available for team projects. |
||
| 183 | * |
||
| 184 | * @param int $project_id Default project id |
||
| 185 | * |
||
| 186 | * @throws AccessForbiddenException |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | protected function getProject($project_id = 0) |
||
| 200 | } |
||
| 201 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.