WP_SimplePie_File   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 36 5
1
<?php
2
/**
3
 * Feed API: WP_SimplePie_File class
4
 *
5
 * @package WordPress
6
 * @subpackage Feed
7
 * @since 4.7.0
8
 */
9
10
/**
11
 * Core class for fetching remote files and reading local files with SimplePie.
12
 *
13
 * @since 2.8.0
14
 *
15
 * @see SimplePie_File
16
 */
17
class WP_SimplePie_File extends SimplePie_File {
18
19
	/**
20
	 * Constructor.
21
	 *
22
	 * @since 2.8.0
23
	 * @since 3.2.0 Updated to use a PHP5 constructor.
24
	 * @access public
25
	 *
26
	 * @param string       $url             Remote file URL.
27
	 * @param integer      $timeout         Optional. How long the connection should stay open in seconds.
28
	 *                                      Default 10.
29
	 * @param integer      $redirects       Optional. The number of allowed redirects. Default 5.
30
	 * @param string|array $headers         Optional. Array or string of headers to send with the request.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $headers not be string|array|null? Also, consider making the array more specific, something like array<String>, or 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. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

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

Loading history...
31
	 *                                      Default null.
32
	 * @param string       $useragent       Optional. User-agent value sent. Default null.
33
	 * @param boolean      $force_fsockopen Optional. Whether to force opening internet or unix domain socket
34
	 *                                      connection or not. Default false.
35
	 */
36
	public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
37
		$this->url = $url;
38
		$this->timeout = $timeout;
0 ignored issues
show
Bug introduced by
The property timeout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		$this->redirects = $redirects;
40
		$this->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers can also be of type string or null. However, the property $headers is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
		$this->useragent = $useragent;
42
43
		$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE;
44
45
		if ( preg_match('/^http(s)?:\/\//i', $url) ) {
46
			$args = array(
47
				'timeout' => $this->timeout,
48
				'redirection' => $this->redirects,
49
			);
50
51
			if ( !empty($this->headers) )
52
				$args['headers'] = $this->headers;
53
54
			if ( SIMPLEPIE_USERAGENT != $this->useragent ) //Use default WP user agent unless custom has been specified
55
				$args['user-agent'] = $this->useragent;
56
57
			$res = wp_safe_remote_request($url, $args);
58
59
			if ( is_wp_error($res) ) {
60
				$this->error = 'WP HTTP Error: ' . $res->get_error_message();
61
				$this->success = false;
62
			} else {
63
				$this->headers = wp_remote_retrieve_headers( $res );
0 ignored issues
show
Documentation Bug introduced by
It seems like wp_remote_retrieve_headers($res) can also be of type object<Requests_Utility_...eInsensitiveDictionary>. However, the property $headers is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64
				$this->body = wp_remote_retrieve_body( $res );
65
				$this->status_code = wp_remote_retrieve_response_code( $res );
66
			}
67
		} else {
68
			$this->error = '';
69
			$this->success = false;
70
		}
71
	}
72
}
73