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 |
||
18 | class GithubRepoCommand extends LlumCommand |
||
19 | { |
||
20 | |||
21 | use GithubCommand; |
||
22 | |||
23 | /** |
||
24 | * Github api service class. |
||
25 | * |
||
26 | * @var GithubAPI |
||
27 | */ |
||
28 | protected $api; |
||
29 | |||
30 | /** |
||
31 | * Llum rc file parser. |
||
32 | * |
||
33 | * @var LlumRCParser |
||
34 | */ |
||
35 | protected $parser; |
||
36 | |||
37 | /** |
||
38 | * Command name. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $commandName = 'github:repo'; |
||
43 | |||
44 | /** |
||
45 | * Command description. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $commandDescription = 'Creates a github repo'; |
||
50 | |||
51 | /** |
||
52 | * Method to execute. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $method = 'githubCreateRepo'; |
||
57 | |||
58 | /** |
||
59 | * GithubRepoCommand constructor. |
||
60 | * @param GithubAPI $api |
||
61 | * @param LlumRCParser $parser |
||
62 | */ |
||
63 | public function __construct(GithubAPI $api, LlumRCParser $parser) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Github create repo command. |
||
73 | * |
||
74 | * @param InputInterface $input |
||
75 | * @param OutputInterface $output |
||
76 | */ |
||
77 | public function githubCreateRepo(InputInterface $input, OutputInterface $output) |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Show error. |
||
97 | * @param ClientException $ce |
||
98 | * @param OutputInterface $output |
||
99 | */ |
||
100 | protected function showError(ClientException $ce, OutputInterface $output) |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Get credentials. |
||
113 | * |
||
114 | * @param OutputInterface $input |
||
115 | * @return array |
||
116 | */ |
||
117 | View Code Duplication | public function getCredentials(OutputInterface $output) |
|
125 | |||
126 | /** |
||
127 | * Obtain repo name. |
||
128 | * |
||
129 | * @param InputInterface $input |
||
130 | * @return mixed|string |
||
131 | */ |
||
132 | protected function repoName(InputInterface $input) { |
||
136 | |||
137 | /** |
||
138 | * Obtain repo description. |
||
139 | * |
||
140 | * @param InputInterface $input |
||
141 | * @return mixed|string |
||
142 | */ |
||
143 | protected function repoDescription(InputInterface $input) { |
||
147 | |||
148 | /** |
||
149 | * Configure command. |
||
150 | */ |
||
151 | public function configure() |
||
161 | |||
162 | } |
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.