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 |
||
| 13 | class Auth |
||
|
|
|||
| 14 | { |
||
| 15 | /** @var self */ |
||
| 16 | private static $login; |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | protected $loginUrl; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | private $clientId; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | private $clientSecret; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | protected $grantType = 'client_credentials'; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | private $token; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | private $tokenExpiry; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | private function __construct() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the instance of this class |
||
| 46 | * |
||
| 47 | * @param string|null $loginUrl |
||
| 48 | * @return self |
||
| 49 | */ |
||
| 50 | public static function login($loginUrl = null) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return self |
||
| 65 | */ |
||
| 66 | public function setClientCredentials() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param null|string $clientId |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | public function setClientId($clientId) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param null|string $clientSecret |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public function setClientSecret($clientSecret) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $grantType |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | public function setGrantType(string $grantType) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getToken() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | View Code Duplication | private function auth(): void |
|
| 133 | } |
||
| 134 |