pilipinews /
common
| 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
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
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 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 |