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

Enhance Request Handling in Request.php +semver: minor #227

Merged
merged 4 commits into from
Oct 2, 2024

Conversation

guibranco
Copy link
Owner

@guibranco guibranco commented Oct 2, 2024

User description

Closes #

πŸ“‘ Description

βœ… Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

☒️ Does this introduce a breaking change?

  • Yes
  • No

β„Ή Additional Information


Description

  • Introduced requestHeaders in the response object for better debugging.
  • Improved method robustness by ensuring $data is checked for null before use.

Changes walkthrough πŸ“

Relevant files
Enhancement
Request.php
Enhance Request Handling in Request.phpΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β 

src/Request.php

  • Added requestHeaders to the result of the execute method.
  • Updated post, put, delete, and patch methods to check if $data is not
    null before assigning it to CURLOPT_POSTFIELDS.
  • +11/-4Β  Β 

    Summary by CodeRabbit

    • New Features

      • Enhanced HTTP request handling in the Request class to include response headers.
      • Added optional data parameter for post, put, delete, and patch methods to improve data handling.
    • Documentation

      • Updated changelog with enhancements for version 0.11, including contributions from @guibranco.
      • Improved clarity and structure in the user guide for HTTP requests, detailing usage for various methods with and without payloads.

    Copy link

    coderabbitai bot commented Oct 2, 2024

    Caution

    Review failed

    The pull request is closed.

    Walkthrough

    The changes in the Request class within the GuiBranco\Pancake namespace enhance HTTP request handling. The execute method now includes the requestHeaders property in its result, providing access to the HTTP headers from the request. Additionally, the post, put, delete, and patch methods have been updated to conditionally assign the $data parameter to CURLOPT_POSTFIELDS, ensuring that it is only set when $data is not null. These modifications improve the handling of request data and headers.

    Changes

    File Change Summary
    src/Request.php - Updated execute method to include requestHeaders in the result object.
    - Modified post, put, delete, and patch methods to check if $data is not null before assigning it to CURLOPT_POSTFIELDS.
    docs/changelog.md - Added version entry for 0.11, documenting the enhancement to the Request class with optional data parameter.
    docs/user-guide/request.md - Expanded "Table of content" with new subsections for various HTTP verbs detailing payload usage.
    - Updated sections for "Post," "Put," "Patch," and "Delete" to clarify requests with and without payloads.
    - Made grammatical adjustments for consistency.

    Possibly related PRs

    Suggested reviewers

    • gstraccini

    Poem

    In the fields where requests do play,
    Headers now join the data ballet.
    With checks in place, we dance with glee,
    No more nulls in our HTTP spree!
    Hops of joy for changes bright,
    Making requests a pure delight! πŸ‡βœ¨


    Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

    ❀️ Share
    πŸͺ§ Tips

    Chat

    There are 3 ways to chat with CodeRabbit:

    • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
      • I pushed a fix in commit <commit_id>, please review it.
      • Generate unit testing code for this file.
      • Open a follow-up GitHub issue for this discussion.
    • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
      • @coderabbitai generate unit testing code for this file.
      • @coderabbitai modularize this function.
    • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
      • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
      • @coderabbitai read src/utils.ts and generate unit testing code.
      • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
      • @coderabbitai help me debug CodeRabbit configuration file.

    Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

    CodeRabbit Commands (Invoked using PR comments)

    • @coderabbitai pause to pause the reviews on a PR.
    • @coderabbitai resume to resume the paused reviews.
    • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
    • @coderabbitai full review to do a full review from scratch and review all the files again.
    • @coderabbitai summary to regenerate the summary of the PR.
    • @coderabbitai resolve resolve all the CodeRabbit review comments.
    • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
    • @coderabbitai help to get help.

    Other keywords and placeholders

    • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
    • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
    • Add @coderabbitai anywhere in the PR title to generate the title automatically.

    CodeRabbit Configuration File (.coderabbit.yaml)

    • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
    • Please see the configuration documentation for more information.
    • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

    Documentation and Community

    • Visit our Documentation for detailed information on how to use CodeRabbit.
    • Join our Discord Community to get help, request features, and share feedback.
    • Follow us on X/Twitter for updates and announcements.

    @penify-dev penify-dev bot added the enhancement New feature or request label Oct 2, 2024
    @penify-dev penify-dev bot changed the title Update Request.php Enhance Request Handling in Request.php Oct 2, 2024
    @github-actions github-actions bot added the size/S label Oct 2, 2024
    Copy link
    Contributor

    penify-dev bot commented Oct 2, 2024

    PR Review πŸ”

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward and primarily involve adding checks and a new property to the response object.

    πŸ§ͺΒ Relevant tests

    No

    ⚑ Possible issues

    No

    πŸ”’Β Security concerns

    No

    Copy link

    deepsource-io bot commented Oct 2, 2024

    Here's the code health analysis summary for commits 8695a1c..6505df3. View details on DeepSourceΒ β†—.

    Analysis Summary

    AnalyzerStatusSummaryLink
    DeepSource Test coverage LogoTest coverageβœ…Β SuccessView CheckΒ β†—
    DeepSource SQL LogoSQLβœ…Β SuccessView CheckΒ β†—
    DeepSource Secrets LogoSecretsβœ…Β SuccessView CheckΒ β†—
    DeepSource PHP LogoPHPβœ…Β SuccessView CheckΒ β†—
    DeepSource Docker LogoDockerβœ…Β SuccessView CheckΒ β†—

    Code Coverage Report

    MetricAggregatePhp
    Branch Coverage100%100%
    Composite Coverage95.3%95.3%
    Line Coverage95.3%95.3%

    πŸ’‘ If you’re a repository administrator, you can configure the quality gates from the settings.

    @guibranco guibranco changed the title Enhance Request Handling in Request.php Enhance Request Handling in Request.php +semver: minor Oct 2, 2024
    Copy link

    codacy-production bot commented Oct 2, 2024

    Coverage summary from Codacy

    See diff coverage on Codacy

    Coverage variation Diff coverage
    βœ… +0.05% (target: -1.00%) βœ… 100.00%
    Coverage variation details
    Coverable lines Covered lines Coverage
    Common ancestor commit (8695a1c) 255 243 95.29%
    Head commit (6505df3) 258 (+3) 246 (+3) 95.35% (+0.05%)

    Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

    Diff coverage details
    Coverable lines Covered lines Diff coverage
    Pull request (#227) 7 7 100.00%

    Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

    See your quality gate settingsΒ Β Β Β Change summary preferences

    Codacy stopped sending the deprecated coverage status on June 5th, 2024. Learn more

    Copy link
    Contributor

    penify-dev bot commented Oct 2, 2024

    PR Code Suggestions ✨

    CategorySuggestionΒ  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Β  Score
    Possible issue
    Add a check to ensure CURLOPT_HTTPHEADER is set before assignment

    Ensure that CURLOPT_HTTPHEADER is properly set and not null before assigning it to
    requestHeaders to avoid potential errors.

    src/Request.php [50]

    -$result->requestHeaders = $fields[CURLOPT_HTTPHEADER];
    +$result->requestHeaders = isset($fields[CURLOPT_HTTPHEADER]) ? $fields[CURLOPT_HTTPHEADER] : [];
     
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential issue where CURLOPT_HTTPHEADER could be null, which may lead to unexpected behavior. The proposed change ensures that an empty array is assigned if it is not set, improving robustness.

    8
    Add a check to ensure CURLOPT_POSTFIELDS is set only when $data is not empty

    Ensure that the CURLOPT_POSTFIELDS is only set when $data is not empty, to avoid sending
    empty data in the request.

    src/Request.php [122]

    -$fields[CURLOPT_POSTFIELDS] = $data;
    +if (!empty($data)) { $fields[CURLOPT_POSTFIELDS] = $data; }
     
    Suggestion importance[1-10]: 7

    Why: This suggestion improves the handling of $data by preventing empty payloads from being sent, which can be important for API requests. However, it is not a major bug fix, hence a moderate score.

    7
    Maintainability
    Refactor to consolidate repeated logic for setting CURLOPT_POSTFIELDS

    Consider consolidating the repeated logic for setting CURLOPT_POSTFIELDS across the post,
    put, and patch methods to reduce code duplication.

    src/Request.php [101-133]

    -if ($data !== null) {
    -    $fields[CURLOPT_POSTFIELDS] = $data;
    -}
    +if (!is_null($data)) { $fields[CURLOPT_POSTFIELDS] = $data; }
     
    Suggestion importance[1-10]: 6

    Why: This suggestion promotes code maintainability by reducing duplication, but it does not address a critical issue. The suggestion is valid but not urgent, leading to a moderate score.

    6
    Best practice
    Use a more explicit null check for better clarity and safety

    Consider using a more explicit check for null values to avoid potential issues with falsy
    values in the if statements.

    src/Request.php [101]

    -if ($data !== null) {
    +if (!is_null($data)) {
     
    Suggestion importance[1-10]: 5

    Why: While using is_null() can improve clarity, the current check for !== null is already sufficient in this context. This suggestion is more about style than a critical issue.

    5

    $fields[CURLOPT_POSTFIELDS] = $data;
    if ($data !== null) {
    $fields[CURLOPT_POSTFIELDS] = $data;
    }

    Check warning

    Code scanning / Phpcs (reported by Codacy)

    No blank line found after control structure Warning

    No blank line found after control structure
    $fields[CURLOPT_POSTFIELDS] = $data;
    if ($data !== null) {
    $fields[CURLOPT_POSTFIELDS] = $data;
    }

    Check warning

    Code scanning / Phpcs (reported by Codacy)

    No blank line found after control structure Warning

    No blank line found after control structure
    $fields[CURLOPT_POSTFIELDS] = $data;
    if ($data !== null) {
    $fields[CURLOPT_POSTFIELDS] = $data;
    }

    Check warning

    Code scanning / Phpcs (reported by Codacy)

    No blank line found after control structure Warning

    No blank line found after control structure
    @guibranco guibranco enabled auto-merge (squash) October 2, 2024 18:07
    @gstraccini gstraccini bot added the β˜‘οΈ auto-merge Auto-merge enabled by gstraccini-bot label Oct 2, 2024
    Copy link

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 0

    🧹 Outside diff range and nitpick comments (3)
    src/Request.php (3)

    101-103: LGTM: Improved handling of null data in POST requests

    The conditional check for null data before setting CURLOPT_POSTFIELDS is a good practice. It prevents unnecessary assignments and potential issues with null data.

    Consider adding a blank line after the if block to improve readability:

     if ($data !== null) {
         $fields[CURLOPT_POSTFIELDS] = $data;
     }
    +
     return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 103-103:
    No blank line found after control structure


    111-113: LGTM: Improved handling of null data in PUT requests

    The conditional check for null data before setting CURLOPT_POSTFIELDS is consistent with the change in the post method and is a good practice.

    Consider adding a blank line after the if block to improve readability:

     if ($data !== null) {
         $fields[CURLOPT_POSTFIELDS] = $data;
     }
    +
     return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 113-113:
    No blank line found after control structure


    131-133: LGTM: Improved handling of null data in PATCH requests

    The conditional check for null data before setting CURLOPT_POSTFIELDS is consistent with the changes in other methods and is a good practice.

    Consider adding a blank line after the if block to improve readability:

     if ($data !== null) {
         $fields[CURLOPT_POSTFIELDS] = $data;
     }
    +
     return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 133-133:
    No blank line found after control structure

    πŸ“œ Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    πŸ“₯ Commits

    Files that changed from the base of the PR and between 8695a1c and 7eb6938.

    πŸ“’ Files selected for processing (1)
    • src/Request.php (3 hunks)
    🧰 Additional context used
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)
    src/Request.php

    [warning] 103-103:
    No blank line found after control structure


    [warning] 113-113:
    No blank line found after control structure


    [warning] 133-133:
    No blank line found after control structure

    πŸ”‡ Additional comments (3)
    src/Request.php (3)

    50-50: LGTM: Improved response object with request headers

    Adding the request headers to the result object is a good practice. It enhances debugging capabilities and provides more context for the request-response cycle.


    121-123: LGTM: Improved handling of optional data in DELETE requests

    The conditional check for null data before setting CURLOPT_POSTFIELDS is consistent with the changes in other methods. This change allows for sending data with DELETE requests when necessary, while maintaining backwards compatibility.


    Line range hint 1-153: Overall assessment: Approved with minor formatting suggestions

    The changes in this pull request improve the Request class by:

    1. Adding request headers to the result object, enhancing debugging capabilities.
    2. Implementing consistent null checks for request data across all HTTP methods (POST, PUT, DELETE, PATCH).

    These improvements align with best practices for HTTP request handling and do not introduce breaking changes. The code is more robust and maintainable as a result of these changes.

    To further improve code consistency, consider adding blank lines after the if blocks in the POST, PUT, and PATCH methods as suggested in the individual comments.

    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 103-103:
    No blank line found after control structure


    [warning] 113-113:
    No blank line found after control structure

    $fields[CURLOPT_POSTFIELDS] = $data;
    if ($data !== null) {
    $fields[CURLOPT_POSTFIELDS] = $data;
    }

    Check warning

    Code scanning / Phpcs (reported by Codacy)

    No blank line found after control structure Warning

    No blank line found after control structure
    Copy link

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 0

    🧹 Outside diff range and nitpick comments (5)
    docs/changelog.md (1)

    3-5: Approve changelog entry with a suggestion for improvement

    The new changelog entry for version 0.11 is well-formatted and clearly describes the enhancement to the Request class. However, there's an opportunity for improvement:

    1. The PR description mentions that this change introduces a breaking change, but this is not reflected in the changelog entry.

    Consider modifying the changelog entry to explicitly mention the breaking change. For example:

    - Enhance the Request class with optional data parameter by [@guibranco](https://github.com/guibranco).
    + Enhance the Request class with optional data parameter (breaking change) by [@guibranco](https://github.com/guibranco).

    This will help users understand the potential impact of upgrading to this version.

    src/Request.php (4)

    102-102: Add a blank line after the control structure.

    To improve code readability and adhere to common PHP coding standards, add a blank line after the closing brace of the if statement.

    Here's the suggested change:

      if ($data !== null) {
          $fields[CURLOPT_POSTFIELDS] = $data;
      }
    + 
      return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 102-102:
    No blank line found after control structure


    112-112: Add a blank line after the control structure.

    To improve code readability and adhere to common PHP coding standards, add a blank line after the closing brace of the if statement.

    Here's the suggested change:

      if ($data !== null) {
          $fields[CURLOPT_POSTFIELDS] = $data;
      }
    + 
      return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 112-112:
    No blank line found after control structure


    132-132: Add a blank line after the control structure.

    To improve code readability and adhere to common PHP coding standards, add a blank line after the closing brace of the if statement.

    Here's the suggested change:

      if ($data !== null) {
          $fields[CURLOPT_POSTFIELDS] = $data;
      }
    + 
      return $this->execute($fields);
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 132-132:
    No blank line found after control structure


    Line range hint 100-132: Consistent improvements with potential breaking changes

    The changes made to the post, put, delete, and patch methods are consistent and improve the robustness of the code by only setting CURLOPT_POSTFIELDS when $data is not null. This is a good practice that prevents potential issues with null data.

    However, it's important to note that these changes could be considered breaking changes if any existing code relied on CURLOPT_POSTFIELDS always being set, even with null data. The PR description does mention a breaking change, which aligns with these modifications.

    To ensure backward compatibility:

    1. Update the documentation to clearly state this change in behavior.
    2. Consider adding a major version bump to the library version number.
    3. If possible, add a deprecation notice in the previous version to warn users about the upcoming change.
    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 102-102:
    No blank line found after control structure


    [warning] 112-112:
    No blank line found after control structure

    πŸ“œ Review details

    Configuration used: CodeRabbit UI
    Review profile: CHILL

    πŸ“₯ Commits

    Files that changed from the base of the PR and between 7eb6938 and 0461a6f.

    πŸ“’ Files selected for processing (2)
    • docs/changelog.md (1 hunks)
    • src/Request.php (2 hunks)
    🧰 Additional context used
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)
    src/Request.php

    [warning] 102-102:
    No blank line found after control structure


    [warning] 112-112:
    No blank line found after control structure


    [warning] 132-132:
    No blank line found after control structure

    πŸ”‡ Additional comments (4)
    src/Request.php (4)

    110-112: Approve the conditional check for $data.

    The addition of the conditional check if ($data !== null) before setting CURLOPT_POSTFIELDS is a good improvement, consistent with the change in the post method. It ensures that the PUT fields are only set when data is actually provided.

    As with the post method, note that this change might be considered a breaking change if any existing code relied on CURLOPT_POSTFIELDS always being set, even with null data.

    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 112-112:
    No blank line found after control structure


    120-122: Approve the conditional check for $data.

    The addition of the conditional check if ($data !== null) before setting CURLOPT_POSTFIELDS is a good improvement, consistent with the changes in the post and put methods. It ensures that the DELETE fields are only set when data is actually provided.

    As with the other methods, note that this change might be considered a breaking change if any existing code relied on CURLOPT_POSTFIELDS always being set, even with null data.

    The indentation and spacing in this method are correct, which is good for code readability.


    130-132: Approve the conditional check for $data.

    The addition of the conditional check if ($data !== null) before setting CURLOPT_POSTFIELDS is a good improvement, consistent with the changes in the other methods. It ensures that the PATCH fields are only set when data is actually provided.

    As with the other methods, note that this change might be considered a breaking change if any existing code relied on CURLOPT_POSTFIELDS always being set, even with null data.

    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 132-132:
    No blank line found after control structure


    100-102: Approve the conditional check for $data.

    The addition of the conditional check if ($data !== null) before setting CURLOPT_POSTFIELDS is a good improvement. It ensures that the POST fields are only set when data is actually provided, which is more efficient and prevents potential issues with null data.

    However, please note that this change might be considered a breaking change if any existing code relied on CURLOPT_POSTFIELDS always being set, even with null data.

    To ensure this change doesn't negatively impact the codebase, please run the following verification script:

    βœ… Verification successful

    CURLOPT_POSTFIELDS Usage Verified Successfully.

    The search for CURLOPT_POSTFIELDS returned only instances within src/Request.php. This confirms that the added conditional check does not impact other parts of the codebase.

    🏁 Scripts executed

    The following scripts were executed for the analysis:

    Script:

    #!/bin/bash
    # Description: Check for any direct usage of CURLOPT_POSTFIELDS in the codebase
    # that might be affected by this change.
    
    rg --type php 'CURLOPT_POSTFIELDS'
    

    Length of output: 294

    🧰 Tools
    πŸͺ› GitHub Check: Phpcs (reported by Codacy)

    [warning] 102-102:
    No blank line found after control structure

    @github-actions github-actions bot added size/L and removed size/S labels Oct 2, 2024
    Copy link
    Contributor

    github-actions bot commented Oct 2, 2024

    Infisical secrets check: βœ… No secrets leaked!

    πŸ’» Scan logs
    6:17PM INF scanning for exposed secrets...
    6:17PM INF 160 commits scanned.
    6:17PM INF scan completed in 423ms
    6:17PM INF no leaks found
    

    Copy link

    sonarcloud bot commented Oct 2, 2024

    @guibranco guibranco merged commit 1c39b31 into main Oct 2, 2024
    28 checks passed
    @guibranco guibranco deleted the guibranco-patch-1 branch October 2, 2024 18:18
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    β˜‘οΈ auto-merge Auto-merge enabled by gstraccini-bot enhancement New feature or request Review effort [1-5]: 2 size/L
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant