Skip to content

Commit

Permalink
psalm level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sad-spirit committed Nov 1, 2023
1 parent 4d683b1 commit 128fc9d
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 95 deletions.
58 changes: 31 additions & 27 deletions HTTP/Request2.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ class HTTP_Request2 implements SplSubject
/**
* Fileinfo magic database resource
*
* @var resource
* @var resource|null
* @see detectMimeType()
*/
private static $_fileinfoDb;
private static $_fileinfoDb = null;

/**
* Observers attached to the request (instances of SplObserver)
Expand All @@ -118,7 +118,7 @@ class HTTP_Request2 implements SplSubject
/**
* Authentication data
*
* @var array
* @var null|array{user: string, password: string, scheme: string}
* @see getAuth()
*/
protected $auth;
Expand Down Expand Up @@ -181,7 +181,7 @@ class HTTP_Request2 implements SplSubject
/**
* Request body
*
* @var string|resource
* @var string|resource|HTTP_Request2_MultipartBody
* @see setBody()
*/
protected $body = '';
Expand Down Expand Up @@ -210,7 +210,7 @@ class HTTP_Request2 implements SplSubject
/**
* Cookie jar to persist cookies between requests
*
* @var HTTP_Request2_CookieJar
* @var HTTP_Request2_CookieJar|null
*/
protected $cookieJar = null;

Expand Down Expand Up @@ -266,9 +266,11 @@ public function setUrl($url)
}
// URL contains username / password?
if ($url->getUserinfo()) {
$username = $url->getUser();
$password = $url->getPassword();
$this->setAuth(rawurldecode($username), $password? rawurldecode($password): '');
$this->setAuth(
rawurldecode((string)$url->getUser()),
$password? rawurldecode((string)$password): ''
);
$url->setUserinfo('');
}
if ('' == $url->getPath()) {
Expand Down Expand Up @@ -382,15 +384,15 @@ public function setConfig($nameOrConfig, $value = null)
$this->setConfig($name, $value);
}

} elseif ('proxy' == $nameOrConfig) {
$url = new Net_URL2($value);
} elseif ('proxy' === $nameOrConfig) {
$url = new Net_URL2((string)$value);
$this->setConfig(
[
'proxy_type' => $url->getScheme(),
'proxy_host' => $url->getHost(),
'proxy_port' => $url->getPort(),
'proxy_user' => rawurldecode($url->getUser()),
'proxy_password' => rawurldecode($url->getPassword())
'proxy_user' => rawurldecode((string)$url->getUser()),
'proxy_password' => rawurldecode((string)$url->getPassword())
]
);

Expand Down Expand Up @@ -443,6 +445,7 @@ public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC)
if (empty($user)) {
$this->auth = null;
} else {
/** @psalm-suppress RedundantCast */
$this->auth = [
'user' => (string)$user,
'password' => (string)$password,
Expand All @@ -459,7 +462,7 @@ public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC)
* The array has the keys 'user', 'password' and 'scheme', where 'scheme'
* is one of the HTTP_Request2::AUTH_* constants.
*
* @return array
* @return array{user: string, password: string, scheme: string}|null
*/
public function getAuth()
{
Expand Down Expand Up @@ -506,6 +509,7 @@ public function setHeader($name, $value = null, $replace = true)
}
} else {
if (null === $value && strpos($name, ':')) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
list($name, $value) = array_map('trim', explode(':', $name, 2));
}
// Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2
Expand Down Expand Up @@ -607,18 +611,16 @@ public function addCookie($name, $value)
*/
public function setBody($body, $isFilename = false)
{
if (!$isFilename && !is_resource($body)) {
if (!$body instanceof HTTP_Request2_MultipartBody) {
$this->body = (string)$body;
} else {
$this->body = $body;
}
} else {
if ($body instanceof HTTP_Request2_MultipartBody) {
$this->body = $body;
} elseif ($isFilename || is_resource($body)) {
$fileData = $this->fopenWrapper($body, empty($this->headers['content-type']));
$this->body = $fileData['fp'];
if (empty($this->headers['content-type'])) {
$this->setHeader('content-type', $fileData['type']);
}
} else {
$this->body = (string)$body;
}
$this->postParams = $this->uploads = [];

Expand Down Expand Up @@ -900,8 +902,8 @@ public function setAdapter($adapter)
* responses. Cookies from jar will be automatically added to the request
* headers based on request URL.
*
* @param HTTP_Request2_CookieJar|bool $jar Existing CookieJar object, true to
* create a new one, false to remove
* @param HTTP_Request2_CookieJar|bool|null $jar Existing CookieJar object, true to
* create a new one, false/null to remove
*
* @return $this
* @throws HTTP_Request2_LogicException
Expand Down Expand Up @@ -947,7 +949,7 @@ public function send()
// Sanity check for URL
if (!$this->url instanceof Net_URL2
|| !$this->url->isAbsolute()
|| !in_array(strtolower($this->url->getScheme()), ['https', 'http'])
|| !in_array(strtolower((string)$this->url->getScheme()), ['https', 'http'])
) {
throw new HTTP_Request2_LogicException(
'HTTP_Request2 needs an absolute HTTP(S) request URL, '
Expand All @@ -962,7 +964,7 @@ public function send()
}
// force using single byte encoding if mbstring extension overloads
// strlen() and substr(); see bug #1781, bug #10605
if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) {
if (extension_loaded('mbstring') && (2 & (int)ini_get('mbstring.func_overload'))) {
$oldEncoding = mb_internal_encoding();
mb_internal_encoding('8bit');
}
Expand All @@ -984,7 +986,7 @@ public function send()
* type of file, will only work if $file is a
* filename, not pointer
*
* @return array array('fp' => file pointer, 'size' => file size, 'type' => MIME type)
* @return array{fp: resource, size: int, type: string}
* @throws HTTP_Request2_LogicException
*/
protected function fopenWrapper($file, $detectType = false)
Expand All @@ -996,15 +998,17 @@ protected function fopenWrapper($file, $detectType = false)
);
}
$fileData = [
'fp' => is_string($file)? null: $file,
'type' => 'application/octet-stream',
'size' => 0
];
if (is_string($file)) {
if (is_resource($file)) {
$fileData['fp'] = $file;
} else {
if (!($fileData['fp'] = @fopen($file, 'rb'))) {
$error = error_get_last();
throw new HTTP_Request2_LogicException(
$error['message'], HTTP_Request2_Exception::READ_ERROR
$error ? $error['message'] : "fopen() error",
HTTP_Request2_Exception::READ_ERROR
);
}
if ($detectType) {
Expand Down
10 changes: 6 additions & 4 deletions HTTP/Request2/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
/**
* Mapping of CURLE_* constants to Exception subclasses and error codes
*
* @var array
* @var array<int, array{0: class-string<HTTP_Request2_Exception>, 1?: int}>
*/
protected static $errorMap = [
CURLE_UNSUPPORTED_PROTOCOL => [HTTP_Request2_MessageException::class,
Expand Down Expand Up @@ -103,7 +103,7 @@ class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
/**
* Response being received
*
* @var HTTP_Request2_Response
* @var HTTP_Request2_Response|null
*/
protected $response;

Expand Down Expand Up @@ -498,7 +498,9 @@ protected function callbackWriteHeader($ch, $string)
if (!$this->eventSentHeaders
// we may receive a second set of headers if doing e.g. digest auth
// but don't bother with 100-Continue responses (bug #15785)
|| $this->eventReceivedHeaders && $this->response->getStatus() >= 200
|| $this->eventReceivedHeaders
&& null !== $this->response
&& $this->response->getStatus() >= 200
) {
$this->request->setLastEvent(
'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)
Expand Down Expand Up @@ -534,7 +536,7 @@ protected function callbackWriteHeader($ch, $string)
}

if ($this->request->getConfig('follow_redirects') && $this->response->isRedirect()) {
$redirectUrl = new Net_URL2($this->response->getHeader('location'));
$redirectUrl = new Net_URL2((string)$this->response->getHeader('location'));

// for versions lower than 5.2.10, check the redirection URL protocol
if (!defined('CURLOPT_REDIR_PROTOCOLS') && $redirectUrl->isAbsolute()
Expand Down
4 changes: 2 additions & 2 deletions HTTP/Request2/Adapter/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
/**
* A queue of responses to be returned by sendRequest()
*
* @var array<int, array{HTTP_Request2_Response|HTTP_Request2_Exception, ?string}>
* @var array<int, array{HTTP_Request2_Response|Exception, ?string}>
*/
protected $responses = [];

Expand Down Expand Up @@ -83,7 +83,7 @@ public function sendRequest(HTTP_Request2 $request)

if ($response instanceof HTTP_Request2_Response) {
return $response;
} elseif ($response instanceof HTTP_Request2_Exception) {
} elseif ($response instanceof Exception) {
// rethrow the exception
$class = get_class($response);
$message = $response->getMessage();
Expand Down
Loading

0 comments on commit 128fc9d

Please sign in to comment.