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 namespace Anomaly\UsersModule\Role\Permission; |
||
17 | View Code Duplication | class PermissionFormBuilder extends FormBuilder |
|
|
|||
18 | { |
||
19 | |||
20 | /** |
||
21 | * The addon to modify |
||
22 | * permissions for. |
||
23 | * |
||
24 | * @var null|Addon |
||
25 | */ |
||
26 | protected $addon = null; |
||
27 | |||
28 | /** |
||
29 | * No model needed. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $model = false; |
||
34 | |||
35 | /** |
||
36 | * Disable saving. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $save = false; |
||
41 | |||
42 | /** |
||
43 | * The form actions. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $actions = [ |
||
48 | 'save' => [ |
||
49 | 'href' => 'admin/users/permissions/{request.route.parameters.id}', |
||
50 | ], |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * The form options. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $options = [ |
||
59 | 'breadcrumb' => false, |
||
60 | 'permission' => 'anomaly.module.users::users.permissions', |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Fired when builder is ready to build. |
||
65 | * |
||
66 | * @param RoleRepositoryInterface $roles |
||
67 | * @param BreadcrumbCollection $breadcrumbs |
||
68 | * @param MessageBag $messages |
||
69 | * @param Redirector $redirect |
||
70 | * @return \Illuminate\Http\RedirectResponse |
||
71 | */ |
||
72 | public function onReady( |
||
73 | RoleRepositoryInterface $roles, |
||
74 | BreadcrumbCollection $breadcrumbs, |
||
75 | MessageBag $messages, |
||
76 | Redirector $redirect |
||
77 | ) { |
||
78 | $this->setEntry($role = $roles->find($this->getEntry())); |
||
79 | |||
80 | if ($role->getSlug() === 'admin') { |
||
81 | |||
82 | $messages->warning('anomaly.module.users::warning.modify_admin_role'); |
||
83 | |||
84 | $this->setFormResponse($redirect->to('admin/users/roles')); |
||
85 | |||
86 | return; |
||
87 | } |
||
88 | |||
89 | $breadcrumbs->add($role->getName(), 'admin/users/roles/edit/' . $role->getId()); |
||
90 | $breadcrumbs->add( |
||
91 | 'anomaly.module.users::breadcrumb.permissions', |
||
92 | 'admin/users/roles/permissions/' . $role->getId() |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * If nothing is posted then |
||
98 | * the role gets no permissions. |
||
99 | * |
||
100 | * @param RoleRepositoryInterface $roles |
||
101 | */ |
||
102 | public function onPost(RoleRepositoryInterface $roles) |
||
108 | |||
109 | /** |
||
110 | * Get the addon. |
||
111 | * |
||
112 | * @return null|Addon |
||
113 | */ |
||
114 | public function getAddon() |
||
118 | |||
119 | /** |
||
120 | * Set the addon. |
||
121 | * |
||
122 | * @param Addon $addon |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setAddon(Addon $addon) |
||
131 | } |
||
132 |
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.