-
Notifications
You must be signed in to change notification settings - Fork 1
Fix return value for update #30
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
Conversation
WalkthroughUpdates adjust Composer’s phpstan script, change Client methods’ return types and parameter unions, and modify Exception::fromResponse to return self instead of static, including corresponding PHPDoc updates. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant Client
participant DB as Database
participant Ex as Exception
Caller->>Client: update()/upsert()(args)
Client->>DB: execute write
alt success
DB-->>Client: affectedCount (int)
Client-->>Caller: int
else error
Client->>Ex: Exception::fromResponse(error, opType)
Ex-->>Client: Exception (self)
Client-->>Caller: throw Exception
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
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. Comment |
There was a problem hiding this 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/Client.php (1)
809-846: Cast update() result to int
parseResponse()can return astdClasswhen the response lacks annproperty (e.g. onlynModified), violating: int. On line 846, replace the direct return with something like:$result = $this->query($command); return is_int($result) ? $result : (int) ($result->n ?? $result->nModified ?? 0);
🧹 Nitpick comments (3)
src/Client.php (3)
658-660: Empty string check for_idmay be overly restrictive.The condition
!isset($docObj->_id) || $docObj->_id === ''rejects documents where_idis explicitly set to an empty string. While empty-string IDs are unusual, MongoDB technically allows them. If the intent is to generate UUIDs only when_idis absent or null, consider usingempty($docObj->_id)or explicitly checking fornull.Apply this diff if you want to allow empty strings as valid IDs:
-if (!isset($docObj->_id) || $docObj->_id === '') { +if (!isset($docObj->_id) || $docObj->_id === null) { $docObj->_id = $this->createUuid(); }
732-734: Empty string check for_idmay be overly restrictive.Same concern as in the
insert()method: the condition!isset($docObj->_id) || $docObj->_id === ''rejects documents where_idis explicitly set to an empty string. MongoDB technically allows empty-string IDs. If the intent is to generate UUIDs only when_idis absent or null, consider usingempty($docObj->_id)or explicitly checking fornull.Apply this diff if you want to allow empty strings as valid IDs:
-if (!isset($docObj->_id) || $docObj->_id === '') { +if (!isset($docObj->_id) || $docObj->_id === null) { $docObj->_id = $this->createUuid(); }
1542-1542: Potential exception when ending sessions during cleanup.At line 1542,
endSessions($activeSessions)is called duringclose(). If the connection is already lost, this call may throw an exception that is caught and logged (lines 1543-1548). However, the exception handling is wrapped in a check for the error message 'Connection to MongoDB has been lost', which may not cover all failure scenarios.Consider broadening the exception handling to catch all exceptions during session cleanup, or validate the connection state before calling
endSessions().
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
composer.json(1 hunks)src/Client.php(12 hunks)src/Exception.php(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/Client.php (1)
src/Exception.php (1)
Exception(10-214)
🔇 Additional comments (6)
src/Client.php (4)
1551-1553: CallingisConnected()on the client after potential reconnection failure.At line 1551,
$this->client->isConnected()is checked before closing. If the connection was lost and not successfully re-established, this check may fail silently. However, theclose()method is designed to clean up regardless, so this is likely acceptable. Just ensure that callingclose()on a disconnected client does not throw.
1882-1916: Parameter union type forcreateWriteConcernis broad; validate input thoroughly.The method signature accepts
array|string|intfor$writeConcern. The implementation handles each type separately, but there's no validation for unexpected array keys or invalid combinations (e.g., both string and array fields).The current implementation is adequate for typical use cases, but consider adding validation or documentation to clarify which array keys are supported and which combinations are valid.
If you want to add stricter validation, consider checking for unexpected keys:
+$allowedKeys = ['w', 'j', 'wtimeout']; +$unexpectedKeys = array_diff(array_keys($writeConcern), $allowedKeys); +if (!empty($unexpectedKeys)) { + throw new Exception('Unexpected write concern keys: ' . implode(', ', $unexpectedKeys)); +}
1963-2004: Parameter union type forcreateReadConcernis appropriate; validate input thoroughly.The method signature accepts
array|stringfor$readConcern. The implementation handles each type separately and validates thelevelagainst a whitelist. The array case also supportsafterClusterTime, which is correct for causal consistency.The implementation is sound.
857-883: Cast upsert return to int.query()returnsstdClass|array|intand may return a non‐int type; wrap thequery()call on line 875 in an explicit(int)cast to guarantee an integer result.composer.json (1)
26-26: Clarify reason for removing--level=maxfrom PHPStan.The
--level=maxflag was removed from the PHPStan check script. This reduces the strictness of static analysis. Was this intentional to address specific issues, or is the plan to gradually increase the level later?If the removal is temporary, consider documenting the target level in a comment or tracking the upgrade in an issue.
src/Exception.php (1)
194-213: Return type changed fromstatictoselfin factory method.The
fromResponse()factory method now returnsselfinstead ofstatic. This prevents subclasses (e.g.,ConnectionException,AuthenticationException) from being instantiated via this factory.Since none of the subclasses (lines 219-259) override
fromResponse()or add new constructor parameters, the impact is minimal. However, if future subclasses need to be instantiated viafromResponse(), they will need their own factory methods.This change is acceptable and improves type safety by making the return type concrete.
Summary by CodeRabbit
Breaking Changes
Improvements
Chores