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 |
||
17 | View Code Duplication | class LuosimaoGateway extends Gateway |
|
|
|||
18 | { |
||
19 | use HasHttpRequest; |
||
20 | |||
21 | const ENDPOINT_TEMPLATE = 'https://%s.luosimao.com/%s/%s.%s'; |
||
22 | const ENDPOINT_VERSION = 'v1'; |
||
23 | const ENDPOINT_FORMAT = 'json'; |
||
24 | |||
25 | /** |
||
26 | * Send a short message. |
||
27 | * |
||
28 | * @param string|int $to |
||
29 | * @param string $message |
||
30 | * @param array $data |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | public function send($to, $message, array $data = []) |
||
45 | |||
46 | /** |
||
47 | * Build endpoint url. |
||
48 | * |
||
49 | * @param string $type |
||
50 | * @param string $function |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | protected function buildEndpoint($type, $function) |
||
58 | } |
||
59 |
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.