Client::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Pilipinews\Common;
4
5
/**
6
 * Client
7
 *
8
 * @package Pilipinews
9
 * @author  Rougin Gutib <[email protected]>
10
 */
11
class Client
12
{
13
    const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36';
14
15
    /**
16
     * @var resource
17
     */
18
    protected $curl;
19
20
    /**
21
     * Initializes the cURL session.
22
     */
23 15
    public function __construct()
24
    {
25 15
        $curl = curl_init();
26
27 15
        curl_setopt($curl, CURLOPT_ENCODING, '');
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_ENCODING, '');
Loading history...
28
29 15
        curl_setopt($curl, CURLOPT_USERAGENT, self::USER_AGENT);
30
31 15
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
32
33 15
        $this->curl = $curl;
0 ignored issues
show
Documentation Bug introduced by
It seems like $curl can also be of type false. 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...
34 15
    }
35
36
    /**
37
     * Performs the cURL session.
38
     *
39
     * @param  boolean $close
40
     * @return mixed
41
     */
42 15
    public function execute($close = true)
43
    {
44 15
        $result = curl_exec($this->curl);
45
46 15
        $close && curl_close($this->curl);
47
48 15
        return $result;
49
    }
50
51
    /**
52
     * Sets an option to the cURL session.
53
     *
54
     * @param  integer $key
55
     * @param  mixed   $value
56
     * @return self
57
     */
58 15
    public function set($key, $value)
59
    {
60 15
        curl_setopt($this->curl, $key, $value);
61
62 15
        return $this;
63
    }
64
65
    /**
66
     * Sets the URL to be used in the cURL session.
67
     *
68
     * @param  string $link
69
     * @return self
70
     */
71 15
    public function url($link)
72
    {
73 15
        return $this->set(CURLOPT_URL, $link);
74
    }
75
76
    /**
77
     * Performs a new cURL session with an URL.
78
     *
79
     * @param  string $url
80
     * @return string
81
     */
82 15
    public static function request($url)
83
    {
84 15
        $curl = new static;
85
86 15
        $curl->url((string) $url);
87
88 15
        return $curl->execute();
89
    }
90
}
91