Files
php-curl-class/src/Curl/BaseCurl.php
T
Zach Borboa 4cf4635e25 Increase psalm strictness (#925)
* Fix PossiblyUndefinedArrayOffset

ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/Curl.php:1890:28 -
Possibly undefined array key (see https://psalm.dev/167)
                list($key, $value) = explode(':', $raw_headers[$i], 2);

* Fix PossiblyUndefinedArrayOffset

ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/Curl.php:1034:28 -
Possibly undefined array key (see https://psalm.dev/167)
                list($key, $value) = explode(':', $header, 2);

* Fix PossiblyUndefinedArrayOffset

ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/MultiCurl.php:465:28 -
Possibly undefined array key (see https://psalm.dev/167)
                list($key, $value) = explode(':', $header, 2);

* Fix PossiblyInvalidOperand

ERROR: PossiblyInvalidOperand - ../src/Curl/Curl.php:1407:69 - Cannot
concatenate with a int<0, max>|string (see https://psalm.dev/163)
                    echo 'Response content length (calculated): ' . $response_calculated_length . "\n";

* Fix PossiblyInvalidArgument

ERROR: PossiblyInvalidArgument - ../src/Curl/BaseCurl.php:408:34 -
Argument 2 of define expects array<array-key, mixed>|null|scalar, but
possibly different type false|resource provided (see
https://psalm.dev/092)
                define('STDERR', fopen('php://stderr', 'wb'));

* Fix ArgumentTypeCoercion

ERROR: ArgumentTypeCoercion - ../src/Curl/MultiCurl.php:956:16 -
Argument 1 of usleep expects int<0, max>, but parent type int provided
(see https://psalm.dev/193)
        usleep((int) $sleep_seconds * 1000000);

* Fix PossiblyFalseArgument

ERROR: PossiblyFalseArgument - ../src/Curl/MultiCurl.php:131:50 -
Argument 2 of file_put_contents cannot be false, possibly
array<array-key, string>|resource|string value expected (see
https://psalm.dev/104)
                    file_put_contents($filename, stream_get_contents($fh));

* Fix PossiblyFalseArgument

ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:437:49 - Argument 2
of stream_copy_to_stream cannot be false, possibly resource value
expected (see https://psalm.dev/104)
                stream_copy_to_stream($tmpfile, $fh);

ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:438:24 - Argument 1
of fclose cannot be false, possibly resource value expected (see
https://psalm.dev/104)
                fclose($fh);

* Fix PossiblyFalseArgument

ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:463:49 - Argument 2
of stream_copy_to_stream cannot be false, possibly resource value
expected (see https://psalm.dev/104)
            stream_copy_to_stream($file_handle, $main_file_handle);

ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:468:16 - Argument 1
of fclose cannot be false, possibly resource value expected (see
https://psalm.dev/104)
        fclose($main_file_handle);

* Fix PossiblyFalseArgument

ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:1891:36 - Argument 1
of count cannot be false, possibly Countable|array<array-key, mixed>
value expected (see https://psalm.dev/104)
        $raw_headers_count = count($raw_headers);

ERROR: PossiblyInvalidArrayAccess - ../src/Curl/Curl.php:1893:24 -
Cannot access array value on non-array variable $raw_headers of type
false (see https://psalm.dev/109)
            if (strpos($raw_headers[$i], ':') !== false) {

* Fix PossiblyInvalidArgument

ERROR: PossiblyInvalidArgument - ../src/Curl/Curl.php:1868:30 - Argument
2 of define expects array<array-key, mixed>|null|scalar, but possibly
different type false|resource provided (see https://psalm.dev/092)
            define('STDOUT', fopen('php://stdout', 'w'));

* Update psalm baseline
2025-02-19 21:40:56 -05:00

423 lines
9.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Curl;
abstract class BaseCurl
{
public $beforeSendCallback = null;
public $afterSendCallback = null;
public $successCallback = null;
public $errorCallback = null;
public $completeCallback = null;
protected $options = [];
protected $userSetOptions = [];
/**
* Before Send
*
* @param $callback callable|null
*/
public function beforeSend($callback)
{
$this->beforeSendCallback = $callback;
}
abstract public function close();
/**
* Complete
*
* @param $callback callable|null
*/
public function complete($callback)
{
$this->completeCallback = $callback;
}
/**
* Disable Timeout
*/
public function disableTimeout()
{
$this->setTimeout(null);
}
/**
* Error
*
* @param $callback callable|null
*/
public function error($callback)
{
$this->errorCallback = $callback;
}
/**
* Get Opt
*
* @param $option
* @return mixed
*/
public function getOpt($option)
{
return $this->options[$option] ?? null;
}
/**
* Remove Header
*
* Remove an internal header from the request.
* Using `curl -H "Host:" ...' is equivalent to $curl->removeHeader('Host');.
*
* @param $key
*/
public function removeHeader($key)
{
$this->setHeader($key, '');
}
/**
* Set auto referer
*
* @param mixed $auto_referer
*/
public function setAutoReferer($auto_referer = true)
{
$this->setAutoReferrer($auto_referer);
}
/**
* Set auto referrer
*
* @param mixed $auto_referrer
*/
public function setAutoReferrer($auto_referrer = true)
{
$this->setOpt(CURLOPT_AUTOREFERER, $auto_referrer);
}
/**
* Set Basic Authentication
*
* @param $username
* @param $password
*/
public function setBasicAuthentication($username, $password = '')
{
$this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
}
/**
* Set Connect Timeout
*
* @param $seconds
*/
public function setConnectTimeout($seconds)
{
$this->setOpt(CURLOPT_CONNECTTIMEOUT, $seconds);
}
abstract public function setCookie($key, $value);
abstract public function setCookieFile($cookie_file);
abstract public function setCookieJar($cookie_jar);
abstract public function setCookieString($string);
abstract public function setCookies($cookies);
/**
* Set Digest Authentication
*
* @param $username
* @param $password
*/
public function setDigestAuthentication($username, $password = '')
{
$this->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$this->setOpt(CURLOPT_USERPWD, $username . ':' . $password);
}
/**
* After Send
*
* This function is called after the request has been sent.
*
* It can be used to override whether or not the request errored. The
* instance is passed as the first argument to the function and the instance
* has attributes like $instance->httpStatusCode and $instance->response to
* help decide if the request errored. Set $instance->error to true or false
* within the function.
*
* When $instance->error is true indicating a request error, the error
* callback set by Curl::error() is called. When $instance->error is false,
* the success callback set by Curl::success() is called.
*
* @param $callback callable|null
*/
public function afterSend($callback)
{
$this->afterSendCallback = $callback;
}
/**
* Set File
*
* @param $file
*/
public function setFile($file)
{
$this->setOpt(CURLOPT_FILE, $file);
}
protected function setFileInternal($file)
{
$this->setOptInternal(CURLOPT_FILE, $file);
}
/**
* Set follow location
*
* @param mixed $follow_location
* @see Curl::setMaximumRedirects()
*/
public function setFollowLocation($follow_location = true)
{
$this->setOpt(CURLOPT_FOLLOWLOCATION, $follow_location);
}
/**
* Set forbid reuse
*
* @param mixed $forbid_reuse
*/
public function setForbidReuse($forbid_reuse = true)
{
$this->setOpt(CURLOPT_FORBID_REUSE, $forbid_reuse);
}
abstract public function setHeader($key, $value);
abstract public function setHeaders($headers);
/**
* Set Interface
*
* The name of the outgoing network interface to use.
* This can be an interface name, an IP address or a host name.
*
* @param $interface
*/
public function setInterface($interface)
{
$this->setOpt(CURLOPT_INTERFACE, $interface);
}
abstract public function setJsonDecoder($mixed);
/**
* Set maximum redirects
*
* @param mixed $maximum_redirects
* @see Curl::setFollowLocation()
*/
public function setMaximumRedirects($maximum_redirects)
{
$this->setOpt(CURLOPT_MAXREDIRS, $maximum_redirects);
}
abstract public function setOpt($option, $value);
protected function setOptInternal($option, $value)
{
}
abstract public function setOpts($options);
/**
* Set Port
*
* @param $port
*/
public function setPort($port)
{
$this->setOpt(CURLOPT_PORT, (int) $port);
}
/**
* Set Proxy
*
* Set an HTTP proxy to tunnel requests through.
*
* @param $proxy - The HTTP proxy to tunnel requests through. May include port number.
* @param $port - The port number of the proxy to connect to. This port number can also be set in $proxy.
* @param $username - The username to use for the connection to the proxy.
* @param $password - The password to use for the connection to the proxy.
*/
public function setProxy($proxy, $port = null, $username = null, $password = null)
{
$this->setOpt(CURLOPT_PROXY, $proxy);
if ($port !== null) {
$this->setOpt(CURLOPT_PROXYPORT, $port);
}
if ($username !== null && $password !== null) {
$this->setOpt(CURLOPT_PROXYUSERPWD, $username . ':' . $password);
}
}
/**
* Set Proxy Auth
*
* Set the HTTP authentication method(s) to use for the proxy connection.
*
* @param $auth
*/
public function setProxyAuth($auth)
{
$this->setOpt(CURLOPT_PROXYAUTH, $auth);
}
/**
* Set Proxy Tunnel
*
* Set the proxy to tunnel through HTTP proxy.
*
* @param $tunnel boolean
*/
public function setProxyTunnel($tunnel = true)
{
$this->setOpt(CURLOPT_HTTPPROXYTUNNEL, $tunnel);
}
/**
* Set Proxy Type
*
* Set the proxy protocol type.
*
* @param $type
*/
public function setProxyType($type)
{
$this->setOpt(CURLOPT_PROXYTYPE, $type);
}
/**
* Set Range
*
* @param $range
*/
public function setRange($range)
{
$this->setOpt(CURLOPT_RANGE, $range);
}
protected function setRangeInternal($range)
{
$this->setOptInternal(CURLOPT_RANGE, $range);
}
/**
* Set Referer
*
* @param $referer
*/
public function setReferer($referer)
{
$this->setReferrer($referer);
}
/**
* Set Referrer
*
* @param $referrer
*/
public function setReferrer($referrer)
{
$this->setOpt(CURLOPT_REFERER, $referrer);
}
abstract public function setRetry($mixed);
/**
* Set Timeout
*
* @param $seconds
*/
public function setTimeout($seconds)
{
$this->setOpt(CURLOPT_TIMEOUT, $seconds);
}
protected function setTimeoutInternal($seconds)
{
$this->setOptInternal(CURLOPT_TIMEOUT, $seconds);
}
abstract public function setUrl($url, $mixed_data = '');
/**
* Set User Agent
*
* @param $user_agent
*/
public function setUserAgent($user_agent)
{
$this->setOpt(CURLOPT_USERAGENT, $user_agent);
}
protected function setUserAgentInternal($user_agent)
{
$this->setOptInternal(CURLOPT_USERAGENT, $user_agent);
}
abstract public function setXmlDecoder($mixed);
abstract public function stop();
/**
* Success
*
* @param $callback callable|null
*/
public function success($callback)
{
$this->successCallback = $callback;
}
abstract public function unsetHeader($key);
/**
* Unset Proxy
*
* Disable use of the proxy.
*/
public function unsetProxy()
{
$this->setOpt(CURLOPT_PROXY, null);
}
/**
* Verbose
*
* @param bool $on
* @param resource|string $output
*/
public function verbose($on = true, $output = 'STDERR')
{
if ($output === 'STDERR') {
if (defined('STDERR')) {
$output = STDERR;
} else {
$output = fopen('php://stderr', 'wb');
}
}
// Turn off CURLINFO_HEADER_OUT for verbose to work. This has the side
// effect of causing Curl::requestHeaders to be empty.
if ($on) {
$this->setOpt(CURLINFO_HEADER_OUT, false);
}
$this->setOpt(CURLOPT_VERBOSE, $on);
$this->setOpt(CURLOPT_STDERR, $output);
}
}