Skip to content

refactor(HTTP): split setCURLOptions into category helper methods to reduce complexity#10339

Open
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:refactor/setcurloptions
Open

refactor(HTTP): split setCURLOptions into category helper methods to reduce complexity#10339
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:refactor/setcurloptions

Conversation

@gr8man

@gr8man gr8man commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description

This PR refactors the massive setCURLOptions() method in CodeIgniter\HTTP\CURLRequest (which was a 180+ line method with extremely high cyclomatic complexity) by splitting it into 8 domain-specific private helper methods. This change adheres to the Single Responsibility Principle (SRP) and the Open-Closed Principle (OCP), making the options-building logic significantly cleaner, easier to read, and simpler to maintain without introducing any backward compatibility (BC) breaks.
Additionally, static analysis (PHPStan Level 6 with strict rules) and test validation have been integrated for all newly created helper methods.

Proposed Changes

  • system/HTTP/CURLRequest.php:
    • Extracted configuration building logic from setCURLOptions() into:
      • applyAuthOptions()
      • applySslOptions()
      • applyProxyOptions()
      • applyDebugOptions()
      • applyRedirectOptions()
      • applyConnectionOptions()
      • applyPayloadOptions()
      • applyMiscOptions()
    • Replaced legacy empty() checks with strict comparison operators (isset(), !== '', etc.) inside the new helper methods to comply with strict PHPStan rules.
    • Added full PHPDoc type annotations for arrays (solving missingType.iterableValue warnings).
  • tests/system/HTTP/CURLRequestTest.php:
    • Added direct reflection-based unit tests (testApply*Direct) using getPrivateMethodInvoker for all 8 helper methods to ensure maximum test coverage.
    • Adjusted float assertions from assertSame to assertEqualsWithDelta to satisfy Rector rules.
  • utils/phpstan-baseline/empty.notAllowed.neon:
    • Updated the baseline count for allowed empty() constructs for CURLRequest to match the new strict refactoring.

Checklist

  • All unit tests pass successfully (OK (100 tests, 269 assertions)).
  • PHPStan analysis passes with no errors ([OK] No errors).
  • Rector analysis passes with no errors.
  • Code formatting conforms to project standards (php-cs-fixer).
  • No changelog entry added (refactoring only, as there are no real changes for end users).

@gr8man gr8man force-pushed the refactor/setcurloptions branch from 0ea2fd3 to 2c5d21c Compare June 24, 2026 19:24

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the general idea, this will make the codebase better. But...

applyPayloadOptions() and applyMiscOptions() are trying to do too many things. I would reorganize these methods into four groups:

applyBodyOptions() // form_params, multipart, json
applyResponseOptions() // decode_content, http_errors
applyProtocolOptions() // HTTP version
applyClientOptions() // cookie, user_agent

I would prefer testing the observable behavior through setCURLOptions() or the public request API rather than testing every private helper directly. That keeps the tests stable if these internal groups are reorganized later.

private function applyAuthOptions(array $curlOptions, array $config): array
{
// Auth Headers
if (isset($config['auth']) && $config['auth'] !== []) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($config['auth']) && $config['auth'] !== []) {
if (isset($config['auth']) && is_array($config['auth']) && count($config['auth']) >= 2) {

{
// Certificate
if (! empty($config['cert'])) {
if (isset($config['cert']) && $config['cert'] !== '' && $config['cert'] !== []) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($config['cert']) && $config['cert'] !== '' && $config['cert'] !== []) {
$cert = $config['cert'] ?? null;
if ((bool) $cert) {

{
// Debug
if ($config['debug']) {
if (isset($config['debug']) && $config['debug'] !== false && $config['debug'] !== '') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($config['debug']) && $config['debug'] !== false && $config['debug'] !== '') {
if ((bool) ($config['debug'] ?? false)) {

private function applyPayloadOptions(array $curlOptions, array $config): array
{
// Decode Content
if (isset($config['decode_content']) && $config['decode_content'] !== false) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($config['decode_content']) && $config['decode_content'] !== false) {
if ((bool) ($config['decode_content'] ?? false)) {


// version
if (! empty($config['version'])) {
if (isset($config['version']) && $config['version'] !== '') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (isset($config['version']) && $config['version'] !== '') {
$version = $config['version'] ?? null;
if ((bool) $version) {

Comment on lines -42 to -43
parent::setUp();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this a mistake? Why are you removing this?

@michalsn michalsn added the refactor Pull requests that refactor code label Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Pull requests that refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants