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 |
||
20 | class ProjectRoleController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * Show roles and permissions. |
||
24 | */ |
||
25 | View Code Duplication | public function show() |
|
|
|||
26 | { |
||
27 | $project = $this->getProject(); |
||
28 | |||
29 | $this->response->html($this->helper->layout->project('project/role/show', [ |
||
30 | 'project' => $project, |
||
31 | 'roles' => $this->projectRoleModel->getAllWithRestrictions($project['id']), |
||
32 | 'title' => t('Custom Project Roles'), |
||
33 | ])); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Show form to create new role. |
||
38 | * |
||
39 | * @param array $values |
||
40 | * @param array $errors |
||
41 | * |
||
42 | * @throws AccessForbiddenException |
||
43 | */ |
||
44 | View Code Duplication | public function create(array $values = [], array $errors = []) |
|
45 | { |
||
46 | $project = $this->getProject(); |
||
47 | |||
48 | $this->response->html($this->template->render('project/role/create', [ |
||
49 | 'project' => $project, |
||
50 | 'values' => $values + ['project_id' => $project['id']], |
||
51 | 'errors' => $errors, |
||
52 | ])); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Save new role. |
||
57 | */ |
||
58 | public function store() |
||
79 | |||
80 | /** |
||
81 | * Show form to change existing role. |
||
82 | * |
||
83 | * @param array $values |
||
84 | * @param array $errors |
||
85 | * |
||
86 | * @throws AccessForbiddenException |
||
87 | */ |
||
88 | View Code Duplication | public function edit(array $values = [], array $errors = []) |
|
103 | |||
104 | /** |
||
105 | * Update role. |
||
106 | */ |
||
107 | public function update() |
||
129 | |||
130 | /** |
||
131 | * Remove a custom role. |
||
132 | */ |
||
133 | View Code Duplication | public function remove() |
|
156 | } |
||
157 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.