Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

@kumy
Copy link
Contributor

@kumy kumy commented Nov 16, 2025

Hi @jschroed91

would you please consider this PR to bring fixes for PHP 8+ to avoid deprecation notices like:

Return type of Caxy\BaseX\Query::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used...

72785b6 is the minimal required fix, and as I was on it I also added type hints for other methods in b4b2b8f.

Thanks

Edit: Also added the fix proposed by the review
image

Summary by CodeRabbit

  • Refactor
    • Improved overall stability and reliability via comprehensive type safety and stronger input/output contracts.
    • More consistent query/session behavior, safer streaming/iteration, and clearer error reporting for connection and command failures.

@coderabbitai
Copy link

coderabbitai bot commented Nov 16, 2025

Warning

Rate limit exceeded

@kumy has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 42 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 30c5f75 and e43b6b9.

📒 Files selected for processing (2)
  • src/Query.php (3 hunks)
  • src/Session.php (12 hunks)

Walkthrough

Added strict typing across src/Query.php and src/Session.php: properties, constructors, method parameters, and return types were annotated and internal logic adjusted to match typed contracts (iterator behavior, buffering, send/receive/error handling).

Changes

Cohort / File(s) Summary
Query type and iterator updates
src/Query.php
Properties typed (Session $session, string $id, ?array $cache = null, int $pos = 0); constructor now __construct(Session $session, string $query); methods updated with parameter and return types (e.g., bind(...) : void, execute() : string, more() : bool, next() : void, current() : mixed, valid() : bool, rewind() : void); more() and next() adjusted for typed buffering/iteration and error handling.
Session typing and I/O robustness
src/Session.php
Properties typed (`Socket

Sequence Diagram(s)

sequenceDiagram
    participant Q as Query
    participant S as Session
    Note over Q,S `#E8F5E9`: Typed interactions and error checks

    Q->>S: execute(query : string)
    S-->>Q: resultId : string
    Q->>S: exec(cmd : string, arg : string)
    alt ok
        S-->>Q: response string
    else error
        S-->>Q: throws BaseXException (info ?? 'Unknown error')
    end
    Q->>Q: more() -> fetch & buffer into cache (pos:int)
    Q->>Q: next() advances pos if more() true
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Areas to focus:
    • verify declared return types (especially string, mixed, bool) match actual runtime outputs
    • check iterator semantics (more(), next(), current(), valid(), rewind()) with new typed cache/pos
    • inspect socket error handling and BaseXException messages using ?string info
    • confirm Socket|false usage and any code paths assuming a truthy socket

Poem

🐰
Types tucked neatly in my burrow wall,
Session steady, queries answer the call,
Buffers lined up, pos hopped into place,
Errors named kindly, no mystery to chase,
I nibble bugs — now code runs calm and small.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main objective of the pull request: adding type hints to PHP iterator method signatures to comply with PHP 8+ compatibility and remove deprecation notices.

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

Comment @coderabbitai help to get the list of available commands and usage tips.

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 (1)
src/Session.php (1)

158-165: Add error handling for socket_recv() in read() method

The read() method (line 161) does not check if socket_recv() fails. Per PHP documentation, socket_recv() returns false on error, but the code assigns this directly to $this->bsize (declared as int). When the next call to read() occurs, the condition $this->bpos == $this->bsize may still evaluate, and the method would return stale buffer data instead of detecting the socket failure.

This matches the error-handling pattern already established elsewhere in the file (e.g., socket_connect() check at line 26). Add the following:

 private function read(): string
 {
     if ($this->bpos == $this->bsize) {
-        $this->bsize = socket_recv($this->socket, $this->buffer, 4096, 0);
+        $this->bsize = socket_recv($this->socket, $this->buffer, 4096, 0);
+        if ($this->bsize === false) {
+            throw new BaseXException("Socket read error: " . socket_last_error($this->socket));
+        }
         $this->bpos = 0;
     }
     return $this->buffer[$this->bpos++];
 }
🧹 Nitpick comments (2)
src/Session.php (1)

16-20: Property type declarations look good.

The type declarations are appropriate. The mixed type for $socket correctly handles the fact that socket_create() can return Socket|false. All properties are properly initialized with type-safe defaults.

Consider using Socket|false for PHP 8.1+ for more precise typing:

-    protected mixed $socket;
+    protected Socket|false $socket;

This would provide better IDE support and type safety, though mixed is perfectly acceptable for broader PHP 8.0 compatibility.

src/Query.php (1)

1-11: Consider adding strict type enforcement.

While the type hints are correctly added, consider adding declare(strict_types=1); after the opening PHP tag to enable strict type checking at runtime. This would catch type-related bugs earlier and is a best practice for newly typed code.

Apply this pattern to both files:

 <?php
+declare(strict_types=1);
+
 /*
  * PHP client for BaseX.

Note: This could be a breaking change if any client code relies on PHP's type coercion, so verify compatibility before applying.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira 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 e060fd4 and b4b2b8f.

📒 Files selected for processing (2)
  • src/Query.php (3 hunks)
  • src/Session.php (13 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/Session.php (2)
src/Query.php (5)
  • info (73-76)
  • __construct (26-30)
  • execute (42-45)
  • Query (13-116)
  • close (83-86)
src/BaseXException.php (1)
  • BaseXException (13-14)
src/Query.php (1)
src/Session.php (6)
  • Session (13-201)
  • __construct (22-49)
  • query (77-80)
  • execute (57-69)
  • info (125-128)
  • close (133-137)
🔇 Additional comments (11)
src/Session.php (6)

22-22: LGTM! Constructor parameter types are correct.

The explicit parameter types match the method's usage and improve type safety.


57-68: Excellent improvements to type safety and error handling.

The return type declaration correctly reflects the method's behavior, and the null coalescing operator on line 66 ensures a meaningful error message even when $this->info is null.


77-118: LGTM! Method signatures are well-typed.

All return type declarations are accurate and improve type safety. The void return types for command methods (create, add, replace, store) correctly indicate these methods are called for their side effects.


125-137: LGTM! Return types are correct.

The nullable return type for info() correctly reflects that the info property may be null, and void for close() is appropriate.


149-156: LGTM! Return type is correct.

The string return type accurately reflects this method's behavior.


167-200: LGTM! Method signatures are well-typed.

All type declarations are correct. The null coalescing operator on line 172 provides consistent error handling with the execute() method.

src/Query.php (5)

15-18: LGTM! Property types are correct and well-initialized.

The type declarations accurately reflect the property usage and align with the typed Session API.


26-30: LGTM! Constructor parameter types are correct.

The explicit types enforce the dependency on Session and improve type safety throughout the class.


32-45: LGTM! Method signatures are well-typed.

The parameter and return types accurately reflect each method's behavior.


47-96: LGTM! All method signatures are correctly typed.

The type declarations improve API clarity and type safety. The exec() method's string return type ensures consistent behavior across all command execution methods.


98-115: Excellent! Iterator interface compliance achieved.

These return type declarations directly address the PR's main objective by ensuring compatibility with PHP 8+'s Iterator interface requirements. The mixed return types for current() and key() match the interface specification, eliminating the deprecation warnings mentioned in the PR description.

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: 1

Caution

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

⚠️ Outside diff range comments (1)
src/Session.php (1)

22-28: Critical: Check for socket_create failure before using the socket.

If socket_create() returns false, line 26 will attempt to pass false to socket_connect(), which expects a Socket object in PHP 8+. This will cause a type error.

Apply this diff to check for socket creation failure:

     public function __construct(string $hostname, int $port, string $user, string $password)
     {
         // create server connection
         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+        if ($this->socket === false) {
+            throw new BaseXException("Failed to create socket: " . socket_strerror(socket_last_error()));
+        }
         if (!socket_connect($this->socket, $hostname, $port)) {
             throw new BaseXException("Can't communicate with server.");
         }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Jira 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 b4b2b8f and 30c5f75.

📒 Files selected for processing (2)
  • src/Query.php (3 hunks)
  • src/Session.php (12 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/Query.php (1)
src/Session.php (6)
  • Session (13-204)
  • __construct (22-49)
  • query (77-80)
  • execute (57-69)
  • info (125-128)
  • close (133-137)
src/Session.php (2)
src/Query.php (5)
  • info (73-76)
  • __construct (26-30)
  • execute (42-45)
  • Query (13-116)
  • close (83-86)
src/BaseXException.php (1)
  • BaseXException (13-14)
🔇 Additional comments (11)
src/Query.php (6)

15-18: Property type declarations look good.

The property types are appropriate and align with PHP 8+ requirements for strict typing.


26-30: Constructor signature is correct.

The typed parameters properly match the property declarations.


66-71: Non-standard Iterator pattern - verify intentional design.

The next() method conditionally increments $pos only when more() returns true. The standard Iterator contract expects next() to advance unconditionally, with valid() checking if the new position is valid. This forward-only approach may be intentional, but please confirm this behavior is correct for BaseX query result iteration.


88-96: Proper error handling and type safety.

The typed signature and exception handling align well with PHP 8+ best practices.


98-115: Iterator interface implementation is PHP 8+ compatible.

All method signatures correctly match the Iterator interface requirements, which resolves the deprecation notices. The empty rewind() implementation is acceptable for a forward-only iterator.


47-64: Verified: Double ok() call pattern correctly implements BaseX protocol.

The code properly handles the BaseX wire protocol. The ok() method (Session.php:190-192) reads one byte from the socket, and its @internal documentation confirms it is "not idempotent." The pattern in more() is correct: the first ok() call in the while loop checks for the end-of-results marker (null byte), and the second ok() call reads the status byte. Each invocation consumes one byte as intended.

src/Session.php (5)

17-20: Property types are correctly declared.

These property type declarations are appropriate and properly initialized.


57-69: Good error handling with defensive null check.

The typed signature and use of the null coalescing operator (line 66) provide good type safety and prevent potential null reference issues.


77-137: Public method signatures are properly typed.

All method signatures correctly use parameter and return types, which provides better type safety and IDE support.


158-168: Excellent defensive error handling.

The added check for socket_recv failure (lines 162-164) with detailed error information is a solid improvement for debugging socket communication issues.


170-203: Internal methods are properly typed.

All private and public method signatures include appropriate type hints and return types, completing the PHP 8+ compatibility improvements.

src/Session.php Outdated Show resolved Hide resolved
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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.