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 |
||
| 13 | View Code Duplication | class OptionsSystemsController extends AppController |
|
|
|
|||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Index method |
||
| 18 | * |
||
| 19 | * @return \Cake\Http\Response|void |
||
| 20 | */ |
||
| 21 | public function index() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * View method |
||
| 30 | * |
||
| 31 | * @param string|null $id Options System id. |
||
| 32 | * @return \Cake\Http\Response|void |
||
| 33 | * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
||
| 34 | */ |
||
| 35 | public function view($id = null) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Add method |
||
| 46 | * |
||
| 47 | * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. |
||
| 48 | */ |
||
| 49 | public function add() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Edit method |
||
| 66 | * |
||
| 67 | * @param string|null $id Options System id. |
||
| 68 | * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. |
||
| 69 | * @throws \Cake\Network\Exception\NotFoundException When record not found. |
||
| 70 | */ |
||
| 71 | public function edit($id = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Delete method |
||
| 90 | * |
||
| 91 | * @param string|null $id Options System id. |
||
| 92 | * @return \Cake\Http\Response|null Redirects to index. |
||
| 93 | * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. |
||
| 94 | */ |
||
| 95 | public function delete($id = null) |
||
| 107 | } |
||
| 108 |
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.