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 |
||
12 | class UsersController extends AppController |
||
13 | { |
||
14 | |||
15 | 5 | public function beforeFilter(Event $event) |
|
20 | |||
21 | |||
22 | /** |
||
23 | * Index method |
||
24 | * |
||
25 | * @return \Cake\Network\Response|null |
||
26 | */ |
||
27 | 1 | public function index() |
|
28 | { |
||
29 | 1 | $users = $this->paginate($this->Users); |
|
30 | |||
31 | 1 | $this->set(compact('users')); |
|
32 | 1 | $this->set('_serialize', ['users']); |
|
33 | 1 | } |
|
34 | |||
35 | /** |
||
36 | * View method |
||
37 | * |
||
38 | * @param string|null $id User id. |
||
39 | * @return \Cake\Network\Response|null |
||
40 | * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
||
41 | */ |
||
42 | 1 | public function view($id = null) |
|
43 | { |
||
44 | 1 | $user = $this->Users->get($id, [ |
|
45 | 'contain' => [] |
||
46 | 1 | ]); |
|
47 | |||
48 | 1 | $this->set('user', $user); |
|
49 | 1 | $this->set('_serialize', ['user']); |
|
50 | 1 | } |
|
51 | |||
52 | /** |
||
53 | * Add method |
||
54 | * |
||
55 | * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise. |
||
56 | */ |
||
57 | 1 | View Code Duplication | public function add() |
|
|||
58 | { |
||
59 | 1 | $user = $this->Users->newEntity(); |
|
60 | 1 | if ($this->request->is('post')) { |
|
61 | 1 | $user = $this->Users->patchEntity($user, $this->request->data); |
|
62 | 1 | if ($this->Users->save($user)) { |
|
63 | 1 | $this->Flash->success(__('The user has been saved.')); |
|
64 | |||
65 | 1 | return $this->redirect(['action' => 'index']); |
|
66 | } |
||
67 | $this->Flash->error(__('The user could not be saved. Please, try again.')); |
||
68 | } |
||
69 | $this->set(compact('user')); |
||
70 | $this->set('_serialize', ['user']); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Edit method |
||
75 | * |
||
76 | * @param string|null $id User id. |
||
77 | * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise. |
||
78 | * @throws \Cake\Network\Exception\NotFoundException When record not found. |
||
79 | */ |
||
80 | 1 | View Code Duplication | public function edit($id = null) |
81 | { |
||
82 | 1 | $user = $this->Users->get($id, [ |
|
83 | 'contain' => [] |
||
84 | 1 | ]); |
|
85 | 1 | if ($this->request->is(['patch', 'post', 'put'])) { |
|
86 | 1 | $user = $this->Users->patchEntity($user, $this->request->data); |
|
87 | 1 | if ($this->Users->save($user)) { |
|
88 | 1 | $this->Flash->success(__('The user has been saved.')); |
|
89 | |||
90 | 1 | return $this->redirect(['action' => 'index']); |
|
91 | } |
||
92 | $this->Flash->error(__('The user could not be saved. Please, try again.')); |
||
93 | } |
||
94 | 1 | $this->set(compact('user')); |
|
95 | 1 | $this->set('_serialize', ['user']); |
|
96 | 1 | } |
|
97 | |||
98 | /** |
||
99 | * Delete method |
||
100 | * |
||
101 | * @param string|null $id User id. |
||
102 | * @return \Cake\Network\Response|null Redirects to index. |
||
103 | * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
||
104 | */ |
||
105 | View Code Duplication | public function delete($id = null) |
|
106 | { |
||
107 | $this->request->allowMethod(['post', 'delete']); |
||
108 | $user = $this->Users->get($id); |
||
109 | if ($this->Users->delete($user)) { |
||
110 | $this->Flash->success(__('The user has been deleted.')); |
||
111 | } else { |
||
112 | $this->Flash->error(__('The user could not be deleted. Please, try again.')); |
||
113 | } |
||
114 | |||
115 | return $this->redirect(['action' => 'index']); |
||
116 | } |
||
117 | |||
118 | public function login() { |
||
133 | } |
||
134 |
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.