Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts":{
"test": "phpunit",
"check": "vendor/bin/phpstan analyse --memory-limit=1G --level=max src",
"check": "vendor/bin/phpstan analyse --memory-limit=1G src",
"format": "vendor/bin/pint",
"lint": "vendor/bin/pint --test"
},
Expand Down
102 changes: 42 additions & 60 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function insert(string $collection, array $document, array $options = [])
$docObj->{$key} = $value;
}

if (!isset($docObj->_id) || $docObj->_id === '' || $docObj->_id === null) {
if (!isset($docObj->_id) || $docObj->_id === '') {
$docObj->_id = $this->createUuid();
}

Expand Down Expand Up @@ -729,7 +729,7 @@ public function insertMany(string $collection, array $documents, array $options
$docObj->{$key} = $value;
}

if (!isset($docObj->_id) || $docObj->_id === '' || $docObj->_id === null) {
if (!isset($docObj->_id) || $docObj->_id === '') {
$docObj->_id = $this->createUuid();
}

Expand Down Expand Up @@ -806,10 +806,10 @@ public function lastDocument(string $collection): array
* - arrayFilters: Array filters for updates
* @param bool $multi Whether to update multiple documents
*
* @return Client
* @return int Number of modified documents
* @throws Exception
*/
public function update(string $collection, array $where = [], array $updates = [], array $options = [], bool $multi = false): self
public function update(string $collection, array $where = [], array $updates = [], array $options = [], bool $multi = false): int
{
// Build command with session and concerns
$command = [
Expand Down Expand Up @@ -843,9 +843,7 @@ public function update(string $collection, array $where = [], array $updates = [
$otherOptions = array_diff_key($options, array_flip(['session', 'writeConcern', 'readConcern', 'upsert']));
$command = array_merge($command, $otherOptions);

$this->query($command);

return $this;
return $this->query($command);
}

/**
Expand All @@ -856,10 +854,10 @@ public function update(string $collection, array $where = [], array $updates = [
* @param array $operations Array of operations, each with 'filter' and 'update' keys
* @param array $options
*
* @return self
* @return int Number of modified documents
* @throws Exception
*/
public function upsert(string $collection, array $operations, array $options = []): self
public function upsert(string $collection, array $operations, array $options = []): int
{
$updates = [];

Expand All @@ -874,7 +872,7 @@ public function upsert(string $collection, array $operations, array $options = [
$updates[] = $updateOperation;
}

$this->query(
return $this->query(
array_merge(
[
self::COMMAND_UPDATE => $collection,
Expand All @@ -883,7 +881,6 @@ public function upsert(string $collection, array $operations, array $options = [
$options
)
);
return $this;
}


Expand Down Expand Up @@ -1542,9 +1539,7 @@ public function close(): void
$activeSessions[] = ['id' => $sessionData['id'], 'sessionId' => $sessionId];
}

if (!empty($activeSessions)) {
$this->endSessions($activeSessions);
}
$this->endSessions($activeSessions);
} catch (Exception $e) {
// Silently ignore if connection is already lost during cleanup
if (!str_contains($e->getMessage(), 'Connection to MongoDB has been lost')) {
Expand All @@ -1553,7 +1548,7 @@ public function close(): void
}
}

if (isset($this->client) && $this->client->isConnected()) {
if ($this->client->isConnected()) {
$this->client->close();
}

Expand Down Expand Up @@ -1871,11 +1866,6 @@ private function validateConnection(): void
throw new Exception('Client is not connected to MongoDB');
}

if (!isset($this->client)) {
$this->isConnected = false;
throw new Exception('MongoDB client is not initialized');
}

if (!$this->client->isConnected()) {
$this->isConnected = false;
throw new Exception('Connection to MongoDB has been lost');
Expand All @@ -1889,7 +1879,7 @@ private function validateConnection(): void
* @return array Validated write concern
* @throws Exception If write concern is invalid
*/
public function createWriteConcern($writeConcern): array
public function createWriteConcern(array|string|int $writeConcern): array
{
if (is_string($writeConcern)) {
return ['w' => $writeConcern];
Expand All @@ -1902,31 +1892,27 @@ public function createWriteConcern($writeConcern): array
return ['w' => $writeConcern];
}

if (is_array($writeConcern)) {
$concern = [];
$concern = [];

if (isset($writeConcern['w'])) {
if (is_int($writeConcern['w']) && $writeConcern['w'] < 0) {
throw new Exception('Write concern w value must be >= 0');
}
$concern['w'] = $writeConcern['w'];
if (isset($writeConcern['w'])) {
if (is_int($writeConcern['w']) && $writeConcern['w'] < 0) {
throw new Exception('Write concern w value must be >= 0');
}
$concern['w'] = $writeConcern['w'];
}

if (isset($writeConcern['j'])) {
$concern['j'] = (bool)$writeConcern['j'];
}
if (isset($writeConcern['j'])) {
$concern['j'] = (bool)$writeConcern['j'];
}

if (isset($writeConcern['wtimeout'])) {
if (!is_int($writeConcern['wtimeout']) || $writeConcern['wtimeout'] < 0) {
throw new Exception('Write concern wtimeout must be a non-negative integer');
}
$concern['wtimeout'] = $writeConcern['wtimeout'];
if (isset($writeConcern['wtimeout'])) {
if (!is_int($writeConcern['wtimeout']) || $writeConcern['wtimeout'] < 0) {
throw new Exception('Write concern wtimeout must be a non-negative integer');
}

return $concern;
$concern['wtimeout'] = $writeConcern['wtimeout'];
}

throw new Exception('Invalid write concern format');
return $concern;
}

/**
Expand Down Expand Up @@ -1974,7 +1960,7 @@ private function shouldSkipReadConcern(array $options): bool
* @return array Validated read concern
* @throws Exception If read concern is invalid
*/
public function createReadConcern($readConcern): array
public function createReadConcern(array|string $readConcern): array
{
if (is_string($readConcern)) {
$validLevels = [
Expand All @@ -1992,33 +1978,29 @@ public function createReadConcern($readConcern): array
return ['level' => $readConcern];
}

if (is_array($readConcern)) {
$concern = [];
$concern = [];

if (isset($readConcern['level'])) {
$validLevels = [
self::READ_CONCERN_LOCAL,
self::READ_CONCERN_AVAILABLE,
self::READ_CONCERN_MAJORITY,
self::READ_CONCERN_LINEARIZABLE,
self::READ_CONCERN_SNAPSHOT
];

if (!in_array($readConcern['level'], $validLevels)) {
throw new Exception('Invalid read concern level: ' . $readConcern['level']);
}
if (isset($readConcern['level'])) {
$validLevels = [
self::READ_CONCERN_LOCAL,
self::READ_CONCERN_AVAILABLE,
self::READ_CONCERN_MAJORITY,
self::READ_CONCERN_LINEARIZABLE,
self::READ_CONCERN_SNAPSHOT
];

$concern['level'] = $readConcern['level'];
if (!in_array($readConcern['level'], $validLevels)) {
throw new Exception('Invalid read concern level: ' . $readConcern['level']);
}

if (isset($readConcern['afterClusterTime'])) {
$concern['afterClusterTime'] = $readConcern['afterClusterTime'];
}
$concern['level'] = $readConcern['level'];
}

return $concern;
if (isset($readConcern['afterClusterTime'])) {
$concern['afterClusterTime'] = $readConcern['afterClusterTime'];
}

throw new Exception('Invalid read concern format');
return $concern;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ public function getErrorCategory(): string
*
* @param \stdClass $errorResponse MongoDB error response
* @param string|null $operationType Operation type
* @return static
* @return self
*/
public static function fromResponse(\stdClass $errorResponse, ?string $operationType = null): static
public static function fromResponse(\stdClass $errorResponse, ?string $operationType = null): self
{
$message = $errorResponse->errmsg ?? 'Unknown MongoDB error';
$code = $errorResponse->code ?? 0;
$errorLabels = $errorResponse->errorLabels ?? [];
$writeErrors = $errorResponse->writeErrors ?? null;
$writeConcernErrors = $errorResponse->writeConcernErrors ?? null;

return new static(
return new self(
$message,
$code,
null,
Expand Down