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 |
||
7 | class BaseApi |
||
8 | { |
||
9 | protected $module; // 接口模块 |
||
10 | protected $className; |
||
11 | protected $apitype; |
||
12 | |||
13 | protected $AppId; |
||
14 | protected $AppSecret; |
||
15 | |||
16 | /** |
||
17 | * 构造方法 根据类名设置 当前要访问的接口模块. |
||
18 | * |
||
19 | * @author Tian |
||
20 | * |
||
21 | * @date 2015-12-08 |
||
22 | */ |
||
23 | public function __construct() |
||
35 | |||
36 | /** |
||
37 | * 获取AppId |
||
38 | * |
||
39 | * @return string AppId |
||
40 | */ |
||
41 | public static function getAppId() |
||
45 | |||
46 | /** |
||
47 | * 获取AppSecret |
||
48 | * |
||
49 | * @return string AppSecret |
||
50 | */ |
||
51 | public static function getAppSecret() |
||
55 | |||
56 | /** |
||
57 | * get发送数据. |
||
58 | * |
||
59 | * @author Tian |
||
60 | * |
||
61 | * @date 2015-12-08 |
||
62 | * |
||
63 | * @param string $node 接口节点 |
||
64 | * @param array $queryStr 需要携带的查询字符串 |
||
65 | * |
||
66 | * @return bool|array 接口返回的结果 |
||
67 | */ |
||
68 | View Code Duplication | final protected function _get($node, array $queryStr, $arsort = true) |
|
85 | |||
86 | /** |
||
87 | * post发送数据. |
||
88 | * |
||
89 | * @author Tian |
||
90 | * |
||
91 | * @date 2015-12-08 |
||
92 | * |
||
93 | * @param string $node 接口节点 |
||
94 | * @param array $data 需要发送的数据 |
||
95 | * @param bool $jsonEncode 是否转换为jsons数据 |
||
96 | * |
||
97 | * @return bool|array 接口返回的结果 |
||
98 | */ |
||
99 | View Code Duplication | final protected function _post($node, array $data, $jsonEncode = true) |
|
116 | |||
117 | /** |
||
118 | * 设置错误信息. |
||
119 | * |
||
120 | * @author Tian |
||
121 | * |
||
122 | * @date 2015-12-08 |
||
123 | * |
||
124 | * @param string $error 错误信息 |
||
125 | */ |
||
126 | final protected function setError($error) |
||
130 | |||
131 | /** |
||
132 | * 返回错误信息. |
||
133 | * |
||
134 | * @author Tian |
||
135 | * |
||
136 | * @date 2015-12-08 |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | final public function getError() |
||
144 | |||
145 | /** |
||
146 | * 获取api原始返回值 |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | final public function getApiData() |
||
154 | |||
155 | /** |
||
156 | * 缓存方法 |
||
157 | * |
||
158 | * @param string $name 缓存名 |
||
159 | * @param string $value 缓存值 如果不输入值 则根据缓存名返回缓存值. |
||
160 | * @param int $expires 缓存过期时间 默认0 即永不超时. 单位秒 |
||
161 | * |
||
162 | * @return bool|null|string |
||
163 | */ |
||
164 | final public function cache($name, $value = '', $expires = 0) |
||
168 | |||
169 | /** |
||
170 | * 设置post操作的get参数. |
||
171 | * |
||
172 | * @param $name |
||
173 | * @param $value |
||
174 | */ |
||
175 | final public function setPostQueryStr($name, $value) |
||
179 | } |
||
180 |
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.