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 GitDispatcher extends Dispatcher { |
||
8 | |||
9 | const ACTION_GIT = 'git'; |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private static $action_types = [ |
||
15 | self::ACTION_GIT |
||
16 | ]; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | public static $allowed_actions = [ |
||
22 | 'update', |
||
23 | 'show' |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var \DNProject |
||
28 | */ |
||
29 | protected $project = null; |
||
30 | |||
31 | /** |
||
32 | * @var \DNEnvironment |
||
33 | */ |
||
34 | protected $environment = null; |
||
35 | |||
36 | public function init() { |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @param \SS_HTTPRequest $request |
||
49 | * |
||
50 | * @return \HTMLText|\SS_HTTPResponse |
||
51 | */ |
||
52 | public function index(\SS_HTTPRequest $request) { |
||
55 | |||
56 | /** |
||
57 | * @param SS_HTTPRequest $request |
||
58 | * @return SS_HTTPResponse |
||
59 | */ |
||
60 | public function update(SS_HTTPRequest $request) { |
||
71 | |||
72 | /** |
||
73 | * @param SS_HTTPRequest $request |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function show(\SS_HTTPRequest $request) { |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | public function Link() { |
||
119 | |||
120 | /** |
||
121 | * @param string $name |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getModel($name = '') { |
||
128 | |||
129 | /** |
||
130 | * @param int $ID |
||
131 | * @return SS_HTTPResponse |
||
132 | */ |
||
133 | protected function getFetch($ID) { |
||
146 | |||
147 | /** |
||
148 | * @return SS_HTTPResponse |
||
149 | */ |
||
150 | View Code Duplication | protected function createFetch() { |
|
167 | |||
168 | /** |
||
169 | * @param $project |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | protected function getGitBranches($project) { |
||
183 | |||
184 | /** |
||
185 | * @param $project |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | protected function getGitTags($project) { |
||
199 | |||
200 | /** |
||
201 | * @param $project |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function getGitPrevDeploys($project) { |
||
232 | |||
233 | } |
||
234 |
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.