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 |
||
34 | class Write extends Base |
||
35 | { |
||
36 | |||
37 | |||
38 | |||
39 | public function __construct() |
||
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * Handles requests to get all (or a filtered subset) of entities for a particular model |
||
49 | * |
||
50 | * @param WP_REST_Request $request |
||
51 | * @param string $version |
||
52 | * @param string $model_name |
||
53 | * @return WP_REST_Response|\WP_Error |
||
54 | */ |
||
55 | View Code Duplication | public static function handleRequestInsert(WP_REST_Request $request, $version, $model_name) |
|
70 | |||
71 | |||
72 | |||
73 | /** |
||
74 | * Handles a request from \WP_REST_Server to update an EE model |
||
75 | * |
||
76 | * @param WP_REST_Request $request |
||
77 | * @param string $version |
||
78 | * @param string $model_name |
||
79 | * @return WP_REST_Response|\WP_Error |
||
80 | */ |
||
81 | View Code Duplication | public static function handleRequestUpdate(WP_REST_Request $request, $version, $model_name) |
|
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * Deletes a single model object and returns it. Unless |
||
101 | * |
||
102 | * @param WP_REST_Request $request |
||
103 | * @param string $version |
||
104 | * @param string $model_name |
||
105 | * @return WP_REST_Response|\WP_Error |
||
106 | */ |
||
107 | View Code Duplication | public static function handleRequestDelete(WP_REST_Request $request, $version, $model_name) |
|
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Inserts a new model object according to the $request |
||
127 | * |
||
128 | * @param EEM_Base $model |
||
129 | * @param WP_REST_Request $request |
||
130 | * @return array |
||
131 | * @throws EE_Error |
||
132 | * @throws RestException |
||
133 | */ |
||
134 | public function insert(EEM_Base $model, WP_REST_Request $request) |
||
176 | |||
177 | |||
178 | |||
179 | /** |
||
180 | * Updates an existing model object according to the $request |
||
181 | * |
||
182 | * @param EEM_Base $model |
||
183 | * @param WP_REST_Request $request |
||
184 | * @return array |
||
185 | * @throws EE_Error |
||
186 | */ |
||
187 | public function update(EEM_Base $model, WP_REST_Request $request) |
||
231 | |||
232 | |||
233 | |||
234 | /** |
||
235 | * Updates an existing model object according to the $request |
||
236 | * |
||
237 | * @param EEM_Base $model |
||
238 | * @param WP_REST_Request $request |
||
239 | * @return array of either the soft-deleted item, or |
||
240 | * @throws EE_Error |
||
241 | */ |
||
242 | public function delete(EEM_Base $model, WP_REST_Request $request) |
||
243 | { |
||
244 | Capabilities::verifyAtLeastPartialAccessTo($model, EEM_Base::caps_delete, 'delete'); |
||
245 | $default_cap_to_check_for = EE_Restriction_Generator_Base::get_default_restrictions_cap(); |
||
246 | View Code Duplication | if (! current_user_can($default_cap_to_check_for)) { |
|
247 | throw new RestException( |
||
248 | 'rest_cannot_delete_' . EEH_Inflector::pluralize_and_lower(($model->get_this_model_name())), |
||
249 | sprintf( |
||
250 | esc_html__( |
||
251 | // @codingStandardsIgnoreStart |
||
252 | 'For now, only those with the admin capability to "%1$s" are allowed to use the REST API to delete data into Event Espresso.', |
||
253 | // @codingStandardsIgnoreEnd |
||
254 | 'event_espresso' |
||
255 | ), |
||
256 | $default_cap_to_check_for |
||
257 | ), |
||
258 | array('status' => 403) |
||
259 | ); |
||
260 | } |
||
261 | $obj_id = $request->get_param('id'); |
||
262 | //this is where we would apply more fine-grained caps |
||
263 | $model_obj = $model->get_one_by_ID($obj_id); |
||
264 | View Code Duplication | if (! $model_obj instanceof EE_Base_Class) { |
|
265 | $lowercase_model_name = strtolower($model->get_this_model_name()); |
||
266 | throw new RestException( |
||
267 | sprintf('rest_%s_invalid_id', $lowercase_model_name), |
||
268 | sprintf(__('Invalid %s ID.', 'event_espresso'), $lowercase_model_name), |
||
269 | array('status' => 404) |
||
270 | ); |
||
271 | } |
||
272 | $requested_permanent_delete = filter_var($request->get_param('force'), FILTER_VALIDATE_BOOLEAN); |
||
273 | $requested_allow_blocking = filter_var($request->get_param('allow_blocking'), FILTER_VALIDATE_BOOLEAN); |
||
274 | if ($requested_permanent_delete) { |
||
275 | $previous = $this->returnModelObjAsJsonResponse($model_obj, $request); |
||
276 | $deleted = (bool)$model->delete_permanently_by_ID($obj_id, $requested_allow_blocking); |
||
277 | return array( |
||
278 | 'deleted' => $deleted, |
||
279 | 'previous' => $previous, |
||
280 | ); |
||
281 | } else { |
||
282 | if ($model instanceof EEM_Soft_Delete_Base) { |
||
283 | $model->delete_by_ID($obj_id, $requested_allow_blocking); |
||
284 | return $this->returnModelObjAsJsonResponse($model_obj, $request); |
||
285 | } else { |
||
286 | throw new RestException( |
||
287 | 'rest_trash_not_supported', |
||
288 | 501, |
||
289 | sprintf( |
||
290 | esc_html__('%1$s do not support trashing. Set force=1 to delete.', 'event_espresso'), |
||
291 | EEH_Inflector::pluralize($model->get_this_model_name()) |
||
292 | ) |
||
293 | ); |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | |||
299 | |||
300 | /** |
||
301 | * Returns an array ready to be converted into a JSON response, based solely on the model object |
||
302 | * |
||
303 | * @param EE_Base_Class $model_obj |
||
304 | * @param WP_REST_Request $request |
||
305 | * @return array ready for a response |
||
306 | */ |
||
307 | protected function returnModelObjAsJsonResponse(EE_Base_Class $model_obj, WP_REST_Request $request) |
||
335 | |||
336 | |||
337 | |||
338 | /** |
||
339 | * Gets the item affected by this request |
||
340 | * |
||
341 | * @param EEM_Base $model |
||
342 | * @param WP_REST_Request $request |
||
343 | * @param int|string $obj_id |
||
344 | * @return \WP_Error|array |
||
345 | */ |
||
346 | protected function getOneBasedOnRequest(EEM_Base $model, WP_REST_Request $request, $obj_id) |
||
368 | } |
||
369 | // End of file Read.php |
||
370 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: