Complex classes like LinkedIn 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 LinkedIn, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class LinkedIn implements LinkedInInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * The OAuth access token received in exchange for a valid authorization |
||
| 38 | * code. null means the access token has yet to be determined. |
||
| 39 | * |
||
| 40 | * @var AccessToken |
||
| 41 | */ |
||
| 42 | protected $accessToken = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string format |
||
| 46 | */ |
||
| 47 | private $format; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string responseFormat |
||
| 51 | */ |
||
| 52 | private $responseDataType; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ResponseInterface |
||
| 56 | */ |
||
| 57 | private $lastResponse; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var RequestManager |
||
| 61 | */ |
||
| 62 | private $requestManager; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Authenticator |
||
| 66 | */ |
||
| 67 | private $authenticator; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var UrlGeneratorInterface |
||
| 71 | */ |
||
| 72 | private $urlGenerator; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Constructor. |
||
| 76 | * |
||
| 77 | * @param string $appId |
||
| 78 | * @param string $appSecret |
||
| 79 | * @param string $format 'json', 'xml' |
||
| 80 | * @param string $responseDataType 'array', 'string', 'simple_xml' 'psr7', 'stream' |
||
| 81 | 7 | */ |
|
| 82 | public function __construct($appId, $appSecret, $format = 'json', $responseDataType = 'array', RequestManagerInterface $requestManager = null, AuthenticatorInterface $authenticator = null) |
||
| 83 | 7 | { |
|
| 84 | 7 | $this->format = $format; |
|
| 85 | $this->responseDataType = $responseDataType; |
||
| 86 | 7 | ||
| 87 | 7 | $this->requestManager = isset($requestManager) ? $requestManager : new RequestManager(); |
|
|
|
|||
| 88 | 7 | $this->authenticator = isset($authenticator) ? $authenticator : new Authenticator($this->requestManager, $appId, $appSecret); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | 1 | */ |
|
| 94 | public function isAuthenticated() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | 1 | */ |
|
| 109 | public function api($method, $resource, array $options = []) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Modify and filter the request options. Make sure we use the correct query parameters and headers. |
||
| 139 | * |
||
| 140 | * @param array &$options |
||
| 141 | * |
||
| 142 | * @return string the request format to use |
||
| 143 | 1 | */ |
|
| 144 | protected function filterRequestOption(array &$options) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | 2 | */ |
|
| 174 | public function getLoginUrl($options = []) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * See docs for LinkedIn::api(). |
||
| 188 | * |
||
| 189 | * @param string $resource |
||
| 190 | * @param array $options |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | public function get($resource, array $options = []) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * See docs for LinkedIn::api(). |
||
| 201 | * |
||
| 202 | * @param string $resource |
||
| 203 | * @param array $options |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function post($resource, array $options = []) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function clearStorage() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | 3 | */ |
|
| 225 | public function hasError() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | 2 | */ |
|
| 233 | public function getError() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the default format to use when sending requests. |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | 1 | */ |
|
| 245 | protected function getFormat() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | 1 | */ |
|
| 253 | public function setFormat($format) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the default data type to be returned as a response. |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | 1 | */ |
|
| 265 | protected function getResponseDataType() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritdoc} |
||
| 272 | */ |
||
| 273 | public function setResponseDataType($responseDataType) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getLastResponse() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | 1 | */ |
|
| 291 | public function getAccessToken() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | 1 | */ |
|
| 306 | public function setAccessToken($accessToken) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | 1 | */ |
|
| 320 | public function setUrlGenerator(UrlGeneratorInterface $urlGenerator) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return UrlGeneratorInterface |
||
| 329 | 2 | */ |
|
| 330 | protected function getUrlGenerator() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param RequestManagerInterface $manager |
||
| 341 | */ |
||
| 342 | public function setRequestManager(RequestManagerInterface $manager) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function setStorage(DataStorageInterface $storage) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * {@inheritdoc} |
||
| 360 | */ |
||
| 361 | public function setHttpClient(HttpClient $client) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | public function setHttpMessageFactory(MessageFactory $factory) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return RequestManager |
||
| 380 | */ |
||
| 381 | protected function getRequestManager() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return Authenticator |
||
| 388 | */ |
||
| 389 | protected function getAuthenticator() |
||
| 393 | |||
| 394 | } |
||
| 395 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.