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 |
||
13 | class Prpcrypt |
||
14 | { |
||
15 | protected $AESKey; |
||
16 | protected $blockSize; |
||
17 | |||
18 | public function __construct($k) |
||
24 | |||
25 | /** |
||
26 | * 对明文进行加密 |
||
27 | * |
||
28 | * @param string $text 需要加密的明文 |
||
29 | * |
||
30 | * @param string $appId |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | public function encrypt($text, $appId) |
||
54 | |||
55 | /** |
||
56 | * @param string $encrypted 需要解密的密文 |
||
57 | * @param string $appId APPID |
||
58 | * |
||
59 | * @return array|string |
||
60 | */ |
||
61 | public function decrypt($encrypted, $appId) |
||
103 | |||
104 | /** |
||
105 | * 随机生成16位字符串 |
||
106 | * |
||
107 | * @return string 生成的字符串 |
||
108 | */ |
||
109 | View Code Duplication | public function getRandomStr() |
|
120 | |||
121 | /** |
||
122 | * Return AESKey. |
||
123 | * |
||
124 | * @return string |
||
125 | * |
||
126 | * @throws Exception |
||
127 | */ |
||
128 | protected function getAESKey() |
||
140 | |||
141 | /** |
||
142 | * Decode string. |
||
143 | * |
||
144 | * @param string $decrypted |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | View Code Duplication | public function decode($decrypted) |
|
158 | |||
159 | /** |
||
160 | * Encode string. |
||
161 | * |
||
162 | * @param string $text |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function encode($text) |
||
182 | |||
183 | } |
||
184 |
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.