Completed
Push — try/woocommerce-analytics ( 2b8c13...0a25c9 )
by
unknown
32:37 queued 22:26
created

Jetpack_JSON_API_Plugins_Install_Endpoint   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 6.52 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B install() 0 22 5
B validate_plugins() 3 16 5

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
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
4
include_once ABSPATH . 'wp-admin/includes/file.php';
5
// POST /sites/%s/plugins/%s/install
6
new Jetpack_JSON_API_Plugins_Install_Endpoint(
7
	array(
8
		'description'          => 'Install a plugin to your jetpack blog',
9
		'group'                => '__do_not_document',
10
		'stat'                 => 'plugins:1:install',
11
		'min_version'          => '1',
12
		'max_version'          => '1.1',
13
		'method'               => 'POST',
14
		'path'                 => '/sites/%s/plugins/%s/install',
15
		'path_labels'          => array(
16
			'$site'   => '(int|string) The site ID, The site domain',
17
			'$plugin' => '(int|string) The plugin slug to install',
18
		),
19
		'response_format'      => Jetpack_JSON_API_Plugins_Endpoint::$_response_format,
0 ignored issues
show
Bug introduced by
The property _response_format cannot be accessed from this context as it is declared private in class Jetpack_JSON_API_Plugins_Endpoint.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
20
		'example_request_data' => array(
21
			'headers' => array(
22
				'authorization' => 'Bearer YOUR_API_TOKEN'
23
			),
24
		),
25
		'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/akismet/install'
26
	)
27
);
28
29
new Jetpack_JSON_API_Plugins_Install_Endpoint(
30
	array(
31
		'description'          => 'Install a plugin to your jetpack blog',
32
		'group'                => '__do_not_document',
33
		'stat'                 => 'plugins:1:install',
34
		'min_version'          => '1.2',
35
		'method'               => 'POST',
36
		'path'                 => '/sites/%s/plugins/%s/install',
37
		'path_labels'          => array(
38
			'$site'   => '(int|string) The site ID, The site domain',
39
			'$plugin' => '(int|string) The plugin slug to install',
40
		),
41
		'response_format'      => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2,
0 ignored issues
show
Bug introduced by
The property _response_format_v1_2 cannot be accessed from this context as it is declared private in class Jetpack_JSON_API_Plugins_Endpoint.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
42
		'example_request_data' => array(
43
			'headers' => array(
44
				'authorization' => 'Bearer YOUR_API_TOKEN'
45
			),
46
		),
47
		'example_request'      => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/akismet/install'
48
	)
49
);
50
51
class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint {
52
53
	// POST /sites/%s/plugins/%s/install
54
	protected $needed_capabilities = 'install_plugins';
55
	protected $action = 'install';
56
57
	protected function install() {
58
		jetpack_require_lib( 'plugins' );
59
		$result = '';
60
		foreach ( $this->plugins as $index => $slug ) {
61
			$result = Jetpack_Plugins::install_plugin( $slug );
62
			if ( is_wp_error( $result ) ) {
63
				$this->log[ $slug ][] = $result->get_error_message();
64
				if ( ! $this->bulk ) {
65
					return $result;
66
				}
67
			}
68
		}
69
70
		if ( is_wp_error( $result ) ) {
71
			return $result;
72
		}
73
74
		// No errors, install worked. Now replace the slug with the actual plugin id
75
		$this->plugins[$index] = Jetpack_Plugins::get_plugin_id_by_slug( $slug );
0 ignored issues
show
Bug introduced by
The variable $index seems to be defined by a foreach iteration on line 60. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
Bug introduced by
The variable $slug seems to be defined by a foreach iteration on line 60. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
76
77
		return true;
78
	}
79
80
	protected function validate_plugins() {
81 View Code Duplication
		if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
82
			return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ) );
83
		}
84
85
		jetpack_require_lib( 'plugins' );
86
		foreach ( $this->plugins as $index => $slug ) {
87
			// make sure it is not already installed
88
			if ( Jetpack_Plugins::get_plugin_id_by_slug( $slug ) ) {
89
				return new WP_Error( 'plugin_already_installed', __( 'The plugin is already installed', 'jetpack' ) );
90
			}
91
92
		}
93
94
		return true;
95
	}
96
}
97