Skip to content

Conversation

maglnet
Copy link

@maglnet maglnet commented Sep 1, 2025

What does this PR do?

This changes the way how API warnings are handled within the php client.
Originally the warning was directly sent to stdout and so could cause unwanted output within apps or websites, that may break json / html output etc.
This now uses the trigger_error method, so warnings will be sent to the target that is configured within the php installation.

Test Plan

I made a call to on of the current database endoints that contain a "deprecated" warnings message within the x-appwrite-waring header.

Related PRs and Issues

appwrite/sdk-for-php#44

Have you read the Contributing Guidelines on issues?

yes

Summary by CodeRabbit

  • Bug Fixes
    • Warnings are no longer printed to standard output. They are now emitted via PHP’s error handling system, ensuring cleaner responses and proper routing to error logs/handlers.
    • No changes to public APIs or signatures.

Copy link

coderabbitai bot commented Sep 1, 2025

Walkthrough

The PHP client template at templates/php/src/Client.php.twig changes how warnings are emitted. Instead of printing warnings to stdout using echo (e.g., "Warning: ..."), the code now invokes \trigger_error($warning, E_USER_WARNING) for each warning extracted from x-<title>-warning. The iteration over warnings remains unchanged, and warnings are still processed individually (e.g., semicolon-separated values handled as before). No exported or public API signatures are modified.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
templates/php/src/Client.php.twig (2)

162-173: Header parsing lowercases values and drops duplicates

Lowercasing the entire header alters values (e.g., warning text) and multiple headers overwrite earlier ones. Preserve case for values and join duplicates.

-        curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$responseHeaders) {
-            $len = strlen($header);
-            $header = explode(':', strtolower($header), 2);
-            if (count($header) < 2) { // ignore invalid headers
-                return $len;
-            }
-            $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]);
-            return $len;
-        });
+        curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $rawHeader) use (&$responseHeaders) {
+            $len = strlen($rawHeader);
+            $parts = explode(':', $rawHeader, 2);
+            if (count($parts) < 2) { // ignore invalid headers
+                return $len;
+            }
+            $name  = strtolower(trim($parts[0]));
+            $value = trim($parts[1]);
+            if (isset($responseHeaders[$name])) {
+                $responseHeaders[$name] .= ', ' . $value;
+            } else {
+                $responseHeaders[$name] = $value;
+            }
+            return $len;
+        });

196-200: Content-Type parsing breaks when no ";" present

strpos can return false, making substr(..., 0, 0) and missing JSON decode. Parse robustly.

-        switch(substr($contentType, 0, strpos($contentType, ';'))) {
+        $mediaType = strtolower(trim(explode(';', $contentType, 2)[0] ?? ''));
+        switch ($mediaType) {
             case 'application/json':
                 $responseBody = json_decode($responseBody, true);
             break;
         }
🧹 Nitpick comments (2)
templates/php/src/Client.php.twig (2)

152-161: Avoid mutating $headers while iterating

Building cURL headers by modifying the array in-place during foreach is brittle. Use a separate array.

-        foreach ($headers as $i => $header) {
-            $headers[] = $i . ':' . $header;
-            unset($headers[$i]);
-        }
+        $curlHeaders = [];
+        foreach ($headers as $i => $header) {
+            $curlHeaders[] = $i . ':' . $header;
+        }
...
-        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders);

216-218: Guard missing Location header

Prevent an undefined index notice when Location is absent.

-            return $responseHeaders['location'];
+            return $responseHeaders['location'] ?? '';
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3ad8053 and d59921a.

📒 Files selected for processing (1)
  • templates/php/src/Client.php.twig (1 hunks)
🔇 Additional comments (1)
templates/php/src/Client.php.twig (1)

190-194: Tighten warning handling: trim, sanitize, and avoid falsey skip

Use a strict non-empty check, trim and filter out blank entries, replace any CR/LF in each warning, then call trigger_error. Confirm desired behavior if your environment converts warnings into exceptions.

-        $warnings = $responseHeaders['x-{{spec.title | caseLower}}-warning'] ?? '';
-        if ($warnings) {
-            foreach(explode(';', $warnings) as $warning) {
-                \trigger_error($warning, E_USER_WARNING);
-            }
-        }
+        $warnings = $responseHeaders['x-{{spec.title | caseLower}}-warning'] ?? '';
+        if ($warnings !== '') {
+            foreach (array_filter(array_map('trim', explode(';', $warnings)), 'strlen') as $warning) {
+                $warning = preg_replace('/[\r\n]+/', ' ', $warning);
+                \trigger_error($warning, E_USER_WARNING);
+            }
+        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants