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 |
||
7 | class DeployDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_DEPLOY = 'deploy'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $action_types = [ |
||
15 | self::ACTION_DEPLOY |
||
16 | ]; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public static $allowed_actions = [ |
||
22 | 'history', |
||
23 | 'start', |
||
24 | 'log' |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var \DNProject |
||
29 | */ |
||
30 | protected $project = null; |
||
31 | |||
32 | /** |
||
33 | * @var \DNEnvironment |
||
34 | */ |
||
35 | protected $environment = null; |
||
36 | |||
37 | View Code Duplication | public function init() { |
|
52 | |||
53 | /** |
||
54 | * |
||
55 | * @param \SS_HTTPRequest $request |
||
56 | * |
||
57 | * @return \HTMLText|\SS_HTTPResponse |
||
58 | */ |
||
59 | public function index(\SS_HTTPRequest $request) { |
||
62 | |||
63 | /** |
||
64 | * @return SS_HTTPResponse |
||
65 | */ |
||
66 | public function history(SS_HTTPRequest $request) { |
||
106 | |||
107 | /** |
||
108 | * |
||
109 | * @param SS_HTTPRequest $request |
||
110 | * |
||
111 | * @return SS_HTTPResponse |
||
112 | * @throws ValidationException |
||
113 | * @throws null |
||
114 | */ |
||
115 | public function start(SS_HTTPRequest $request) { |
||
145 | |||
146 | /** |
||
147 | * Action - Get the latest deploy log |
||
148 | * |
||
149 | * @param SS_HTTPRequest $request |
||
150 | * |
||
151 | * @return string |
||
152 | * @throws SS_HTTPResponse_Exception |
||
153 | */ |
||
154 | public function log(SS_HTTPRequest $request) { |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | public function Link() { |
||
188 | |||
189 | /** |
||
190 | * @param string $name |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | public function getModel($name = '') { |
||
197 | |||
198 | } |
||
199 |
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.