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 |
||
15 | class GithubInitCommand extends LlumCommand |
||
16 | { |
||
17 | |||
18 | use GithubCommand; |
||
19 | |||
20 | /** |
||
21 | * Symfony console output. |
||
22 | * |
||
23 | * @var OutputInterface |
||
24 | */ |
||
25 | protected $output; |
||
26 | |||
27 | /** |
||
28 | * Llum rc file parser. |
||
29 | * |
||
30 | * @var LlumRCParser |
||
31 | */ |
||
32 | protected $parser; |
||
33 | |||
34 | /** |
||
35 | * Command name. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $commandName = 'github:init'; |
||
40 | |||
41 | /** |
||
42 | * Command description. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $commandDescription = 'Initializes current folder as a new git project'; |
||
47 | |||
48 | /** |
||
49 | * Method to execute. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $method = 'gitHubInitRepo'; |
||
54 | |||
55 | /** |
||
56 | * GithubInitCommand constructor. |
||
57 | * |
||
58 | * @param LlumRCParser $parser |
||
59 | */ |
||
60 | public function __construct(LlumRCParser $parser) |
||
65 | |||
66 | /** |
||
67 | * Github init repo command. |
||
68 | * |
||
69 | * @param InputInterface $input |
||
70 | * @param OutputInterface $output |
||
71 | */ |
||
72 | public function gitHubInitRepo(InputInterface $input, OutputInterface $output) |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Get github repo URL. |
||
87 | * |
||
88 | * @param OutputInterface $output |
||
89 | * @param InputInterface $input |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getRepoURL(InputInterface $input, OutputInterface $output) |
||
96 | |||
97 | /** |
||
98 | * Obtain repo name. |
||
99 | * |
||
100 | * @param InputInterface $input |
||
101 | * @return mixed|string |
||
102 | */ |
||
103 | protected function repoName(InputInterface $input) { |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Get github username. |
||
111 | * |
||
112 | * @param InputInterface $input |
||
113 | * @param OutputInterface $output |
||
114 | * @return array|mixed |
||
115 | */ |
||
116 | protected function gitHubUsername(InputInterface $input, OutputInterface $output) |
||
121 | |||
122 | /** |
||
123 | * Get github username from llum config. |
||
124 | * |
||
125 | * @param OutputInterface $output |
||
126 | * @return array |
||
127 | */ |
||
128 | View Code Duplication | protected function getGithubUserNameFromConfig(OutputInterface $output) |
|
136 | |||
137 | /** |
||
138 | * Run git init command. |
||
139 | */ |
||
140 | protected function runGitInit(){ |
||
144 | |||
145 | /** |
||
146 | * Run git add command. |
||
147 | */ |
||
148 | protected function runGitAdd(){ |
||
152 | |||
153 | /** |
||
154 | * Run git commit command. |
||
155 | */ |
||
156 | protected function runGitCommit(){ |
||
160 | |||
161 | /** |
||
162 | * Run llum github:repo command. |
||
163 | */ |
||
164 | protected function runGitCreateRepo() |
||
169 | |||
170 | /** |
||
171 | * Run git remote add origin. |
||
172 | * |
||
173 | * @param $url |
||
174 | */ |
||
175 | protected function runGitRemoteAddOrigin($url){ |
||
179 | |||
180 | /** |
||
181 | * Run git pull. |
||
182 | */ |
||
183 | protected function runGitPull(){ |
||
187 | |||
188 | /** |
||
189 | * Run git push. |
||
190 | */ |
||
191 | protected function runGitPush(){ |
||
195 | |||
196 | /** |
||
197 | * Configure command. |
||
198 | */ |
||
199 | public function configure() |
||
209 | } |
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.