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 | class BaseClient |
||
18 | { |
||
19 | use HasHttpRequests { |
||
20 | request as performRequest; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @var \Kaylyu\Alipay\Kernel\ServiceContainer |
||
25 | */ |
||
26 | protected $app; |
||
27 | |||
28 | /** |
||
29 | * @var |
||
30 | */ |
||
31 | protected $baseUri; |
||
32 | |||
33 | /** |
||
34 | * BaseClient constructor. |
||
35 | * |
||
36 | * @param \Kaylyu\Alipay\Kernel\ServiceContainer $app |
||
37 | */ |
||
38 | public function __construct(ServiceContainer $app) |
||
42 | |||
43 | /** |
||
44 | * @param $request |
||
45 | * @param null $appAuthToken |
||
46 | * @author kaylv <[email protected]> |
||
47 | * @return array|Collection|string |
||
48 | */ |
||
49 | public function httpPost($request, $appAuthToken = null) |
||
53 | |||
54 | /** |
||
55 | * @param $request |
||
56 | * @param string $method |
||
57 | * @param null $appAuthToken |
||
58 | * @author kaylv <[email protected]> |
||
59 | * @return array|Collection|string |
||
60 | */ |
||
61 | public function request($request, string $method = 'GET', $appAuthToken = null) |
||
86 | |||
87 | /** |
||
88 | * Register Guzzle middlewares. |
||
89 | */ |
||
90 | protected function registerHttpMiddlewares() |
||
95 | |||
96 | /** |
||
97 | * Log the request. |
||
98 | * |
||
99 | * @return \Closure |
||
100 | */ |
||
101 | protected function logMiddleware() |
||
107 | |||
108 | /** |
||
109 | * 初始化AopClient |
||
110 | * @author kaylv <[email protected]> |
||
111 | * @throws Exception |
||
112 | */ |
||
113 | View Code Duplication | private function newAopClient() |
|
162 | |||
163 | /** |
||
164 | * 格式化返回数据 |
||
165 | * @param $response |
||
166 | * @author kaylv <[email protected]> |
||
167 | * @return array|Collection|string |
||
168 | */ |
||
169 | protected function formatResponseToType($response) |
||
188 | } |
||
189 |
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.