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 |
||
41 | abstract class AbstractClient implements ClientInterface { |
||
42 | |||
43 | /** |
||
44 | * Array of Statically Bound Tokens for SDK Clients |
||
45 | * - Allows for reinstating objects in multiple areas, without needing to Sign-in |
||
46 | * - Allows for multiple client_id's to be used between SDK Clients |
||
47 | * |
||
48 | * @var array = array( |
||
49 | * $client_id => $token |
||
50 | * ) |
||
51 | */ |
||
52 | protected static $_STORED_TOKENS = array(); |
||
53 | |||
54 | |||
55 | /** |
||
56 | * The configured server domain name/url on the SDK Client |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $server = ''; |
||
60 | |||
61 | /** |
||
62 | * The API Url configured on the SDK Client |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $apiURL = ''; |
||
66 | |||
67 | /** |
||
68 | * The full token object returned by the Login method |
||
69 | * @var \stdClass |
||
70 | */ |
||
71 | protected $token; |
||
72 | |||
73 | /** |
||
74 | * Array of OAuth Creds to be used by SDK Client |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $credentials = array(); |
||
78 | |||
79 | /** |
||
80 | * Token expiration time |
||
81 | * @var |
||
82 | */ |
||
83 | protected $expiration; |
||
84 | |||
85 | /** |
||
86 | * The list of registered EntryPoints |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $entryPoints = array(); |
||
90 | |||
91 | 1 | public function __construct($server = '',array $credentials = array()){ |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | * @param string $server |
||
102 | */ |
||
103 | 1 | public function setServer($server) { |
|
108 | |||
109 | /** |
||
110 | * @inheritdoc |
||
111 | */ |
||
112 | 1 | public function getAPIUrl() { |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | * Retrieves stored token based on passed in Credentials |
||
119 | */ |
||
120 | 2 | public function setCredentials(array $credentials){ |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 1 | public function setToken(\stdClass $token){ |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 2 | public function getToken(){ |
|
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 1 | public function getCredentials(){ |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 1 | public function getServer() { |
|
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | 1 | public function authenticated(){ |
|
167 | |||
168 | /** |
||
169 | * Register the defined EntryPoints in SDK, located in src/EntryPoint/registry.php file |
||
170 | * @throws EntryPointException |
||
171 | */ |
||
172 | 1 | protected function registerSDKEntryPoints(){ |
|
178 | |||
179 | /** |
||
180 | * @param $funcName |
||
181 | * @param $className |
||
182 | * @return self |
||
183 | * @throws EntryPointException |
||
184 | */ |
||
185 | 2 | public function registerEntryPoint($funcName, $className){ |
|
194 | |||
195 | /** |
||
196 | * Generates the EntryPoint objects based on a Method name that was called |
||
197 | * @param $name |
||
198 | * @param $params |
||
199 | * @return mixed |
||
200 | * @throws EntryPointException |
||
201 | */ |
||
202 | 2 | public function __call($name, $params){ |
|
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | * @throws AuthenticationException - When Login request fails |
||
219 | */ |
||
220 | 1 | public function login() { |
|
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | * @throws AuthenticationException - When Refresh Request fails |
||
238 | */ |
||
239 | 1 | public function refreshToken(){ |
|
260 | |||
261 | /** |
||
262 | * @inheritdoc |
||
263 | * @throws AuthenticationException - When logout request fails |
||
264 | */ |
||
265 | 1 | public function logout(){ |
|
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | * @param \stdClass $token |
||
283 | */ |
||
284 | 1 | public static function storeToken($token, $client_id) { |
|
288 | |||
289 | /** |
||
290 | * @inheritdoc |
||
291 | */ |
||
292 | 1 | public static function getStoredToken($client_id) { |
|
295 | |||
296 | /** |
||
297 | * @inheritdoc |
||
298 | */ |
||
299 | 1 | public static function removeStoredToken($client_id) { |
|
303 | |||
304 | } |
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.