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 |
||
23 | class CLIRunner implements CLIRunnerInterface{ |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | const COMMANDS = [ |
||
29 | // local |
||
30 | 'keypair' => 'getKeypair', |
||
31 | 'hash_email' => 'hashEmail', |
||
32 | 'hash_phone' => 'hashPhone', |
||
33 | # 'encrypt' => '', |
||
|
|||
34 | # 'decrypt' => '', |
||
35 | // network |
||
36 | 'credits' => 'checkCredits', |
||
37 | 'check' => 'checkCapabilities', |
||
38 | 'email2id' => 'getIdByEmail', |
||
39 | 'phone2id' => 'getIdByPhone', |
||
40 | 'id2pubkey' => 'getPubkeyById', |
||
41 | # 'send' => '', |
||
42 | # 'sende2e' => '', |
||
43 | # 'sendimage' => '', |
||
44 | # 'sendfile' => '', |
||
45 | # 'receive' => '', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var \chillerlan\Threema\Crypto\CryptoInterface |
||
50 | */ |
||
51 | protected $cryptoInterface; |
||
52 | |||
53 | /** |
||
54 | * @var \chillerlan\Threema\GatewayOptions |
||
55 | */ |
||
56 | protected $gatewayOptions; |
||
57 | |||
58 | /** |
||
59 | * @var \chillerlan\Threema\Gateway |
||
60 | */ |
||
61 | protected $threemaGateway; |
||
62 | |||
63 | /** |
||
64 | * @var \ReflectionClass |
||
65 | */ |
||
66 | protected $reflection; |
||
67 | |||
68 | /** |
||
69 | * @var array[\ReflectionMethod] |
||
70 | */ |
||
71 | private $CLIRunnerInterfaceMap; |
||
72 | |||
73 | /** |
||
74 | * CLIRunner constructor. |
||
75 | * |
||
76 | * @param \chillerlan\Threema\Crypto\CryptoInterface $cryptoInterface |
||
77 | * @param \chillerlan\Threema\GatewayOptions $gatewayOptions |
||
78 | */ |
||
79 | public function __construct(CryptoInterface $cryptoInterface, GatewayOptions $gatewayOptions){ |
||
89 | |||
90 | /** |
||
91 | * @param array $arguments $_SERVER['argc'] |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function run(array $arguments):string{ |
||
115 | |||
116 | /** |
||
117 | * output a string, wrap at 100 chars |
||
118 | * |
||
119 | * @param string $string string to output |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | protected function log2cli(string $string):string{ |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | * @codeCoverageIgnore |
||
130 | */ |
||
131 | protected function readStdIn(){ |
||
146 | |||
147 | /** |
||
148 | * @param array $params |
||
149 | * |
||
150 | * @return \stdClass |
||
151 | */ |
||
152 | protected function parseDocBlock(array $params):stdClass{ |
||
184 | |||
185 | /** |
||
186 | * @return string |
||
187 | */ |
||
188 | public function help():string{ |
||
210 | |||
211 | /** |
||
212 | * @inheritdoc |
||
213 | */ |
||
214 | public function getKeypair(string $privateKeyFile = null, string $publicKeyFile = null):string{ |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | public function hashEmail(string $email):string{ |
||
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | public function hashPhone(string $phoneNo):string{ |
||
245 | |||
246 | /** |
||
247 | * @inheritdoc |
||
248 | */ |
||
249 | public function checkCredits():string{ |
||
252 | |||
253 | /** |
||
254 | * @inheritdoc |
||
255 | */ |
||
256 | public function checkCapabilities(string $threemaID):string{ |
||
259 | |||
260 | /** |
||
261 | * @inheritdoc |
||
262 | */ |
||
263 | public function getIdByEmail(string $email):string{ |
||
266 | |||
267 | /** |
||
268 | * @inheritdoc |
||
269 | */ |
||
270 | public function getIdByPhone(string $phoneNo):string{ |
||
273 | |||
274 | /** |
||
275 | * @inheritdoc |
||
276 | */ |
||
277 | public function getPubkeyById(string $threemaID):string{ |
||
280 | |||
281 | } |
||
282 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.