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 |
||
34 | class GroupsController extends AppController |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Edit action. |
||
39 | * |
||
40 | * @return mixed |
||
41 | * |
||
42 | * @throws RolledbackTransactionException |
||
43 | * @throws \Aura\Intl\Exception |
||
44 | */ |
||
45 | public function add() |
||
65 | |||
66 | /** |
||
67 | * Move down group action. |
||
68 | * |
||
69 | * @param int $id Group id. |
||
70 | * @return \Cake\Http\Response|null |
||
71 | */ |
||
72 | public function down($id) |
||
76 | |||
77 | /** |
||
78 | * Edit action. |
||
79 | * |
||
80 | * @param int $id Group id. |
||
81 | * @return \Cake\Http\Response|null |
||
82 | * |
||
83 | * @throws RecordNotFoundException |
||
84 | * @throws InvalidPrimaryKeyException |
||
85 | * @throws RolledbackTransactionException |
||
86 | * @throws \Aura\Intl\Exception |
||
87 | */ |
||
88 | public function edit($id) |
||
108 | |||
109 | /** |
||
110 | * Index action. |
||
111 | * |
||
112 | * @return void |
||
113 | * |
||
114 | * @throws \Aura\Intl\Exception |
||
115 | */ |
||
116 | public function index() |
||
121 | |||
122 | /** |
||
123 | * Initialization hook method. |
||
124 | * |
||
125 | * @return void |
||
126 | * |
||
127 | * @throws \Aura\Intl\Exception |
||
128 | * @throws \JBZoo\Utils\Exception |
||
129 | * @throws \Cake\Core\Exception\Exception When trying to set a key that is invalid. |
||
130 | */ |
||
131 | public function initialize() |
||
140 | |||
141 | /** |
||
142 | * Process action. |
||
143 | * |
||
144 | * @return \Cake\Http\Response|null |
||
145 | * |
||
146 | * @throws \Aura\Intl\Exception |
||
147 | */ |
||
148 | public function process() |
||
153 | |||
154 | /** |
||
155 | * Move up group action. |
||
156 | * |
||
157 | * @param int $id Group id. |
||
158 | * @return \Cake\Http\Response|null |
||
159 | * |
||
160 | * @SuppressWarnings(PHPMD.ShortMethodName) |
||
161 | */ |
||
162 | public function up($id) |
||
166 | } |
||
167 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.