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 2 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 @@ -5533,12 +5533,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
20 changes: 10 additions & 10 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,7 +469,7 @@ protected function applyMethod(string $method, array $curlOptions): array
*/
protected function applyBody(array $curlOptions = []): array
{
if (! empty($this->body)) {
if ($this->body !== '' && $this->body !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

can we just cast to string then check against empty string?

$curlOptions[CURLOPT_POSTFIELDS] = (string) $this->getBody();
}

Expand Down Expand Up @@ -518,18 +518,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']) {
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 of && we can check against empty array or empty string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

:532 Result of || is always true.
🪪 booleanOr.alwaysTrue
:532 Strict comparison using !== between array{} and '' will always evaluate to true.
🪪 notIdentical.alwaysTrue

$cert = $config['cert'];

if (is_array($cert)) {
Expand Down Expand Up @@ -575,7 +575,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 +621,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 +632,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 +650,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}

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

Choose a reason for hiding this comment

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

right side change to is_string and !== ''?

$version = sprintf('%.1F', $config['version']);
if ($version === '1.0') {
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
Expand Down
Loading