Completed
Push — update/instagram-oembed-simpli... ( f48bfe...478cfa )
by Bernhard
08:24
created

Jetpack_IXR_Client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B query() 0 41 6
A get_jetpack_error() 0 18 4
1
<?php
2
/**
3
 * IXR_Client
4
 *
5
 * @package automattic/jetpack-connection
6
 *
7
 * @since 1.5
8
 * @since 7.7 Moved to the jetpack-connection package.
9
 */
10
11
use Automattic\Jetpack\Connection\Client;
12
use Automattic\Jetpack\Connection\Manager;
13
14
/**
15
 * A Jetpack implementation of the WordPress core IXR client.
16
 */
17
class Jetpack_IXR_Client extends IXR_Client {
18
	/**
19
	 * Jetpack args, used for the remote requests.
20
	 *
21
	 * @var array
22
	 */
23
	public $jetpack_args = null;
24
25
	/**
26
	 * Constructor.
27
	 * Initialize a new Jetpack IXR client instance.
28
	 *
29
	 * @param array       $args    Jetpack args, used for the remote requests.
30
	 * @param string|bool $path    Path to perform the reuqest to.
31
	 * @param int         $port    Port number.
32
	 * @param int         $timeout The connection timeout, in seconds.
33
	 */
34
	public function __construct( $args = array(), $path = false, $port = 80, $timeout = 15 ) {
35
		$connection = new Manager();
36
37
		$defaults = array(
38
			'url'     => $connection->xmlrpc_api_url(),
39
			'user_id' => 0,
40
		);
41
42
		$args = wp_parse_args( $args, $defaults );
0 ignored issues
show
Documentation introduced by
$defaults is of type array<string,string|inte...","user_id":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
44
		$this->jetpack_args = $args;
0 ignored issues
show
Documentation Bug introduced by
It seems like $args can be null. However, the property $jetpack_args is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
45
46
		$this->IXR_Client( $args['url'], $path, $port, $timeout );
47
	}
48
49
	/**
50
	 * Perform the IXR request.
51
	 *
52
	 * @param string[] ...$args IXR args.
53
	 *
54
	 * @return bool True if request succeeded, false otherwise.
55
	 */
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be string[][]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
	public function query( ...$args ) {
57
		$method  = array_shift( $args );
58
		$request = new IXR_Request( $method, $args );
59
		$xml     = trim( $request->getXml() );
60
61
		$response = Client::remote_request( $this->jetpack_args, $xml );
62
63
		if ( is_wp_error( $response ) ) {
64
			$this->error = new IXR_Error( -10520, sprintf( 'Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message() ) );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
			return false;
66
		}
67
68
		if ( ! $response ) {
69
			$this->error = new IXR_Error( -10520, 'Jetpack: Unknown Error' );
70
			return false;
71
		}
72
73
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
74
			$this->error = new IXR_Error( -32300, 'transport error - HTTP status code was not 200' );
75
			return false;
76
		}
77
78
		$content = wp_remote_retrieve_body( $response );
79
80
		// Now parse what we've got back.
81
		$this->message = new IXR_Message( $content );
82
		if ( ! $this->message->parse() ) {
83
			// XML error.
84
			$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
85
			return false;
86
		}
87
88
		// Is the message a fault?
89
		if ( 'fault' === $this->message->messageType ) {
90
			$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
91
			return false;
92
		}
93
94
		// Message must be OK.
95
		return true;
96
	}
97
98
	/**
99
	 * Retrieve the Jetpack error from the result of the last request.
100
	 *
101
	 * @param int    $fault_code   Fault code.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fault_code not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
102
	 * @param string $fault_string Fault string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fault_string not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
103
	 * @return WP_Error Error object.
104
	 */
105
	public function get_jetpack_error( $fault_code = null, $fault_string = null ) {
106
		if ( is_null( $fault_code ) ) {
107
			$fault_code = $this->error->code;
108
		}
109
110
		if ( is_null( $fault_string ) ) {
111
			$fault_string = $this->error->message;
112
		}
113
114
		if ( preg_match( '#jetpack:\s+\[(\w+)\]\s*(.*)?$#i', $fault_string, $match ) ) {
115
			$code    = $match[1];
116
			$message = $match[2];
117
			$status  = $fault_code;
118
			return new \WP_Error( $code, $message, $status );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $code.

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...
119
		}
120
121
		return new \WP_Error( "IXR_{$fault_code}", $fault_string );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with "IXR_{$fault_code}".

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...
122
	}
123
}
124