Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: prevent using empty() on CURLRequest #9195

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -5473,12 +5473,6 @@
'count' => 1,
'path' => __DIR__ . '/system/HTTP/CLIRequest.php',
];
$ignoreErrors[] = [
// identifier: empty.notAllowed
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 10,
'path' => __DIR__ . '/system/HTTP/CURLRequest.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Method CodeIgniter\\\\HTTP\\\\CURLRequest\\:\\:applyBody\\(\\) has parameter \\$curlOptions with no value type specified in iterable type array\\.$#',
Expand Down
24 changes: 13 additions & 11 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function send(string $method, string $url)
// Reset our curl options so we're on a fresh slate.
$curlOptions = [];

if (! empty($this->config['query']) && is_array($this->config['query'])) {
if (array_key_exists('query', $this->config) && is_array($this->config['query']) && $this->config['query'] !== []) {
// This is likely too naive a solution.
// Should look into handling when $url already
// has query vars on it.
Expand Down Expand Up @@ -422,7 +422,7 @@ public function send(string $method, string $url)
*/
protected function applyRequestHeaders(array $curlOptions = []): array
{
if (empty($this->headers)) {
if ($this->headers === []) {
return $curlOptions;
}

Expand Down Expand Up @@ -469,8 +469,10 @@ protected function applyMethod(string $method, array $curlOptions): array
*/
protected function applyBody(array $curlOptions = []): array
{
if (! empty($this->body)) {
$curlOptions[CURLOPT_POSTFIELDS] = (string) $this->getBody();
$requestBody = (string) $this->getBody();

if ($requestBody !== '') {
$curlOptions[CURLOPT_POSTFIELDS] = $requestBody;
}

return $curlOptions;
Expand Down Expand Up @@ -518,18 +520,18 @@ protected function setResponseHeaders(array $headers = [])
protected function setCURLOptions(array $curlOptions = [], array $config = [])
{
// Auth Headers
if (! empty($config['auth'])) {
if (array_key_exists('auth', $config) && is_array($config['auth']) && $config['auth'] !== []) {
$curlOptions[CURLOPT_USERPWD] = $config['auth'][0] . ':' . $config['auth'][1];

if (! empty($config['auth'][2]) && strtolower($config['auth'][2]) === 'digest') {
if (isset($this->config['auth'][2]) && $this->config['auth'][2] === 'digest') {
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
} else {
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
}
}

// Certificate
if (! empty($config['cert'])) {
if (array_key_exists('cert', $config) && $config['cert']) {
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
$cert = $config['cert'];

if (is_array($cert)) {
Expand Down Expand Up @@ -575,7 +577,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}

// Decode Content
if (! empty($config['decode_content'])) {
if (array_key_exists('decode_content', $config) && $config['decode_content']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for right side, pls be more specific what we are checking against

$accept = $this->getHeaderLine('Accept-Encoding');

if ($accept !== '') {
Expand Down Expand Up @@ -621,7 +623,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
$curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = (float) $config['connect_timeout'] * 1000;

// Post Data - application/x-www-form-urlencoded
if (! empty($config['form_params']) && is_array($config['form_params'])) {
if (array_key_exists('form_params', $config) && is_array($config['form_params']) && $config['form_params'] !== []) {
$postFields = http_build_query($config['form_params']);
$curlOptions[CURLOPT_POSTFIELDS] = $postFields;

Expand All @@ -632,7 +634,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}

// Post Data - multipart/form-data
if (! empty($config['multipart']) && is_array($config['multipart'])) {
if (array_key_exists('multipart', $config) && is_array($config['multipart']) && $config['multipart'] !== []) {
// setting the POSTFIELDS option automatically sets multipart
$curlOptions[CURLOPT_POSTFIELDS] = $config['multipart'];
}
Expand All @@ -650,7 +652,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}

// version
if (! empty($config['version'])) {
if (array_key_exists('version', $config) && ((is_string($config['version']) && $config['version'] !== '') || (is_numeric($config['version']) && $config['version'] !== 0))) {
$version = sprintf('%.1F', $config['version']);
if ($version === '1.0') {
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
Expand Down
Loading