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 |
||
18 | abstract class Token_Subscription_Service implements Subscription_Service { |
||
19 | |||
20 | const JWT_AUTH_TOKEN_COOKIE_NAME = 'jp-premium-content-session'; |
||
21 | const DECODE_EXCEPTION_FEATURE = 'memberships'; |
||
22 | const DECODE_EXCEPTION_MESSAGE = 'Problem decoding provided token'; |
||
23 | const REST_URL_ORIGIN = 'https://subscribe.wordpress.com/'; |
||
24 | |||
25 | /** |
||
26 | * Initialize the token subscription service. |
||
27 | * |
||
28 | * @inheritDoc |
||
29 | */ |
||
30 | public function initialize() { |
||
36 | |||
37 | /** |
||
38 | * The user is visiting with a subscriber token cookie. |
||
39 | * |
||
40 | * This is theoretically where the cookie JWT signature verification |
||
41 | * thing will happen. |
||
42 | * |
||
43 | * How to obtain one of these (or what exactly it is) is |
||
44 | * still a WIP (see api/auth branch) |
||
45 | * |
||
46 | * @inheritDoc |
||
47 | * |
||
48 | * @param array $valid_plan_ids List of valid plan IDs. |
||
49 | */ |
||
50 | public function visitor_can_view_content( $valid_plan_ids ) { |
||
108 | |||
109 | /** |
||
110 | * Decode the given token. |
||
111 | * |
||
112 | * @param string $token Token to decode. |
||
113 | * |
||
114 | * @return array|false |
||
115 | */ |
||
116 | public function decode_token( $token ) { |
||
124 | |||
125 | /** |
||
126 | * Get the key for decoding the auth token. |
||
127 | * |
||
128 | * @return string|false |
||
129 | */ |
||
130 | abstract public function get_key(); |
||
131 | |||
132 | /** |
||
133 | * Get the ID of the current site. |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | abstract public function get_site_id(); |
||
138 | |||
139 | // phpcs:disable |
||
140 | /** |
||
141 | * Get the URL to access the protected content. |
||
142 | * |
||
143 | * @param string $mode Access mode (either "subscribe" or "login"). |
||
144 | */ |
||
145 | public function access_url( $mode = 'subscribe' ) { |
||
155 | // phpcs:enable |
||
156 | |||
157 | /** |
||
158 | * Get the token stored in the auth cookie. |
||
159 | * |
||
160 | * @return ?string |
||
161 | */ |
||
162 | private function token_from_cookie() { |
||
168 | |||
169 | /** |
||
170 | * Store the auth cookie. |
||
171 | * |
||
172 | * @param string $token Auth token. |
||
173 | * @return void |
||
174 | */ |
||
175 | private function set_token_cookie( $token ) { |
||
180 | |||
181 | /** |
||
182 | * Get the token if present in the current request. |
||
183 | * |
||
184 | * @return ?string |
||
185 | */ |
||
186 | private function token_from_request() { |
||
198 | |||
199 | /** |
||
200 | * Return true if any ID/date pairs are valid. Otherwise false. |
||
201 | * |
||
202 | * @param int[] $valid_plan_ids List of valid plan IDs. |
||
203 | * @param array<int, Token_Subscription> $token_subscriptions : ID must exist in the provided <code>$valid_subscriptions</code> parameter. |
||
204 | * The provided end date needs to be greater than <code>now()</code>. |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | protected function validate_subscriptions( $valid_plan_ids, $token_subscriptions ) { |
||
228 | |||
229 | /** |
||
230 | * Get the URL of the JWT endpoint. |
||
231 | * |
||
232 | * @param int $site_id Site ID. |
||
233 | * @param string $redirect_url URL to redirect after checking the token validity. |
||
234 | * @return string URL of the JWT endpoint. |
||
235 | */ |
||
236 | private function get_rest_api_token_url( $site_id, $redirect_url ) { |
||
239 | |||
240 | /** |
||
241 | * Report the subscriptions as an ID => [ 'end_date' => ]. mapping |
||
242 | * |
||
243 | * @param array $subscriptions_from_bd List of subscriptions from BD. |
||
244 | * |
||
245 | * @return array<int, array> |
||
246 | */ |
||
247 | View Code Duplication | public static function abbreviate_subscriptions( $subscriptions_from_bd ) { |
|
264 | } |
||
265 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.