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 |
||
14 | abstract class Api |
||
15 | { |
||
16 | public $apikey; |
||
17 | |||
18 | /** |
||
19 | * @var bool If Response is json format, Default is true, will decode to array. |
||
20 | */ |
||
21 | protected $isJsonResponse = true; |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | * |
||
26 | * @param string $apikey Api key |
||
27 | */ |
||
28 | 17 | public function __construct($apikey) |
|
32 | |||
33 | /** |
||
34 | * Fetch result. |
||
35 | * |
||
36 | * @param mixed $params |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | abstract public function get($params); |
||
41 | |||
42 | /** |
||
43 | * 准备请求头. |
||
44 | * |
||
45 | * @return array 默认的请头 |
||
46 | */ |
||
47 | 16 | protected function prepareHeaders() |
|
53 | |||
54 | /** |
||
55 | * Fetch result by $address. |
||
56 | * |
||
57 | * @param string $address |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | 16 | final public function fetch($address) |
|
83 | |||
84 | /** |
||
85 | * Parse response to array. |
||
86 | * |
||
87 | * @param mixed $result |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | 11 | View Code Duplication | protected function _parseResponse($result) |
110 | } |
||
111 |
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.