diff --git a/composer.json b/composer.json index 9e1c76e..eca4255 100755 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Client.php b/src/Client.php index 63007b7..c6d2c91 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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(); } @@ -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(); } @@ -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 = [ @@ -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); } /** @@ -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 = []; @@ -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, @@ -883,7 +881,6 @@ public function upsert(string $collection, array $operations, array $options = [ $options ) ); - return $this; } @@ -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')) { @@ -1553,7 +1548,7 @@ public function close(): void } } - if (isset($this->client) && $this->client->isConnected()) { + if ($this->client->isConnected()) { $this->client->close(); } @@ -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'); @@ -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]; @@ -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; } /** @@ -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 = [ @@ -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; } /** diff --git a/src/Exception.php b/src/Exception.php index 3fe28c2..7c47d6a 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -191,9 +191,9 @@ 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; @@ -201,7 +201,7 @@ public static function fromResponse(\stdClass $errorResponse, ?string $operation $writeErrors = $errorResponse->writeErrors ?? null; $writeConcernErrors = $errorResponse->writeConcernErrors ?? null; - return new static( + return new self( $message, $code, null,