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 |
||
29 | abstract class BaseAPI |
||
30 | { |
||
31 | |||
32 | public $options = []; |
||
33 | |||
34 | private static $instance; |
||
35 | |||
36 | /** |
||
37 | * Create api |
||
38 | * |
||
39 | * @param array $options local api options |
||
40 | */ |
||
41 | public function __construct($options = []) |
||
45 | |||
46 | /** |
||
47 | * Set or retrieve option value |
||
48 | * |
||
49 | * @param string or array $key the option key to set/retrieve, or an array of options to set |
||
50 | * @param object $value value to store |
||
51 | * |
||
52 | * @return object if key is string and value is null, return options[key] value |
||
53 | */ |
||
54 | View Code Duplication | public function option($key, $value = null) |
|
67 | |||
68 | /** |
||
69 | * Submit asynchronous get request |
||
70 | * |
||
71 | * @param string $url request url |
||
72 | * @param array $fields response fields |
||
73 | * |
||
74 | * @return GuzzleHttp\Promise Request promise |
||
75 | */ |
||
76 | public function getAsync($url, $fields = []) |
||
90 | |||
91 | /** |
||
92 | * Submit get request |
||
93 | * |
||
94 | * @param string $url request url |
||
95 | * @param array $fields response fields |
||
96 | * |
||
97 | * @return GuzzleHttp\Promise or json or array response |
||
98 | */ |
||
99 | public function get($url, $fields = []) |
||
110 | |||
111 | /** |
||
112 | * Convert guzzle response to data |
||
113 | * |
||
114 | * @param Response $response GuzzleHttp\Response |
||
115 | * |
||
116 | * @return string or array data |
||
117 | */ |
||
118 | public function format($response) |
||
128 | |||
129 | /** |
||
130 | * Get API instance |
||
131 | * |
||
132 | * @param string $key API class name |
||
133 | * @param array $options API options |
||
134 | * |
||
135 | * @return object API instance |
||
136 | */ |
||
137 | public static function getInstance($key, $options) |
||
146 | |||
147 | /** |
||
148 | * Copy display fields to url string |
||
149 | * |
||
150 | * @param array $fields response fields |
||
151 | * @param string $url request url |
||
152 | * |
||
153 | * @return string url with ?fields=field1,field2 |
||
154 | */ |
||
155 | public function addFieldsToUrl($fields, $url) |
||
165 | |||
166 | /** |
||
167 | * Create API Guzzle Client |
||
168 | * |
||
169 | * @param string $key Sunlight Foundation API key |
||
170 | * |
||
171 | * @return null |
||
172 | */ |
||
173 | abstract protected function setClient($key); |
||
174 | |||
175 | /** |
||
176 | * Get API Guzzle Client |
||
177 | * |
||
178 | * @return GuzzleHttp\Client API Client |
||
179 | */ |
||
180 | abstract protected function getClient(); |
||
181 | } |
||
182 |
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.