Complex classes like CommonApiClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CommonApiClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class CommonApiClient implements CommonApiClientInterface |
||
41 | { |
||
42 | const URL_SANDBOX = 'https://proxy.staging.devpayever.com/'; |
||
43 | const URL_LIVE = 'https://proxy.payever.org/'; |
||
44 | |||
45 | const SUB_URL_AUTH = 'oauth/v2/token'; |
||
46 | const SUB_URL_LIST_CHANNEL_SETS = 'api/shop/%s/channel-sets'; |
||
47 | const SUB_URL_CURRENCY = 'api/rest/v1/currency'; |
||
48 | |||
49 | /** |
||
50 | * Stores oAuth Authentication Tokens |
||
51 | * |
||
52 | * @var OauthTokenList $tokens |
||
53 | */ |
||
54 | protected $tokens; |
||
55 | |||
56 | /** |
||
57 | * Stores API Configuration |
||
58 | * |
||
59 | * @var ClientConfigurationInterface $configuration |
||
60 | */ |
||
61 | protected $configuration; |
||
62 | |||
63 | /** |
||
64 | * Stores current Client |
||
65 | * |
||
66 | * @var HttpClientInterface $httpClient |
||
67 | */ |
||
68 | protected $httpClient; |
||
69 | |||
70 | /** |
||
71 | * @param ClientConfigurationInterface $clientConfiguration |
||
72 | * @param OauthTokenList $oauthTokenList |
||
73 | * @param HttpClientInterface $httpClient |
||
74 | * |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function __construct( |
||
86 | |||
87 | /** |
||
88 | * Returns Base URL to payever Payments API |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getBaseUrl() |
||
96 | |||
97 | /** |
||
98 | * Returns current configuration |
||
99 | * |
||
100 | * @return ClientConfigurationInterface |
||
101 | */ |
||
102 | public function getConfiguration() |
||
106 | |||
107 | /** |
||
108 | * Returns current OauthToken list |
||
109 | * |
||
110 | * @return OauthTokenList |
||
111 | */ |
||
112 | public function getTokens() |
||
116 | |||
117 | /** |
||
118 | * Overrides configuration |
||
119 | * |
||
120 | * @param ClientConfigurationInterface $configuration |
||
121 | */ |
||
122 | public function setConfiguration(ClientConfigurationInterface $configuration) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function getHttpClient() |
||
144 | |||
145 | /** |
||
146 | * @param string $logLevel |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setHttpClientRequestFailureLogLevel($logLevel = LogLevel::CRITICAL) |
||
157 | |||
158 | /** |
||
159 | * @param string $logLevel |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function setHttpClientRequestFailureLogLevelOnce($logLevel) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function setHttpClient(HttpClientInterface $httpClient) |
||
184 | |||
185 | /** |
||
186 | * Returns Authentication OauthToken |
||
187 | * |
||
188 | * @param string $scope OauthToken scope |
||
189 | * |
||
190 | * @return OauthTokenInterface |
||
191 | * |
||
192 | * @throws \Exception |
||
193 | */ |
||
194 | public function getToken($scope = OauthTokenInterface::SCOPE_PAYMENT_ACTIONS) |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | * |
||
231 | * @throws \Exception |
||
232 | */ |
||
233 | public function getCurrenciesRequest($lang = '') |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | * |
||
246 | * @throws \Exception |
||
247 | */ |
||
248 | public function listChannelSetsRequest($businessUuid) |
||
259 | |||
260 | /** |
||
261 | * @param bool $staticBind |
||
262 | * @return string |
||
263 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
264 | */ |
||
265 | protected function getBaseEntrypoint($staticBind = false) |
||
285 | |||
286 | /** |
||
287 | * Loads Tokens |
||
288 | * |
||
289 | * @param OauthTokenList|null $oauthTokenList |
||
290 | * |
||
291 | * @return $this |
||
292 | */ |
||
293 | protected function loadTokens(OauthTokenList $oauthTokenList = null) |
||
303 | |||
304 | /** |
||
305 | * Requests new oAuth OauthToken which will be used further |
||
306 | * |
||
307 | * @link https://docs.payever.org/shopsystems/api/getting-started/authentication Documentation |
||
308 | * |
||
309 | * @param string $scope Scope in which the token will be used |
||
310 | * |
||
311 | * @return ResponseInterface |
||
312 | * |
||
313 | * @throws \Exception |
||
314 | */ |
||
315 | protected function obtainTokenRequest($scope) |
||
337 | |||
338 | /** |
||
339 | * Requests for an updated oAuth OauthToken data |
||
340 | * |
||
341 | * @param OauthTokenInterface|object|array $token OauthToken for the update |
||
342 | * |
||
343 | * @return ResponseInterface |
||
344 | * |
||
345 | * @throws \Exception |
||
346 | */ |
||
347 | protected function refreshTokenRequest(OauthTokenInterface $token) |
||
366 | |||
367 | /** |
||
368 | * Returns URL for Authentication path |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | protected function getAuthenticationURL() |
||
376 | |||
377 | /** |
||
378 | * Returns URL for Available Channel Sets path |
||
379 | * |
||
380 | * @param string $businessUuid |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | protected function getListChannelSetsURL($businessUuid) |
||
388 | |||
389 | /** |
||
390 | * Returns URL to Currencies path |
||
391 | * |
||
392 | * @param string $lang |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | protected function getCurrenciesURL($lang = '') |
||
400 | } |
||
401 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.