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:
Complex classes like ProjectRoleHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProjectRoleHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class ProjectRoleHelper extends Base |
||
23 | { |
||
24 | /** |
||
25 | * Get project role for the current user. |
||
26 | * |
||
27 | * @param int $project_id |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public function getProjectUserRole($project_id) |
||
35 | |||
36 | /** |
||
37 | * Return true if the task can be moved by the logged user. |
||
38 | * |
||
39 | * @param array $task |
||
40 | * |
||
41 | * @return bool |
||
42 | */ |
||
43 | public function isDraggable(array &$task) |
||
51 | |||
52 | /** |
||
53 | * Return true is the column is sortable. |
||
54 | * |
||
55 | * @param int $project_id |
||
56 | * @param int $column_id |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function isSortableColumn($project_id, $column_id) |
||
78 | |||
79 | /** |
||
80 | * Check if the user can move a task. |
||
81 | * |
||
82 | * @param int $project_id |
||
83 | * @param int $src_column_id |
||
84 | * @param int $dst_column_id |
||
85 | * |
||
86 | * @return bool|int |
||
87 | */ |
||
88 | public function canMoveTask($project_id, $src_column_id, $dst_column_id) |
||
114 | |||
115 | /** |
||
116 | * Return true if the user can create a task for the given column. |
||
117 | * |
||
118 | * @param int $project_id |
||
119 | * @param int $column_id |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | View Code Duplication | public function canCreateTaskInColumn($project_id, $column_id) |
|
135 | |||
136 | /** |
||
137 | * Return true if the user can create a task for the given column. |
||
138 | * |
||
139 | * @param int $project_id |
||
140 | * @param int $column_id |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | View Code Duplication | public function canChangeTaskStatusInColumn($project_id, $column_id) |
|
156 | |||
157 | /** |
||
158 | * Return true if the user can remove a task. |
||
159 | * |
||
160 | * Regular users can't remove tasks from other people |
||
161 | * |
||
162 | * @public |
||
163 | * |
||
164 | * @param array $task |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function canRemoveTask(array $task) |
||
180 | |||
181 | /** |
||
182 | * Check project access. |
||
183 | * |
||
184 | * @param string $controller |
||
185 | * @param string $action |
||
186 | * @param int $project_id |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function checkProjectAccess($controller, $action, $project_id) |
||
214 | |||
215 | /** |
||
216 | * Check authorization for a custom project role to change the task status. |
||
217 | * |
||
218 | * @param int $project_id |
||
219 | * @param int $column_id |
||
220 | * @param string $role |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | View Code Duplication | protected function isAllowedToChangeTaskStatus($project_id, $column_id, $role) |
|
248 | |||
249 | /** |
||
250 | * Check authorization for a custom project role to create a task. |
||
251 | * |
||
252 | * @param int $project_id |
||
253 | * @param int $column_id |
||
254 | * @param string $role |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | View Code Duplication | protected function isAllowedToCreateTask($project_id, $column_id, $role) |
|
282 | |||
283 | /** |
||
284 | * Check if the role can move task in the given project. |
||
285 | * |
||
286 | * @param int $project_id |
||
287 | * @param string $role |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | protected function isAllowedToMoveTask($project_id, $role) |
||
303 | } |
||
304 |
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@property
annotation 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.