CurlRequest::setOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Curl request class
4
 *
5
 * @file      CurlRequest.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2015-01-20 18:12
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\CurlRequest;
17
18
use Veles\CurlRequest\AuthStrategies\AuthStrategyInterface;
19
20
/**
21
 * Class CurlRequest
22
 * @author  Yancharuk Alexander <alex at itvault dot info>
23
 */
24
class CurlRequest extends CurlAbstract
25
{
26
	/**
27
	 * Creates cURL handler and sets options
28
	 *
29
	 * @param       $url
30
	 * @param array $options
31
	 */
32 14
	public function __construct($url, array $options = [])
33
	{
34 14
		$this->curl = curl_init();
0 ignored issues
show
Documentation Bug introduced by
It seems like curl_init() can also be of type CurlHandle. However, the property $curl is declared as type resource. 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...
35 14
		$options   += $this->default_options;
36
37 14
		$this->setOption(CURLOPT_URL, $url)
38 14
			->setArrayOptions($options);
39
	}
40
41 1
	public function __destruct()
42
	{
43 1
		curl_close($this->curl);
44
	}
45
46
	/**
47
	 * Executes request and by default returns result as string
48
	 *
49
	 * @return mixed
50
	 */
51 1
	public function exec()
52
	{
53 1
		return curl_exec($this->curl);
54
	}
55
56
	/**
57
	 * Sets additional header for request
58
	 *
59
	 * @param array $headers Array of additional headers
60
	 *
61
	 * @return $this
62
	 */
63 3
	public function setHeaders(array $headers)
64
	{
65 3
		$this->options[CURLOPT_HTTPHEADER] = $headers;
66 3
		curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
67
68 3
		return $this;
69
	}
70
71
	/**
72
	 * Get headers set for prepared request
73
	 *
74
	 * @return array
75
	 */
76 3
	public function getHeaders()
77
	{
78 3
		return isset($this->options[CURLOPT_HTTPHEADER])
79 3
			? $this->options[CURLOPT_HTTPHEADER]
80 3
			: [];
81
	}
82
83
	/**
84
	 * Set curl option
85
	 *
86
	 * @param $option
87
	 * @param $value
88
	 *
89
	 * @return $this
90
	 */
91 14
	public function setOption($option, $value)
92
	{
93 14
		$this->options[$option] = $value;
94 14
		curl_setopt($this->curl, $option, $value);
95
96 14
		return $this;
97
	}
98
99
	/**
100
	 * Sets additional options to curl-request
101
	 *
102
	 * @param array $options
103
	 *
104
	 * @return $this
105
	 */
106 14
	public function setArrayOptions(array $options)
107
	{
108 14
		foreach ($options as $option => $value) {
109 14
			$this->options[$option] = $value;
110
		}
111
112 14
		curl_setopt_array($this->curl, $options);
113
114 14
		return $this;
115
	}
116
117
	/**
118
	 * Set request authentication strategy
119
	 *
120
	 * @param AuthStrategyInterface $auth Authentication strategy
121
	 *
122
	 * @return $this
123
	 */
124 1
	public function setAuth(AuthStrategyInterface $auth)
125
	{
126 1
		$auth->apply($this);
127 1
		$this->auth = $auth;
128
129 1
		return $this;
130
	}
131
}
132