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 |
||
29 | class Token implements AuthProviderInterface |
||
|
|||
30 | { |
||
31 | /** |
||
32 | * Generated auth token. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $token; |
||
37 | |||
38 | /** |
||
39 | * Path to p8 private key. |
||
40 | * |
||
41 | * @var string|null |
||
42 | */ |
||
43 | private $privateKeyPath; |
||
44 | |||
45 | /** |
||
46 | * Private key data. |
||
47 | * |
||
48 | * @var string|null |
||
49 | */ |
||
50 | private $privateKeyContent; |
||
51 | |||
52 | /** |
||
53 | * Private key secret. |
||
54 | * |
||
55 | * @var string|null |
||
56 | */ |
||
57 | private $privateKeySecret; |
||
58 | |||
59 | /** |
||
60 | * The Key ID obtained from Apple developer account. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $keyId; |
||
65 | |||
66 | /** |
||
67 | * The Team ID obtained from Apple developer account. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $teamId; |
||
72 | |||
73 | /** |
||
74 | * The bundle ID for app obtained from Apple developer account. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | private $appBundleId; |
||
79 | |||
80 | /** |
||
81 | * This provider accepts the following options: |
||
82 | * |
||
83 | * - key_id |
||
84 | * - team_id |
||
85 | * - app_bundle_id |
||
86 | * - private_key_path |
||
87 | * - private_key_content |
||
88 | * - private_key_secret |
||
89 | * |
||
90 | * @param array $options |
||
91 | */ |
||
92 | private function __construct(array $options) |
||
101 | |||
102 | /** |
||
103 | * Create Token Auth Provider. |
||
104 | * |
||
105 | * @param array $options |
||
106 | * @return Token |
||
107 | */ |
||
108 | public static function create(array $options): Token |
||
115 | |||
116 | /** |
||
117 | * Use previously generated token. |
||
118 | * |
||
119 | * @param string $tokenString |
||
120 | * @param array $options |
||
121 | * @return Token |
||
122 | */ |
||
123 | public static function useExisting(string $tokenString, array $options): Token |
||
130 | |||
131 | /** |
||
132 | * Authenticate client. |
||
133 | * |
||
134 | * @param Request $request |
||
135 | */ |
||
136 | public function authenticateClient(Request $request) |
||
143 | |||
144 | /** |
||
145 | * Generate a correct apns-topic string |
||
146 | * |
||
147 | * @param string $pushType |
||
148 | * @return string |
||
149 | */ |
||
150 | View Code Duplication | public function generateApnsTopic(string $pushType) |
|
166 | |||
167 | /** |
||
168 | * Get last generated token. |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function get(): string |
||
176 | |||
177 | /** |
||
178 | * Generate private EC key. |
||
179 | * |
||
180 | * @return JWK |
||
181 | */ |
||
182 | private function generatePrivateECKey(): JWK |
||
198 | |||
199 | /** |
||
200 | * Get claims payload. |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | private function getClaimsPayload(): array |
||
211 | |||
212 | /** |
||
213 | * Get protected header. |
||
214 | * |
||
215 | * @param JWK $privateECKey |
||
216 | * @return array |
||
217 | */ |
||
218 | private function getProtectedHeader(JWK $privateECKey): array |
||
225 | |||
226 | /** |
||
227 | * Generate new token. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | private function generate(): string |
||
253 | } |
||
254 |