Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

WPCOM_JSON_API_Delete_Media_Endpoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A callback() 20 20 4

How to fix   Duplicated Code   

Duplicated Code

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
2
3
new WPCOM_JSON_API_Delete_Media_Endpoint( array(
4
	'description' => 'Delete a piece of media.',
5
	'group'       => 'media',
6
	'stat'        => 'media:1:delete',
7
	'method'      => 'POST',
8
	'path'        => '/sites/%s/media/%d/delete',
9
	'deprecated'  => true,
10
	'new_version' => '1.1',
11
	'max_version' => '1',
12
	'path_labels' => array(
13
		'$site'    => '(int|string) Site ID or domain',
14
		'$media_ID' => '(int) The media ID',
15
	),
16
17
	'response_format' => array(
18
		'status' => '(string) Returns deleted if the media was successfully deleted',
19
		'id'    => '(int) The ID of the media item',
20
		'date' =>  '(ISO 8601 datetime) The date the media was uploaded',
21
		'parent'           => '(int) ID of the post this media is attached to',
22
		'link'             => '(string) URL to the file',
23
		'title'            => '(string) File name',
24
		'caption'          => '(string) User provided caption of the file',
25
		'description'      => '(string) Description of the file',
26
		'metadata'         => '(array) Misc array of information about the file, such as exif data or sizes',
27
	),
28
29
	'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/$media_ID/delete',
30
	'example_request_data' =>  array(
31
		'headers' => array(
32
			'authorization' => 'Bearer YOUR_API_TOKEN'
33
		)
34
	)
35
) );
36
37 View Code Duplication
class WPCOM_JSON_API_Delete_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
38
	function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
39
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
40
		if ( is_wp_error( $blog_id ) ) {
41
			return $blog_id;
42
		}
43
44
		if ( ! current_user_can( 'delete_post', $media_id ) ) {
45
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
		}
47
48
		$item = $this->get_media_item( $media_id );
49
50
		if ( is_wp_error( $item ) ) {
51
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unknown_media'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
		}
53
54
		wp_delete_post( $media_id );
55
		$item->status = 'deleted';
56
		return $item;
57
	}
58
}
59