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 |
||
35 | class OAuthClient |
||
36 | { |
||
37 | /** @var TokenStorageInterface */ |
||
38 | private $tokenStorage; |
||
39 | |||
40 | /** @var \fkooman\OAuth\Client\Http\HttpClientInterface */ |
||
41 | private $httpClient; |
||
42 | |||
43 | /** @var SessionInterface */ |
||
44 | private $session; |
||
45 | |||
46 | /** @var RandomInterface */ |
||
47 | private $random; |
||
48 | |||
49 | /** @var \DateTime */ |
||
50 | private $dateTime; |
||
51 | |||
52 | /** @var Provider */ |
||
53 | private $provider = null; |
||
54 | |||
55 | /** @var string */ |
||
56 | private $userId = null; |
||
57 | |||
58 | /** |
||
59 | * @param TokenStorageInterface $tokenStorage |
||
60 | * @param Http\HttpClientInterface $httpClient |
||
61 | */ |
||
62 | public function __construct(TokenStorageInterface $tokenStorage, HttpClientInterface $httpClient) |
||
63 | { |
||
64 | $this->tokenStorage = $tokenStorage; |
||
65 | $this->httpClient = $httpClient; |
||
66 | |||
67 | $this->session = new Session(); |
||
68 | $this->random = new Random(); |
||
69 | $this->dateTime = new DateTime(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param Provider $provider |
||
74 | */ |
||
75 | public function setProvider(Provider $provider) |
||
76 | { |
||
77 | $this->provider = $provider; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param SessionInterface $session |
||
82 | */ |
||
83 | public function setSession(SessionInterface $session) |
||
84 | { |
||
85 | $this->session = $session; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param RandomInterface $random |
||
90 | */ |
||
91 | public function setRandom(RandomInterface $random) |
||
92 | { |
||
93 | $this->random = $random; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param DateTime $dateTime |
||
98 | */ |
||
99 | public function setDateTime(DateTime $dateTime) |
||
100 | { |
||
101 | $this->dateTime = $dateTime; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $userId |
||
106 | */ |
||
107 | public function setUserId($userId) |
||
108 | { |
||
109 | $this->userId = $userId; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Perform a GET request, convenience wrapper for ::send(). |
||
114 | * |
||
115 | * @param string $requestScope |
||
116 | * @param string $requestUri |
||
117 | * @param array $requestHeaders |
||
118 | * |
||
119 | * @return Http\Response|false |
||
120 | */ |
||
121 | public function get($requestScope, $requestUri, array $requestHeaders = []) |
||
125 | |||
126 | /** |
||
127 | * Perform a POST request, convenience wrapper for ::send(). |
||
128 | * |
||
129 | * @param string $requestScope |
||
130 | * @param string $requestUri |
||
131 | * @param array $postBody |
||
132 | * @param array $requestHeaders |
||
133 | * |
||
134 | * @return Http\Response|false |
||
135 | */ |
||
136 | public function post($requestScope, $requestUri, array $postBody, array $requestHeaders = []) |
||
140 | |||
141 | /** |
||
142 | * Perform a HTTP request. |
||
143 | * |
||
144 | * @param string $requestScope |
||
145 | * @param Http\Request $request |
||
146 | * |
||
147 | * @return Response|false |
||
148 | */ |
||
149 | public function send($requestScope, Request $request) |
||
190 | |||
191 | /** |
||
192 | * Obtain an authorization request URL to start the authorization process |
||
193 | * at the OAuth provider. |
||
194 | * |
||
195 | * @param string $scope the space separated scope tokens |
||
196 | * @param string $redirectUri the URL registered at the OAuth provider, to |
||
197 | * be redirected back to |
||
198 | * |
||
199 | * @return string the authorization request URL |
||
200 | * |
||
201 | * @see https://tools.ietf.org/html/rfc6749#section-3.3 |
||
202 | * @see https://tools.ietf.org/html/rfc6749#section-3.1.2 |
||
203 | */ |
||
204 | public function getAuthorizeUri($scope, $redirectUri) |
||
237 | |||
238 | /** |
||
239 | * @param string $responseCode the code passed to the "code" |
||
240 | * query parameter on the callback URL |
||
241 | * @param string $responseState the state passed to the "state" |
||
242 | * query parameter on the callback URL |
||
243 | */ |
||
244 | public function handleCallback($responseCode, $responseState) |
||
322 | |||
323 | /** |
||
324 | * @param AccessToken $accessToken |
||
325 | * |
||
326 | * @return AccessToken|false |
||
327 | */ |
||
328 | private function refreshAccessToken(AccessToken $accessToken) |
||
385 | |||
386 | /** |
||
387 | * Find an AccessToken in the list that matches this scope, bound to |
||
388 | * providerId and userId. |
||
389 | * |
||
390 | * @param string $scope |
||
391 | * |
||
392 | * @return AccessToken|false |
||
393 | */ |
||
394 | private function getAccessToken($scope) |
||
410 | } |
||
411 |
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.