PHPLIB-330: Apply automatic fixes via phpcbf

parent c5a45f1c
......@@ -30,8 +30,6 @@ class BulkWriteResult
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
......
......@@ -17,13 +17,15 @@
namespace MongoDB;
use Iterator;
use MongoDB\Driver\CursorId;
use MongoDB\Driver\Exception\ConnectionException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Exception\ResumeTokenException;
use MongoDB\Model\ChangeStreamIterator;
use Iterator;
use function call_user_func;
use function in_array;
/**
* Iterator for a change stream.
......@@ -57,8 +59,6 @@ class ChangeStream implements Iterator
private $hasAdvanced = false;
/**
* Constructor.
*
* @internal
* @param ChangeStreamIterator $iterator
* @param callable $resumeCallable
......@@ -109,6 +109,7 @@ class ChangeStream implements Iterator
if ($this->valid()) {
return $this->key;
}
return null;
}
......@@ -167,7 +168,7 @@ class ChangeStream implements Iterator
return true;
}
if ( ! $exception instanceof ServerException) {
if (! $exception instanceof ServerException) {
return false;
}
......@@ -202,7 +203,7 @@ class ChangeStream implements Iterator
/* Return early if there is not a current result. Avoid any attempt to
* increment the iterator's key. */
if (!$this->valid()) {
if (! $this->valid()) {
return;
}
......@@ -236,6 +237,7 @@ class ChangeStream implements Iterator
{
if ($this->isResumableError($exception)) {
$this->resume();
return;
}
......
......@@ -17,27 +17,30 @@
namespace MongoDB;
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListDatabases;
use MongoDB\Operation\Watch;
use function is_array;
class Client
{
private static $defaultTypeMap = [
'array' => \MongoDB\Model\BSONArray::class,
'document' => \MongoDB\Model\BSONDocument::class,
'root' => \MongoDB\Model\BSONDocument::class,
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];
private static $wireVersionForReadConcern = 4;
private static $wireVersionForWritableCommandWriteConcern = 5;
......@@ -147,13 +150,13 @@ class Client
*/
public function dropDatabase($databaseName, array $options = [])
{
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -269,7 +272,7 @@ class Client
* Start a new client session.
*
* @see http://php.net/manual/en/mongodb-driver-manager.startsession.php
* @param array $options Session options
* @param array $options Session options
* @return Session
*/
public function startSession(array $options = [])
......@@ -288,17 +291,17 @@ class Client
*/
public function watch(array $pipeline = [], array $options = [])
{
if ( ! isset($options['readPreference'])) {
if (! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
$server = $this->manager->selectServer($options['readPreference']);
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern)) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
......
This diff is collapsed.
......@@ -17,17 +17,18 @@
namespace MongoDB;
use MongoDB\Collection;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\GridFS\Bucket;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\CollectionInfoIterator;
use MongoDB\Operation\Aggregate;
use MongoDB\Operation\CreateCollection;
......@@ -38,13 +39,15 @@ use MongoDB\Operation\ListCollections;
use MongoDB\Operation\ModifyCollection;
use MongoDB\Operation\Watch;
use Traversable;
use function is_array;
use function strlen;
class Database
{
private static $defaultTypeMap = [
'array' => \MongoDB\Model\BSONArray::class,
'document' => \MongoDB\Model\BSONDocument::class,
'root' => \MongoDB\Model\BSONDocument::class,
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];
private static $wireVersionForReadConcern = 4;
private static $wireVersionForWritableCommandWriteConcern = 5;
......@@ -175,9 +178,9 @@ class Database
*/
public function aggregate(array $pipeline, array $options = [])
{
$hasWriteStage = \MongoDB\is_last_pipeline_operator_write($pipeline);
$hasWriteStage = is_last_pipeline_operator_write($pipeline);
if ( ! isset($options['readPreference'])) {
if (! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
......@@ -192,22 +195,22 @@ class Database
*
* A read concern is also not compatible with transactions.
*/
if ( ! isset($options['readConcern']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern) &&
! \MongoDB\is_in_transaction($options) &&
( ! $hasWriteStage || \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
if (! isset($options['readConcern']) &&
server_supports_feature($server, self::$wireVersionForReadConcern) &&
! is_in_transaction($options) &&
( ! $hasWriteStage || server_supports_feature($server, self::$wireVersionForReadConcernWithWriteStage))
) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
if ($hasWriteStage &&
! isset($options['writeConcern']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) &&
! \MongoDB\is_in_transaction($options)) {
server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) &&
! is_in_transaction($options)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -228,11 +231,11 @@ class Database
*/
public function command($command, array $options = [])
{
if ( ! isset($options['readPreference'])) {
if (! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
......@@ -255,13 +258,13 @@ class Database
*/
public function createCollection($collectionName, array $options = [])
{
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -282,13 +285,13 @@ class Database
*/
public function drop(array $options = [])
{
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -310,13 +313,13 @@ class Database
*/
public function dropCollection($collectionName, array $options = [])
{
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -417,13 +420,13 @@ class Database
*/
public function modifyCollection($collectionName, array $collectionOptions, array $options = [])
{
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
if ( ! isset($options['writeConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern)) {
$options['writeConcern'] = $this->writeConcern;
}
......@@ -484,17 +487,17 @@ class Database
*/
public function watch(array $pipeline = [], array $options = [])
{
if ( ! isset($options['readPreference'])) {
if (! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
$server = $this->manager->selectServer($options['readPreference']);
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern)) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
......
......@@ -29,8 +29,6 @@ class DeleteResult
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public function __construct(WriteResult $writeResult)
......
......@@ -18,6 +18,7 @@
namespace MongoDB\Exception;
use BadMethodCallException as BaseBadMethodCallException;
use function sprintf;
class BadMethodCallException extends BaseBadMethodCallException implements Exception
{
......
......@@ -18,6 +18,10 @@
namespace MongoDB\Exception;
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
use function get_class;
use function gettype;
use function is_object;
use function sprintf;
class InvalidArgumentException extends DriverInvalidArgumentException implements Exception
{
......
......@@ -17,6 +17,9 @@
namespace MongoDB\Exception;
use function gettype;
use function sprintf;
class ResumeTokenException extends RuntimeException
{
/**
......
......@@ -19,17 +19,40 @@ namespace MongoDB\GridFS;
use MongoDB\Collection;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\GridFS\Exception\CorruptFileException;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\Find;
use stdClass;
use function array_intersect_key;
use function fopen;
use function get_resource_type;
use function in_array;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_resource;
use function is_string;
use function method_exists;
use function MongoDB\apply_type_map_to_document;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toJSON;
use function property_exists;
use function sprintf;
use function stream_context_create;
use function stream_copy_to_stream;
use function stream_get_meta_data;
use function stream_get_wrappers;
use function urlencode;
/**
* Bucket provides a public API for interacting with the GridFS files and chunks
......@@ -42,9 +65,9 @@ class Bucket
private static $defaultBucketName = 'fs';
private static $defaultChunkSizeBytes = 261120;
private static $defaultTypeMap = [
'array' => \MongoDB\Model\BSONArray::class,
'document' => \MongoDB\Model\BSONDocument::class,
'root' => \MongoDB\Model\BSONDocument::class,
'array' => BSONArray::class,
'document' => BSONDocument::class,
'root' => BSONDocument::class,
];
private static $streamWrapperProtocol = 'gridfs';
......@@ -193,7 +216,7 @@ class Bucket
*/
public function downloadToStream($id, $destination)
{
if ( ! is_resource($destination) || get_resource_type($destination) != "stream") {
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
}
......@@ -228,7 +251,7 @@ class Bucket
*/
public function downloadToStreamByName($filename, $destination, array $options = [])
{
if ( ! is_resource($destination) || get_resource_type($destination) != "stream") {
if (! is_resource($destination) || get_resource_type($destination) != "stream") {
throw InvalidArgumentException::invalidType('$destination', $destination, 'resource');
}
......@@ -333,7 +356,7 @@ class Bucket
$file = $this->getRawFileDocumentForStream($stream);
// Filter the raw document through the specified type map
return \MongoDB\apply_type_map_to_document($file, $this->typeMap);
return apply_type_map_to_document($file, $this->typeMap);
}
/**
......@@ -353,9 +376,9 @@ class Bucket
* the root type so we can reliably access the ID.
*/
$typeMap = ['root' => 'stdClass'] + $this->typeMap;
$file = \MongoDB\apply_type_map_to_document($file, $typeMap);
$file = apply_type_map_to_document($file, $typeMap);
if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
if (! isset($file->_id) && ! property_exists($file, '_id')) {
throw new CorruptFileException('file._id does not exist');
}
......@@ -532,7 +555,7 @@ class Bucket
? $updateResult->getMatchedCount() === 1
: $this->collectionWrapper->findFileById($id) !== null;
if ( ! $found) {
if (! $found) {
throw FileNotFoundException::byId($id, $this->getFilesNamespace());
}
}
......@@ -562,7 +585,7 @@ class Bucket
*/
public function uploadFromStream($filename, $source, array $options = [])
{
if ( ! is_resource($source) || get_resource_type($source) != "stream") {
if (! is_resource($source) || get_resource_type($source) != "stream") {
throw InvalidArgumentException::invalidType('$source', $source, 'resource');
}
......@@ -580,10 +603,10 @@ class Bucket
*/
private function createPathForFile(stdClass $file)
{
if ( ! is_object($file->_id) || method_exists($file->_id, '__toString')) {
if (! is_object($file->_id) || method_exists($file->_id, '__toString')) {
$id = (string) $file->_id;
} else {
$id = \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP(['_id' => $file->_id]));
$id = toJSON(fromPHP(['_id' => $file->_id]));
}
return sprintf(
......@@ -632,13 +655,13 @@ class Bucket
*/
private function getRawFileDocumentForStream($stream)
{
if ( ! is_resource($stream) || get_resource_type($stream) != "stream") {
if (! is_resource($stream) || get_resource_type($stream) != "stream") {
throw InvalidArgumentException::invalidType('$stream', $stream, 'resource');
}
$metadata = stream_get_meta_data($stream);
if ( ! isset ($metadata['wrapper_data']) || ! $metadata['wrapper_data'] instanceof StreamWrapper) {
if (! isset($metadata['wrapper_data']) || ! $metadata['wrapper_data'] instanceof StreamWrapper) {
throw InvalidArgumentException::invalidType('$stream wrapper data', isset($metadata['wrapper_data']) ? $metadata['wrapper_data'] : null, StreamWrapper::class);
}
......
......@@ -18,12 +18,14 @@
namespace MongoDB\GridFS;
use MongoDB\Collection;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\UpdateResult;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\UpdateResult;
use stdClass;
use function abs;
use function sprintf;
/**
* CollectionWrapper abstracts the GridFS files and chunks collections.
......@@ -122,7 +124,7 @@ class CollectionWrapper
*
* @see Bucket::downloadToStreamByName()
* @see Bucket::openDownloadStreamByName()
* @param string $filename
* @param string $filename
* @param integer $revision
* @return stdClass|null
*/
......@@ -235,7 +237,7 @@ class CollectionWrapper
*/
public function insertChunk($chunk)
{
if ( ! $this->checkedIndexes) {
if (! $this->checkedIndexes) {
$this->ensureIndexes();
}
......@@ -251,7 +253,7 @@ class CollectionWrapper
*/
public function insertFile($file)
{
if ( ! $this->checkedIndexes) {
if (! $this->checkedIndexes) {
$this->ensureIndexes();
}
......@@ -262,7 +264,7 @@ class CollectionWrapper
* Updates the filename field in the file document for a given ID.
*
* @param mixed $id
* @param string $filename
* @param string $filename
* @return UpdateResult
*/
public function updateFilenameForId($id, $filename)
......@@ -315,7 +317,7 @@ class CollectionWrapper
$this->checkedIndexes = true;
if ( ! $this->isFilesCollectionEmpty()) {
if (! $this->isFilesCollectionEmpty()) {
return;
}
......
......@@ -18,6 +18,7 @@
namespace MongoDB\GridFS\Exception;
use MongoDB\Exception\RuntimeException;
use function sprintf;
class CorruptFileException extends RuntimeException
{
......
......@@ -18,6 +18,9 @@
namespace MongoDB\GridFS\Exception;
use MongoDB\Exception\RuntimeException;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toJSON;
use function sprintf;
class FileNotFoundException extends RuntimeException
{
......@@ -43,7 +46,7 @@ class FileNotFoundException extends RuntimeException
*/
public static function byId($id, $namespace)
{
$json = \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP(['_id' => $id]));
$json = toJSON(fromPHP(['_id' => $id]));
return new static(sprintf('File "%s" not found in "%s"', $json, $namespace));
}
......
......@@ -17,10 +17,17 @@
namespace MongoDB\GridFS;
use IteratorIterator;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\Exception\CorruptFileException;
use IteratorIterator;
use stdClass;
use function ceil;
use function floor;
use function is_integer;
use function property_exists;
use function sprintf;
use function strlen;
use function substr;
/**
* ReadableStream abstracts the process of reading a GridFS file.
......@@ -49,15 +56,15 @@ class ReadableStream
*/
public function __construct(CollectionWrapper $collectionWrapper, stdClass $file)
{
if ( ! isset($file->chunkSize) || ! is_integer($file->chunkSize) || $file->chunkSize < 1) {
if (! isset($file->chunkSize) || ! is_integer($file->chunkSize) || $file->chunkSize < 1) {
throw new CorruptFileException('file.chunkSize is not an integer >= 1');
}
if ( ! isset($file->length) || ! is_integer($file->length) || $file->length < 0) {
if (! isset($file->length) || ! is_integer($file->length) || $file->length < 0) {
throw new CorruptFileException('file.length is not an integer > 0');
}
if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
if (! isset($file->_id) && ! property_exists($file, '_id')) {
throw new CorruptFileException('file._id does not exist');
}
......@@ -202,6 +209,7 @@ class ReadableStream
*/
if ($lastChunkOffset > $this->chunkOffset) {
$this->chunksIterator = null;
return;
}
......@@ -239,7 +247,7 @@ class ReadableStream
return false;
}
if ( ! $this->chunksIterator->valid()) {
if (! $this->chunksIterator->valid()) {
throw CorruptFileException::missingChunk($this->chunkOffset);
}
......@@ -253,7 +261,7 @@ class ReadableStream
$actualChunkSize = strlen($this->buffer);
$expectedChunkSize = ($this->chunkOffset === $this->numChunks - 1)
$expectedChunkSize = $this->chunkOffset === $this->numChunks - 1
? $this->expectedLastChunkSize
: $this->chunkSize;
......
......@@ -17,9 +17,24 @@
namespace MongoDB\GridFS;
use MongoDB\BSON\UTCDateTime;
use Exception;
use MongoDB\BSON\UTCDateTime;
use stdClass;
use function explode;
use function get_class;
use function in_array;
use function is_integer;
use function sprintf;
use function stream_context_get_options;
use function stream_get_wrappers;
use function stream_wrapper_register;
use function stream_wrapper_unregister;
use function trigger_error;
use const E_USER_WARNING;
use const SEEK_CUR;
use const SEEK_END;
use const SEEK_SET;
use const STREAM_IS_URL;
/**
* Stream wrapper for reading and writing a GridFS file.
......@@ -60,7 +75,7 @@ class StreamWrapper
stream_wrapper_unregister($protocol);
}
stream_wrapper_register($protocol, get_called_class(), \STREAM_IS_URL);
stream_wrapper_register($protocol, static::class, STREAM_IS_URL);
}
/**
......@@ -81,7 +96,7 @@ class StreamWrapper
*/
public function stream_eof()
{
if ( ! $this->stream instanceof ReadableStream) {
if (! $this->stream instanceof ReadableStream) {
return false;
}
......@@ -126,14 +141,15 @@ class StreamWrapper
*/
public function stream_read($length)
{
if ( ! $this->stream instanceof ReadableStream) {
if (! $this->stream instanceof ReadableStream) {
return '';
}
try {
return $this->stream->readBytes($length);
} catch (Exception $e) {
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), E_USER_WARNING);
return false;
}
}
......@@ -146,15 +162,15 @@ class StreamWrapper
* @param integer $whence One of SEEK_SET, SEEK_CUR, or SEEK_END
* @return boolean True if the position was updated and false otherwise
*/
public function stream_seek($offset, $whence = \SEEK_SET)
public function stream_seek($offset, $whence = SEEK_SET)
{
$size = $this->stream->getSize();
if ($whence === \SEEK_CUR) {
if ($whence === SEEK_CUR) {
$offset += $this->stream->tell();
}
if ($whence === \SEEK_END) {
if ($whence === SEEK_END) {
$offset += $size;
}
......@@ -222,14 +238,15 @@ class StreamWrapper
*/
public function stream_write($data)
{
if ( ! $this->stream instanceof WritableStream) {
if (! $this->stream instanceof WritableStream) {
return 0;
}
try {
return $this->stream->writeBytes($data);
} catch (Exception $e) {
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), \E_USER_WARNING);
trigger_error(sprintf('%s: %s', get_class($e), $e->getMessage()), E_USER_WARNING);
return false;
}
}
......
......@@ -23,6 +23,19 @@ use MongoDB\BSON\UTCDateTime;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use stdClass;
use function array_intersect_key;
use function hash_final;
use function hash_init;
use function hash_update;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\is_string_array;
use function sprintf;
use function strlen;
use function substr;
/**
* WritableStream abstracts the process of writing a GridFS file.
......@@ -74,12 +87,12 @@ class WritableStream
public function __construct(CollectionWrapper $collectionWrapper, $filename, array $options = [])
{
$options += [
'_id' => new ObjectId,
'_id' => new ObjectId(),
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
'disableMD5' => false,
];
if (isset($options['aliases']) && ! \MongoDB\is_string_array($options['aliases'])) {
if (isset($options['aliases']) && ! is_string_array($options['aliases'])) {
throw InvalidArgumentException::invalidType('"aliases" option', $options['aliases'], 'array of strings');
}
......@@ -107,7 +120,7 @@ class WritableStream
$this->collectionWrapper = $collectionWrapper;
$this->disableMD5 = $options['disableMD5'];
if ( ! $this->disableMD5) {
if (! $this->disableMD5) {
$this->hashCtx = hash_init('md5');
}
......@@ -233,9 +246,9 @@ class WritableStream
private function fileCollectionInsert()
{
$this->file['length'] = $this->length;
$this->file['uploadDate'] = new UTCDateTime;
$this->file['uploadDate'] = new UTCDateTime();
if ( ! $this->disableMD5) {
if (! $this->disableMD5) {
$this->file['md5'] = hash_final($this->hashCtx);
}
......@@ -265,7 +278,7 @@ class WritableStream
'data' => new Binary($data, Binary::TYPE_GENERIC),
];
if ( ! $this->disableMD5) {
if (! $this->disableMD5) {
hash_update($this->hashCtx, $data);
}
......
......@@ -30,8 +30,6 @@ class InsertManyResult
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed[] $insertedIds
*/
......
......@@ -30,8 +30,6 @@ class InsertOneResult
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
* @param mixed $insertedId
*/
......
......@@ -20,6 +20,7 @@ namespace MongoDB;
use IteratorAggregate;
use stdClass;
use Traversable;
use function call_user_func;
/**
* Result class for mapReduce command results.
......@@ -40,8 +41,6 @@ class MapReduceResult implements IteratorAggregate
private $timing;
/**
* Constructor.
*
* @internal
* @param callable $getIterator Callback that returns a Traversable for mapReduce results
* @param stdClass $result Result document from the mapReduce command
......
......@@ -17,10 +17,12 @@
namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use function array_values;
use function MongoDB\recursive_copy;
/**
* Model class for a BSON array.
......@@ -38,7 +40,7 @@ class BSONArray extends ArrayObject implements JsonSerializable, Serializable, U
public function __clone()
{
foreach ($this as $key => $value) {
$this[$key] = \MongoDB\recursive_copy($value);
$this[$key] = recursive_copy($value);
}
}
......@@ -52,7 +54,7 @@ class BSONArray extends ArrayObject implements JsonSerializable, Serializable, U
*/
public static function __set_state(array $properties)
{
$array = new static;
$array = new static();
$array->exchangeArray($properties);
return $array;
......
......@@ -17,10 +17,11 @@
namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use ArrayObject;
use JsonSerializable;
use MongoDB\BSON\Serializable;
use MongoDB\BSON\Unserializable;
use function MongoDB\recursive_copy;
/**
* Model class for a BSON document.
......@@ -38,13 +39,11 @@ class BSONDocument extends ArrayObject implements JsonSerializable, Serializable
public function __clone()
{
foreach ($this as $key => $value) {
$this[$key] = \MongoDB\recursive_copy($value);
$this[$key] = recursive_copy($value);
}
}
/**
* Constructor.
*
* This overrides the parent constructor to allow property access of entries
* by default.
*
......@@ -68,7 +67,7 @@ class BSONDocument extends ArrayObject implements JsonSerializable, Serializable
*/
public static function __set_state(array $properties)
{
$document = new static;
$document = new static();
$document->exchangeArray($properties);
return $document;
......
......@@ -17,10 +17,15 @@
namespace MongoDB\Model;
use Iterator;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Model\BSONDocument;
use Iterator;
use function is_array;
use function MongoDB\BSON\toPHP;
use function sprintf;
use function strlen;
use function substr;
use function unpack;
/**
* Iterator for BSON documents.
......@@ -55,7 +60,7 @@ class BSONIterator implements Iterator
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
if ( ! isset($options['typeMap'])) {
if (! isset($options['typeMap'])) {
$options['typeMap'] = [];
}
......@@ -130,7 +135,7 @@ class BSONIterator implements Iterator
throw new UnexpectedValueException(sprintf('Expected %d bytes; %d remaining', $documentLength, $this->bufferLength - $this->position));
}
$this->current = \MongoDB\BSON\toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
$this->current = toPHP(substr($this->buffer, $this->position, $documentLength), $this->options['typeMap']);
$this->position += $documentLength;
}
}
......@@ -21,6 +21,11 @@ use Countable;
use Generator;
use Iterator;
use Traversable;
use function count;
use function current;
use function key;
use function next;
use function reset;
/**
* Iterator for wrapping a Traversable and caching its results.
......@@ -39,8 +44,6 @@ class CachingIterator implements Countable, Iterator
private $iteratorExhausted = false;
/**
* Constructor.
*
* Initialize the iterator and stores the first item in the cache. This
* effectively rewinds the Traversable and the wrapping Generator, which
* will execute up to its first yield statement. Additionally, this mimics
......@@ -90,7 +93,7 @@ class CachingIterator implements Countable, Iterator
*/
public function next()
{
if ( ! $this->iteratorExhausted) {
if (! $this->iteratorExhausted) {
$this->iterator->next();
$this->storeCurrentItem();
}
......@@ -128,7 +131,7 @@ class CachingIterator implements Countable, Iterator
*/
private function exhaustIterator()
{
while ( ! $this->iteratorExhausted) {
while (! $this->iteratorExhausted) {
$this->next();
}
}
......
......@@ -17,16 +17,22 @@
namespace MongoDB\Model;
use IteratorIterator;
use MongoDB\BSON\Serializable;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\ResumeTokenException;
use MongoDB\Exception\UnexpectedValueException;
use IteratorIterator;
use function count;
use function is_array;
use function is_integer;
use function is_object;
use function MongoDB\Driver\Monitoring\addSubscriber;
use function MongoDB\Driver\Monitoring\removeSubscriber;
/**
* ChangeStreamIterator wraps a change stream's tailable cursor.
......@@ -46,8 +52,6 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
private $resumeToken;
/**
* Constructor.
*
* @internal
* @param Cursor $cursor
* @param integer $firstBatchSize
......@@ -56,7 +60,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
*/
public function __construct(Cursor $cursor, $firstBatchSize, $initialResumeToken, $postBatchResumeToken)
{
if ( ! is_integer($firstBatchSize)) {
if (! is_integer($firstBatchSize)) {
throw InvalidArgumentException::invalidType('$firstBatchSize', $firstBatchSize, 'integer');
}
......@@ -102,7 +106,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
$reply = $event->getReply();
if ( ! isset($reply->cursor->nextBatch) || ! is_array($reply->cursor->nextBatch)) {
if (! isset($reply->cursor->nextBatch) || ! is_array($reply->cursor->nextBatch)) {
throw new UnexpectedValueException('getMore command did not return a "cursor.nextBatch" array');
}
......@@ -141,15 +145,15 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
$getMore = $this->isAtEndOfBatch();
if ($getMore) {
\MongoDB\Driver\Monitoring\addSubscriber($this);
addSubscriber($this);
}
try {
parent::next();
$this->onIteration(!$getMore);
$this->onIteration(! $getMore);
} finally {
if ($getMore) {
\MongoDB\Driver\Monitoring\removeSubscriber($this);
removeSubscriber($this);
}
}
}
......@@ -178,7 +182,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
*/
private function extractResumeToken($document)
{
if ( ! is_array($document) && ! is_object($document)) {
if (! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
......@@ -190,11 +194,11 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
? (isset($document['_id']) ? $document['_id'] : null)
: (isset($document->_id) ? $document->_id : null);
if ( ! isset($resumeToken)) {
if (! isset($resumeToken)) {
throw ResumeTokenException::notFound();
}
if ( ! is_array($resumeToken) && ! is_object($resumeToken)) {
if (! is_array($resumeToken) && ! is_object($resumeToken)) {
throw ResumeTokenException::invalidType($resumeToken);
}
......@@ -208,7 +212,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
*/
private function isAtEndOfBatch()
{
return ($this->batchPosition + 1 >= $this->batchSize);
return $this->batchPosition + 1 >= $this->batchSize;
}
/**
......
......@@ -17,8 +17,9 @@
namespace MongoDB\Model;
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;
use MongoDB\Exception\BadMethodCallException;
use function array_key_exists;
/**
* Collection information model class.
......@@ -36,8 +37,6 @@ class CollectionInfo implements ArrayAccess
private $info;
/**
* Constructor.
*
* @param array $info Collection info
*/
public function __construct(array $info)
......@@ -142,7 +141,7 @@ class CollectionInfo implements ArrayAccess
*/
public function offsetSet($key, $value)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
/**
......@@ -154,6 +153,6 @@ class CollectionInfo implements ArrayAccess
*/
public function offsetUnset($key)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
}
......@@ -17,8 +17,10 @@
namespace MongoDB\Model;
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;
use MongoDB\Exception\BadMethodCallException;
use function array_key_exists;
/**
* Database information model class.
*
......@@ -34,8 +36,6 @@ class DatabaseInfo implements ArrayAccess
private $info;
/**
* Constructor.
*
* @param array $info Database info
*/
public function __construct(array $info)
......@@ -119,7 +119,7 @@ class DatabaseInfo implements ArrayAccess
*/
public function offsetSet($key, $value)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
/**
......@@ -131,6 +131,6 @@ class DatabaseInfo implements ArrayAccess
*/
public function offsetUnset($key)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
}
......@@ -17,6 +17,11 @@
namespace MongoDB\Model;
use function current;
use function key;
use function next;
use function reset;
/**
* DatabaseInfoIterator for inline listDatabases command results.
*
......@@ -32,8 +37,6 @@ class DatabaseInfoLegacyIterator implements DatabaseInfoIterator
private $databases;
/**
* Constructor.
*
* @param array $databases
*/
public function __construct(array $databases)
......
......@@ -17,8 +17,10 @@
namespace MongoDB\Model;
use MongoDB\Exception\BadMethodCallException;
use ArrayAccess;
use MongoDB\Exception\BadMethodCallException;
use function array_key_exists;
use function array_search;
/**
* Index information model class.
......@@ -40,8 +42,6 @@ class IndexInfo implements ArrayAccess
private $info;
/**
* Constructor.
*
* @param array $info Index info
*/
public function __construct(array $info)
......@@ -212,7 +212,7 @@ class IndexInfo implements ArrayAccess
*/
public function offsetSet($key, $value)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
/**
......@@ -224,6 +224,6 @@ class IndexInfo implements ArrayAccess
*/
public function offsetUnset($key)
{
throw BadMethodCallException::classIsImmutable(__CLASS__);
throw BadMethodCallException::classIsImmutable(self::class);
}
}
......@@ -19,6 +19,13 @@ namespace MongoDB\Model;
use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use function is_array;
use function is_float;
use function is_int;
use function is_object;
use function is_string;
use function MongoDB\generate_index_name;
use function sprintf;
/**
* Index input model class.
......@@ -35,40 +42,38 @@ class IndexInput implements Serializable
private $index;
/**
* Constructor.
*
* @param array $index Index specification
* @throws InvalidArgumentException
*/
public function __construct(array $index)
{
if ( ! isset($index['key'])) {
if (! isset($index['key'])) {
throw new InvalidArgumentException('Required "key" document is missing from index specification');
}
if ( ! is_array($index['key']) && ! is_object($index['key'])) {
if (! is_array($index['key']) && ! is_object($index['key'])) {
throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
}
foreach ($index['key'] as $fieldName => $order) {
if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) {
if (! is_int($order) && ! is_float($order) && ! is_string($order)) {
throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
}
}
if ( ! isset($index['ns'])) {
if (! isset($index['ns'])) {
throw new InvalidArgumentException('Required "ns" option is missing from index specification');
}
if ( ! is_string($index['ns'])) {
if (! is_string($index['ns'])) {
throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
}
if ( ! isset($index['name'])) {
$index['name'] = \MongoDB\generate_index_name($index['key']);
if (! isset($index['name'])) {
$index['name'] = generate_index_name($index['key']);
}
if ( ! is_string($index['name'])) {
if (! is_string($index['name'])) {
throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
}
......
......@@ -17,19 +17,29 @@
namespace MongoDB\Operation;
use ArrayIterator;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use ArrayIterator;
use stdClass;
use Traversable;
use function current;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\create_field_path_type_map;
use function MongoDB\is_last_pipeline_operator_write;
use function MongoDB\server_supports_feature;
use function sprintf;
/**
* Operation for the aggregate command.
......@@ -132,7 +142,7 @@ class Aggregate implements Executable
throw new InvalidArgumentException(sprintf('$pipeline is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($operation) && ! is_object($operation)) {
if (! is_array($operation) && ! is_object($operation)) {
throw InvalidArgumentException::invalidType(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
}
......@@ -144,7 +154,7 @@ class Aggregate implements Executable
'useCursor' => true,
];
if ( ! is_bool($options['allowDiskUse'])) {
if (! is_bool($options['allowDiskUse'])) {
throw InvalidArgumentException::invalidType('"allowDiskUse" option', $options['allowDiskUse'], 'boolean');
}
......@@ -196,7 +206,7 @@ class Aggregate implements Executable
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
}
if ( ! is_bool($options['useCursor'])) {
if (! is_bool($options['useCursor'])) {
throw InvalidArgumentException::invalidType('"useCursor" option', $options['useCursor'], 'boolean');
}
......@@ -216,7 +226,7 @@ class Aggregate implements Executable
unset($options['writeConcern']);
}
if ( ! empty($options['explain'])) {
if (! empty($options['explain'])) {
$options['useCursor'] = false;
}
......@@ -238,15 +248,15 @@ class Aggregate implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern']) && ! server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......@@ -260,14 +270,13 @@ class Aggregate implements Executable
}
}
$hasExplain = ! empty($this->options['explain']);
$hasWriteStage = \MongoDB\is_last_pipeline_operator_write($this->pipeline);
$hasWriteStage = is_last_pipeline_operator_write($this->pipeline);
$command = $this->createCommand($server, $hasWriteStage);
$options = $this->createOptions($hasWriteStage, $hasExplain);
$cursor = ($hasWriteStage && ! $hasExplain)
$cursor = $hasWriteStage && ! $hasExplain
? $server->executeReadWriteCommand($this->databaseName, $command, $options)
: $server->executeReadCommand($this->databaseName, $command, $options);
......@@ -280,12 +289,12 @@ class Aggregate implements Executable
}
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap(\MongoDB\create_field_path_type_map($this->options['typeMap'], 'result.$'));
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'result.$'));
}
$result = current($cursor->toArray());
if ( ! isset($result->result) || ! is_array($result->result)) {
if (! isset($result->result) || ! is_array($result->result)) {
throw new UnexpectedValueException('aggregate command did not return a "result" array');
}
......@@ -309,9 +318,8 @@ class Aggregate implements Executable
$cmd['allowDiskUse'] = $this->options['allowDiskUse'];
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......@@ -338,9 +346,9 @@ class Aggregate implements Executable
/* Ignore batchSize if pipeline includes an $out or $merge stage, as
* no documents will be returned and sending a batchSize of zero
* could prevent the pipeline from executing at all. */
$cmd['cursor'] = isset($this->options["batchSize"]) && !$hasWriteStage
$cmd['cursor'] = isset($this->options["batchSize"]) && ! $hasWriteStage
? ['batchSize' => $this->options["batchSize"]]
: new stdClass;
: new stdClass();
}
return new Command($cmd, $cmdOptions);
......@@ -363,7 +371,7 @@ class Aggregate implements Executable
$options['readConcern'] = $this->options['readConcern'];
}
if (!$hasWriteStage && isset($this->options['readPreference'])) {
if (! $hasWriteStage && isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
......
......@@ -19,12 +19,22 @@ namespace MongoDB\Operation;
use MongoDB\BulkWriteResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function array_key_exists;
use function count;
use function current;
use function is_array;
use function is_bool;
use function is_object;
use function key;
use function MongoDB\is_first_key_operator;
use function MongoDB\server_supports_feature;
use function sprintf;
/**
* Operation for executing multiple write operations.
......@@ -132,7 +142,7 @@ class BulkWrite implements Executable
throw new InvalidArgumentException(sprintf('$operations is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($operation)) {
if (! is_array($operation)) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
}
......@@ -143,11 +153,11 @@ class BulkWrite implements Executable
$type = key($operation);
$args = current($operation);
if ( ! isset($args[0]) && ! array_key_exists(0, $args)) {
if (! isset($args[0]) && ! array_key_exists(0, $args)) {
throw new InvalidArgumentException(sprintf('Missing first argument for $operations[%d]["%s"]', $i, $type));
}
if ( ! is_array($args[0]) && ! is_object($args[0])) {
if (! is_array($args[0]) && ! is_object($args[0])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'array or object');
}
......@@ -157,11 +167,11 @@ class BulkWrite implements Executable
case self::DELETE_MANY:
case self::DELETE_ONE:
if ( ! isset($args[1])) {
if (! isset($args[1])) {
$args[1] = [];
}
if ( ! is_array($args[1])) {
if (! is_array($args[1])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array');
}
......@@ -170,7 +180,7 @@ class BulkWrite implements Executable
if (isset($args[1]['collation'])) {
$this->isCollationUsed = true;
if ( ! is_array($args[1]['collation']) && ! is_object($args[1]['collation'])) {
if (! is_array($args[1]['collation']) && ! is_object($args[1]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation'], 'array or object');
}
}
......@@ -180,23 +190,23 @@ class BulkWrite implements Executable
break;
case self::REPLACE_ONE:
if ( ! isset($args[1]) && ! array_key_exists(1, $args)) {
if (! isset($args[1]) && ! array_key_exists(1, $args)) {
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}
if ( ! is_array($args[1]) && ! is_object($args[1])) {
if (! is_array($args[1]) && ! is_object($args[1])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
}
if (\MongoDB\is_first_key_operator($args[1])) {
if (is_first_key_operator($args[1])) {
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is an update operator', $i, $type));
}
if ( ! isset($args[2])) {
if (! isset($args[2])) {
$args[2] = [];
}
if ( ! is_array($args[2])) {
if (! is_array($args[2])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
}
......@@ -206,12 +216,12 @@ class BulkWrite implements Executable
if (isset($args[2]['collation'])) {
$this->isCollationUsed = true;
if ( ! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
if (! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'array or object');
}
}
if ( ! is_bool($args[2]['upsert'])) {
if (! is_bool($args[2]['upsert'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
}
......@@ -221,23 +231,23 @@ class BulkWrite implements Executable
case self::UPDATE_MANY:
case self::UPDATE_ONE:
if ( ! isset($args[1]) && ! array_key_exists(1, $args)) {
if (! isset($args[1]) && ! array_key_exists(1, $args)) {
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}
if ( ! is_array($args[1]) && ! is_object($args[1])) {
if (! is_array($args[1]) && ! is_object($args[1])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
}
if ( ! \MongoDB\is_first_key_operator($args[1])) {
if (! is_first_key_operator($args[1])) {
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is not an update operator', $i, $type));
}
if ( ! isset($args[2])) {
if (! isset($args[2])) {
$args[2] = [];
}
if ( ! is_array($args[2])) {
if (! is_array($args[2])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
}
......@@ -247,7 +257,7 @@ class BulkWrite implements Executable
if (isset($args[2]['arrayFilters'])) {
$this->isArrayFiltersUsed = true;
if ( ! is_array($args[2]['arrayFilters'])) {
if (! is_array($args[2]['arrayFilters'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["arrayFilters"]', $i, $type), $args[2]['arrayFilters'], 'array');
}
}
......@@ -255,12 +265,12 @@ class BulkWrite implements Executable
if (isset($args[2]['collation'])) {
$this->isCollationUsed = true;
if ( ! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
if (! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'array or object');
}
}
if ( ! is_bool($args[2]['upsert'])) {
if (! is_bool($args[2]['upsert'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
}
......@@ -281,7 +291,7 @@ class BulkWrite implements Executable
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}
if ( ! is_bool($options['ordered'])) {
if (! is_bool($options['ordered'])) {
throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
}
......@@ -314,11 +324,11 @@ class BulkWrite implements Executable
*/
public function execute(Server $server)
{
if ($this->isArrayFiltersUsed && ! \MongoDB\server_supports_feature($server, self::$wireVersionForArrayFilters)) {
if ($this->isArrayFiltersUsed && ! server_supports_feature($server, self::$wireVersionForArrayFilters)) {
throw UnsupportedException::arrayFiltersNotSupported();
}
if ($this->isCollationUsed && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if ($this->isCollationUsed && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
......@@ -329,9 +339,8 @@ class BulkWrite implements Executable
$options = ['ordered' => $this->options['ordered']];
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......
......@@ -18,14 +18,21 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_float;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\server_supports_feature;
/**
* Operation for the count command.
......@@ -85,7 +92,7 @@ class Count implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter = [], array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......@@ -143,11 +150,11 @@ class Count implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern']) && ! server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
......@@ -160,7 +167,7 @@ class Count implements Executable, Explainable
$result = current($cursor->toArray());
// Older server versions may return a float
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
if (! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return a numeric "n" value');
}
......@@ -181,7 +188,7 @@ class Count implements Executable, Explainable
{
$cmd = ['count' => $this->collectionName];
if ( ! empty($this->filter)) {
if (! empty($this->filter)) {
$cmd['query'] = (object) $this->filter;
}
......
......@@ -17,11 +17,18 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use function array_intersect_key;
use function count;
use function current;
use function is_array;
use function is_float;
use function is_integer;
use function is_object;
/**
* Operation for obtaining an exact count of documents in a collection
......@@ -80,7 +87,7 @@ class CountDocuments implements Executable
*/
public function __construct($databaseName, $collectionName, $filter, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......@@ -124,7 +131,7 @@ class CountDocuments implements Executable
}
$result = current($allResults);
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
if (! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return a numeric "n" value');
}
......@@ -137,7 +144,7 @@ class CountDocuments implements Executable
private function createAggregate()
{
$pipeline = [
['$match' => (object) $this->filter]
['$match' => (object) $this->filter],
];
if (isset($this->countOptions['skip'])) {
......
......@@ -18,12 +18,21 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\server_supports_feature;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* Operation for the create command.
......@@ -194,11 +203,11 @@ class CreateCollection implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......
......@@ -17,15 +17,19 @@
namespace MongoDB\Operation;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\IndexInput;
use function array_map;
use function is_array;
use function is_integer;
use function MongoDB\server_supports_feature;
use function sprintf;
/**
* Operation for the createIndexes command.
......@@ -82,11 +86,11 @@ class CreateIndexes implements Executable
throw new InvalidArgumentException(sprintf('$indexes is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($index)) {
if (! is_array($index)) {
throw InvalidArgumentException::invalidType(sprintf('$index[%d]', $i), $index, 'array');
}
if ( ! isset($index['ns'])) {
if (! isset($index['ns'])) {
$index['ns'] = $databaseName . '.' . $collectionName;
}
......@@ -99,7 +103,7 @@ class CreateIndexes implements Executable
$expectedIndex += 1;
}
if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
......@@ -131,11 +135,11 @@ class CreateIndexes implements Executable
*/
public function execute(Server $server)
{
if ($this->isCollationUsed && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if ($this->isCollationUsed && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......@@ -146,7 +150,9 @@ class CreateIndexes implements Executable
$this->executeCommand($server);
return array_map(function(IndexInput $index) { return (string) $index; }, $this->indexes);
return array_map(function (IndexInput $index) {
return (string) $index;
}, $this->indexes);
}
/**
......
......@@ -23,6 +23,8 @@ use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Exception\InvalidArgumentException;
use function is_array;
use function is_object;
/**
* Operation for executing a database command.
......@@ -54,14 +56,14 @@ class DatabaseCommand implements Executable
* * typeMap (array): Type map for BSON deserialization. This will be
* applied to the returned Cursor (it is not sent to the server).
*
* @param string $databaseName Database name
* @param array|object $command Command document
* @param array $options Options for command execution
* @param string $databaseName Database name
* @param array|object $command Command document
* @param array $options Options for command execution
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct($databaseName, $command, array $options = [])
{
if ( ! is_array($command) && ! is_object($command)) {
if (! is_array($command) && ! is_object($command)) {
throw InvalidArgumentException::invalidType('$command', $command, 'array or object');
}
......@@ -78,7 +80,7 @@ class DatabaseCommand implements Executable
}
$this->databaseName = (string) $databaseName;
$this->command = ($command instanceof Command) ? $command : new Command($command);
$this->command = $command instanceof Command ? $command : new Command($command);
$this->options = $options;
}
......
......@@ -19,12 +19,15 @@ namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function is_array;
use function is_object;
use function MongoDB\server_supports_feature;
/**
* Operation for the delete command.
......@@ -72,7 +75,7 @@ class Delete implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $limit, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......@@ -113,7 +116,7 @@ class Delete implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
......
......@@ -18,8 +18,8 @@
namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
......
......@@ -18,8 +18,8 @@
namespace MongoDB\Operation;
use MongoDB\DeleteResult;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
......
......@@ -18,14 +18,20 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_integer;
use function is_object;
use function MongoDB\create_field_path_type_map;
use function MongoDB\server_supports_feature;
/**
* Operation for the distinct command.
......@@ -80,7 +86,7 @@ class Distinct implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $fieldName, $filter = [], array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......@@ -131,11 +137,11 @@ class Distinct implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern']) && ! server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
......@@ -147,12 +153,12 @@ class Distinct implements Executable, Explainable
$cursor = $server->executeReadCommand($this->databaseName, new Command($this->createCommandDocument()), $this->createOptions());
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap(\MongoDB\create_field_path_type_map($this->options['typeMap'], 'values.$'));
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'values.$'));
}
$result = current($cursor->toArray());
if ( ! isset($result->values) || ! is_array($result->values)) {
if (! isset($result->values) || ! is_array($result->values)) {
throw new UnexpectedValueException('distinct command did not return a "values" array');
}
......@@ -176,7 +182,7 @@ class Distinct implements Executable, Explainable
'key' => $this->fieldName,
];
if ( ! empty($this->filter)) {
if (! empty($this->filter)) {
$cmd['query'] = (object) $this->filter;
}
......
......@@ -18,12 +18,15 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function MongoDB\server_supports_feature;
/**
* Operation for the drop command.
......@@ -98,7 +101,7 @@ class DropCollection implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......
......@@ -18,12 +18,15 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function MongoDB\server_supports_feature;
/**
* Operation for the dropDatabase command.
......@@ -94,7 +97,7 @@ class DropDatabase implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......
......@@ -18,12 +18,16 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_integer;
use function MongoDB\server_supports_feature;
/**
* Operation for the dropIndexes command.
......@@ -112,7 +116,7 @@ class DropIndexes implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......
......@@ -17,11 +17,12 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use function array_intersect_key;
/**
* Operation for obtaining an estimated count of documents in a collection
......@@ -56,9 +57,9 @@ class EstimatedDocumentCount implements Executable, Explainable
*
* Sessions are not supported for server versions < 3.6.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct($databaseName, $collectionName, array $options = [])
......
......@@ -21,9 +21,12 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\BSONDocument;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_string;
use function MongoDB\server_supports_feature;
/**
* Operation for the explain command.
......@@ -59,9 +62,9 @@ class Explain implements Executable
*
* * verbosity (string): The mode in which the explain command will be run.
*
* @param string $databaseName Database name
* @param string $databaseName Database name
* @param Explainable $explainable Operation to explain
* @param array $options Command options
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct($databaseName, Explainable $explainable, array $options = [])
......@@ -89,11 +92,11 @@ class Explain implements Executable
public function execute(Server $server)
{
if ($this->explainable instanceof Distinct && ! \MongoDB\server_supports_feature($server, self::$wireVersionForDistinct)) {
if ($this->explainable instanceof Distinct && ! server_supports_feature($server, self::$wireVersionForDistinct)) {
throw UnsupportedException::explainNotSupported();
}
if ($this->isFindAndModify($this->explainable) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForFindAndModify)) {
if ($this->isFindAndModify($this->explainable) && ! server_supports_feature($server, self::$wireVersionForFindAndModify)) {
throw UnsupportedException::explainNotSupported();
}
......@@ -138,6 +141,7 @@ class Explain implements Executable
if ($explainable instanceof FindAndModify || $explainable instanceof FindOneAndDelete || $explainable instanceof FindOneAndReplace || $explainable instanceof FindOneAndUpdate) {
return true;
}
return false;
}
}
......@@ -18,15 +18,23 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\BSONDocument;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\server_supports_feature;
use function trigger_error;
use const E_USER_DEPRECATED;
/**
* Operation for the find command.
*
......@@ -147,7 +155,7 @@ class Find implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......@@ -168,7 +176,7 @@ class Find implements Executable, Explainable
}
if (isset($options['cursorType'])) {
if ( ! is_integer($options['cursorType'])) {
if (! is_integer($options['cursorType'])) {
throw InvalidArgumentException::invalidType('"cursorType" option', $options['cursorType'], 'integer');
}
......@@ -288,11 +296,11 @@ class Find implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern']) && ! server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
......@@ -346,7 +354,7 @@ class Find implements Executable, Explainable
];
foreach ($modifierFallback as $modifier) {
if ( ! isset($options[$modifier[0]]) && isset($options['modifiers'][$modifier[1]])) {
if (! isset($options[$modifier[0]]) && isset($options['modifiers'][$modifier[1]])) {
$options[$modifier[0]] = $options['modifiers'][$modifier[1]];
}
}
......@@ -412,7 +420,7 @@ class Find implements Executable, Explainable
$modifiers = empty($this->options['modifiers']) ? [] : (array) $this->options['modifiers'];
if ( ! empty($modifiers)) {
if (! empty($modifiers)) {
$options['modifiers'] = $modifiers;
}
......
......@@ -18,13 +18,20 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function MongoDB\create_field_path_type_map;
use function MongoDB\server_supports_feature;
/**
* Operation for the findAndModify command.
......@@ -137,7 +144,7 @@ class FindAndModify implements Executable, Explainable
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
if ( ! is_bool($options['new'])) {
if (! is_bool($options['new'])) {
throw InvalidArgumentException::invalidType('"new" option', $options['new'], 'boolean');
}
......@@ -145,7 +152,7 @@ class FindAndModify implements Executable, Explainable
throw InvalidArgumentException::invalidType('"query" option', $options['query'], 'array or object');
}
if ( ! is_bool($options['remove'])) {
if (! is_bool($options['remove'])) {
throw InvalidArgumentException::invalidType('"remove" option', $options['remove'], 'boolean');
}
......@@ -169,11 +176,11 @@ class FindAndModify implements Executable, Explainable
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
}
if ( ! is_bool($options['upsert'])) {
if (! is_bool($options['upsert'])) {
throw InvalidArgumentException::invalidType('"upsert" option', $options['upsert'], 'boolean');
}
if ( ! (isset($options['update']) xor $options['remove'])) {
if (! (isset($options['update']) xor $options['remove'])) {
throw new InvalidArgumentException('The "remove" option must be true or an "update" document must be specified, but not both');
}
......@@ -198,15 +205,15 @@ class FindAndModify implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['arrayFilters']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForArrayFilters)) {
if (isset($this->options['arrayFilters']) && ! server_supports_feature($server, self::$wireVersionForArrayFilters)) {
throw UnsupportedException::arrayFiltersNotSupported();
}
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......@@ -218,7 +225,7 @@ class FindAndModify implements Executable, Explainable
$cursor = $server->executeWriteCommand($this->databaseName, new Command($this->createCommandDocument($server)), $this->createOptions());
if (isset($this->options['typeMap'])) {
$cursor->setTypeMap(\MongoDB\create_field_path_type_map($this->options['typeMap'], 'value'));
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'value'));
}
$result = current($cursor->toArray());
......@@ -262,9 +269,8 @@ class FindAndModify implements Executable, Explainable
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......
......@@ -17,10 +17,11 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
/**
* Operation for finding a single document with the find command.
......@@ -126,7 +127,7 @@ class FindOne implements Executable, Explainable
$cursor = $this->find->execute($server);
$document = current($cursor->toArray());
return ($document === false) ? null : $document;
return $document === false ? null : $document;
}
public function getCommandDocument(Server $server)
......
......@@ -17,10 +17,12 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function is_array;
use function is_object;
/**
* Operation for deleting a document with the findAndModify command.
......@@ -71,7 +73,7 @@ class FindOneAndDelete implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
......
......@@ -17,10 +17,14 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function is_array;
use function is_integer;
use function is_object;
use function MongoDB\is_first_key_operator;
/**
* Operation for replacing a document with the findAndModify command.
......@@ -90,15 +94,15 @@ class FindOneAndReplace implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
if ( ! is_array($replacement) && ! is_object($replacement)) {
if (! is_array($replacement) && ! is_object($replacement)) {
throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
}
if (\MongoDB\is_first_key_operator($replacement)) {
if (is_first_key_operator($replacement)) {
throw new InvalidArgumentException('First key in $replacement argument is an update operator');
}
......@@ -111,7 +115,7 @@ class FindOneAndReplace implements Executable, Explainable
throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
}
if ( ! is_integer($options['returnDocument'])) {
if (! is_integer($options['returnDocument'])) {
throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
}
......
......@@ -17,10 +17,14 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function is_array;
use function is_integer;
use function is_object;
use function MongoDB\is_first_key_operator;
/**
* Operation for updating a document with the findAndModify command.
......@@ -93,15 +97,15 @@ class FindOneAndUpdate implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
if ( ! is_array($update) && ! is_object($update)) {
if (! is_array($update) && ! is_object($update)) {
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
}
if ( ! \MongoDB\is_first_key_operator($update)) {
if (! is_first_key_operator($update)) {
throw new InvalidArgumentException('First key in $update argument is not an update operator');
}
......@@ -114,7 +118,7 @@ class FindOneAndUpdate implements Executable, Explainable
throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
}
if ( ! is_integer($options['returnDocument'])) {
if (! is_integer($options['returnDocument'])) {
throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
}
......
......@@ -17,14 +17,19 @@
namespace MongoDB\Operation;
use MongoDB\InsertManyResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\InsertManyResult;
use function is_array;
use function is_bool;
use function is_object;
use function MongoDB\server_supports_feature;
use function sprintf;
/**
* Operation for inserting multiple documents with the insert command.
......@@ -82,7 +87,7 @@ class InsertMany implements Executable
throw new InvalidArgumentException(sprintf('$documents is not a list (unexpected index: "%s")', $i));
}
if ( ! is_array($document) && ! is_object($document)) {
if (! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType(sprintf('$documents[%d]', $i), $document, 'array or object');
}
......@@ -95,7 +100,7 @@ class InsertMany implements Executable
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}
if ( ! is_bool($options['ordered'])) {
if (! is_bool($options['ordered'])) {
throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
}
......@@ -134,9 +139,8 @@ class InsertMany implements Executable
$options = ['ordered' => $this->options['ordered']];
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......
......@@ -17,14 +17,18 @@
namespace MongoDB\Operation;
use MongoDB\InsertOneResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\InsertOneResult;
use function is_array;
use function is_bool;
use function is_object;
use function MongoDB\server_supports_feature;
/**
* Operation for inserting a single document with the insert command.
......@@ -67,7 +71,7 @@ class InsertOne implements Executable
*/
public function __construct($databaseName, $collectionName, $document, array $options = [])
{
if ( ! is_array($document) && ! is_object($document)) {
if (! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
......@@ -110,9 +114,8 @@ class InsertOne implements Executable
throw UnsupportedException::writeConcernNotSupportedInTransaction();
}
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......
......@@ -18,13 +18,16 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
use MongoDB\Model\CollectionInfoCommandIterator;
use MongoDB\Model\CollectionInfoIterator;
use function is_array;
use function is_integer;
use function is_object;
/**
* Operation for the listCollections command.
......@@ -119,7 +122,7 @@ class ListCollections implements Executable
{
$cmd = ['listCollections' => 1];
if ( ! empty($this->options['filter'])) {
if (! empty($this->options['filter'])) {
$cmd['filter'] = (object) $this->options['filter'];
}
......
......@@ -18,13 +18,17 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Model\DatabaseInfoLegacyIterator;
use function current;
use function is_array;
use function is_integer;
use function is_object;
/**
* Operation for the ListDatabases command.
......@@ -86,7 +90,7 @@ class ListDatabases implements Executable
{
$cmd = ['listDatabases' => 1];
if ( ! empty($this->options['filter'])) {
if (! empty($this->options['filter'])) {
$cmd['filter'] = (object) $this->options['filter'];
}
......@@ -98,7 +102,7 @@ class ListDatabases implements Executable
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$result = current($cursor->toArray());
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
if (! isset($result['databases']) || ! is_array($result['databases'])) {
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
}
......
......@@ -17,16 +17,16 @@
namespace MongoDB\Operation;
use EmptyIterator;
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\CachingIterator;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator;
use EmptyIterator;
use function is_integer;
/**
* Operation for the listIndexes command.
......@@ -133,7 +133,7 @@ class ListIndexes implements Executable
* empty iterator instead of throwing.
*/
if ($e->getCode() === self::$errorCodeNamespaceNotFound || $e->getCode() === self::$errorCodeDatabaseNotFound) {
return new IndexInfoIteratorIterator(new EmptyIterator);
return new IndexInfoIteratorIterator(new EmptyIterator());
}
throw $e;
......
......@@ -17,20 +17,29 @@
namespace MongoDB\Operation;
use ArrayIterator;
use MongoDB\BSON\JavascriptInterface;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\MapReduceResult;
use ArrayIterator;
use stdClass;
use function current;
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\create_field_path_type_map;
use function MongoDB\is_mapreduce_output_inline;
use function MongoDB\server_supports_feature;
/**
* Operation for the mapReduce command.
......@@ -143,7 +152,7 @@ class MapReduce implements Executable
*/
public function __construct($databaseName, $collectionName, JavascriptInterface $map, JavascriptInterface $reduce, $out, array $options = [])
{
if ( ! is_string($out) && ! is_array($out) && ! is_object($out)) {
if (! is_string($out) && ! is_array($out) && ! is_object($out)) {
throw InvalidArgumentException::invalidType('$out', $out, 'string or array or object');
}
......@@ -235,15 +244,15 @@ class MapReduce implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
if (isset($this->options['readConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
if (isset($this->options['readConcern']) && ! server_supports_feature($server, self::$wireVersionForReadConcern)) {
throw UnsupportedException::readConcernNotSupported();
}
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......@@ -257,7 +266,7 @@ class MapReduce implements Executable
}
}
$hasOutputCollection = ! \MongoDB\is_mapreduce_output_inline($this->out);
$hasOutputCollection = ! is_mapreduce_output_inline($this->out);
$command = $this->createCommand($server);
$options = $this->createOptions($hasOutputCollection);
......@@ -267,7 +276,7 @@ class MapReduce implements Executable
: $server->executeReadCommand($this->databaseName, $command, $options);
if (isset($this->options['typeMap']) && ! $hasOutputCollection) {
$cursor->setTypeMap(\MongoDB\create_field_path_type_map($this->options['typeMap'], 'results.$'));
$cursor->setTypeMap(create_field_path_type_map($this->options['typeMap'], 'results.$'));
}
$result = current($cursor->toArray());
......@@ -304,9 +313,8 @@ class MapReduce implements Executable
}
}
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......@@ -328,7 +336,7 @@ class MapReduce implements Executable
if (isset($result->results) && is_array($result->results)) {
$results = $result->results;
return function() use ($results) {
return function () use ($results) {
return new ArrayIterator($results);
};
}
......@@ -340,7 +348,7 @@ class MapReduce implements Executable
? new Find($this->databaseName, $result->result, [], $options)
: new Find($result->result->db, $result->result->collection, [], $options);
return function() use ($find, $server) {
return function () use ($find, $server) {
return $find->execute($server);
};
}
......@@ -364,7 +372,7 @@ class MapReduce implements Executable
$options['readConcern'] = $this->options['readConcern'];
}
if ( ! $hasOutputCollection && isset($this->options['readPreference'])) {
if (! $hasOutputCollection && isset($this->options['readPreference'])) {
$options['readPreference'] = $this->options['readPreference'];
}
......
......@@ -18,12 +18,15 @@
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use function current;
use function is_array;
use function MongoDB\server_supports_feature;
/**
* Operation for the collMod command.
......@@ -56,10 +59,10 @@ class ModifyCollection implements Executable
* This is not supported for server versions < 3.2 and will result in an
* exception at execution time if used.
*
* @param string $databaseName Database name
* @param string $collectionName Collection or view to modify
* @param array $collectionOptions Collection or view options to assign
* @param array $options Command options
* @param string $databaseName Database name
* @param string $collectionName Collection or view to modify
* @param array $collectionOptions Collection or view options to assign
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct($databaseName, $collectionName, array $collectionOptions, array $options = [])
......@@ -100,7 +103,7 @@ class ModifyCollection implements Executable
*/
public function execute(Server $server)
{
if (isset($this->options['writeConcern']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForWriteConcern)) {
if (isset($this->options['writeConcern']) && ! server_supports_feature($server, self::$wireVersionForWriteConcern)) {
throw UnsupportedException::writeConcernNotSupported();
}
......
......@@ -17,11 +17,14 @@
namespace MongoDB\Operation;
use MongoDB\UpdateResult;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\UpdateResult;
use function is_array;
use function is_object;
use function MongoDB\is_first_key_operator;
/**
* Operation for replacing a single document with the update command.
......@@ -68,11 +71,11 @@ class ReplaceOne implements Executable
*/
public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = [])
{
if ( ! is_array($replacement) && ! is_object($replacement)) {
if (! is_array($replacement) && ! is_object($replacement)) {
throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
}
if (\MongoDB\is_first_key_operator($replacement)) {
if (is_first_key_operator($replacement)) {
throw new InvalidArgumentException('First key in $replacement argument is an update operator');
}
......
......@@ -17,14 +17,19 @@
namespace MongoDB\Operation;
use MongoDB\UpdateResult;
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\UpdateResult;
use function is_array;
use function is_bool;
use function is_object;
use function MongoDB\is_first_key_operator;
use function MongoDB\server_supports_feature;
/**
* Operation for the update command.
......@@ -92,11 +97,11 @@ class Update implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
{
if ( ! is_array($filter) && ! is_object($filter)) {
if (! is_array($filter) && ! is_object($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}
if ( ! is_array($update) && ! is_object($update)) {
if (! is_array($update) && ! is_object($update)) {
throw InvalidArgumentException::invalidType('$update', $filter, 'array or object');
}
......@@ -117,11 +122,11 @@ class Update implements Executable, Explainable
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
}
if ( ! is_bool($options['multi'])) {
if (! is_bool($options['multi'])) {
throw InvalidArgumentException::invalidType('"multi" option', $options['multi'], 'boolean');
}
if ($options['multi'] && ! \MongoDB\is_first_key_operator($update)) {
if ($options['multi'] && ! is_first_key_operator($update)) {
throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document');
}
......@@ -129,7 +134,7 @@ class Update implements Executable, Explainable
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
}
if ( ! is_bool($options['upsert'])) {
if (! is_bool($options['upsert'])) {
throw InvalidArgumentException::invalidType('"upsert" option', $options['upsert'], 'boolean');
}
......@@ -159,11 +164,11 @@ class Update implements Executable, Explainable
*/
public function execute(Server $server)
{
if (isset($this->options['arrayFilters']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForArrayFilters)) {
if (isset($this->options['arrayFilters']) && ! server_supports_feature($server, self::$wireVersionForArrayFilters)) {
throw UnsupportedException::arrayFiltersNotSupported();
}
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
if (isset($this->options['collation']) && ! server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}
......@@ -174,9 +179,8 @@ class Update implements Executable, Explainable
$bulkOptions = [];
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$bulkOptions['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......@@ -197,9 +201,8 @@ class Update implements Executable, Explainable
$cmd['writeConcern'] = $this->options['writeConcern'];
}
if (
! empty($this->options['bypassDocumentValidation']) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
if (! empty($this->options['bypassDocumentValidation']) &&
server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)
) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}
......
......@@ -17,11 +17,14 @@
namespace MongoDB\Operation;
use MongoDB\UpdateResult;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\UpdateResult;
use function is_array;
use function is_object;
use function MongoDB\is_first_key_operator;
/**
* Operation for updating multiple documents with the update command.
......@@ -74,11 +77,11 @@ class UpdateMany implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
{
if ( ! is_array($update) && ! is_object($update)) {
if (! is_array($update) && ! is_object($update)) {
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
}
if ( ! \MongoDB\is_first_key_operator($update)) {
if (! is_first_key_operator($update)) {
throw new InvalidArgumentException('First key in $update argument is not an update operator');
}
......
......@@ -17,11 +17,14 @@
namespace MongoDB\Operation;
use MongoDB\UpdateResult;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\UpdateResult;
use function is_array;
use function is_object;
use function MongoDB\is_first_key_operator;
/**
* Operation for updating a single document with the update command.
......@@ -74,11 +77,11 @@ class UpdateOne implements Executable, Explainable
*/
public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
{
if ( ! is_array($update) && ! is_object($update)) {
if (! is_array($update) && ! is_object($update)) {
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
}
if ( ! \MongoDB\is_first_key_operator($update)) {
if (! is_first_key_operator($update)) {
throw new InvalidArgumentException('First key in $update argument is not an update operator');
}
......
......@@ -17,24 +17,30 @@
namespace MongoDB\Operation;
use MongoDB\ChangeStream;
use MongoDB\BSON\TimestampInterface;
use MongoDB\Model\ChangeStreamIterator;
use MongoDB\Driver\Command;
use MongoDB\ChangeStream;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\Session;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Model\ChangeStreamIterator;
use function array_intersect_key;
use function array_unshift;
use function count;
use function is_array;
use function is_object;
use function is_string;
use function MongoDB\Driver\Monitoring\addSubscriber;
use function MongoDB\Driver\Monitoring\removeSubscriber;
use function MongoDB\server_supports_feature;
/**
* Operation for creating a change stream with the aggregate command.
......@@ -131,11 +137,11 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
* for the collection name. A cluster-level change stream may be created by
* specifying null for both the database and collection name.
*
* @param Manager $manager Manager instance from the driver
* @param string|null $databaseName Database name
* @param string|null $collectionName Collection name
* @param array $pipeline List of pipeline operations
* @param array $options Command options
* @param Manager $manager Manager instance from the driver
* @param string|null $databaseName Database name
* @param string|null $collectionName Collection name
* @param array $pipeline List of pipeline operations
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(Manager $manager, $databaseName, $collectionName, array $pipeline, array $options = [])
......@@ -170,7 +176,7 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
* ("implicit from the user's perspective" per PHPLIB-342). Since this
* is filling in for an implicit session, we default "causalConsistency"
* to false. */
if ( ! isset($options['session'])) {
if (! isset($options['session'])) {
try {
$options['session'] = $manager->startSession(['causalConsistency' => false]);
} catch (RuntimeException $e) {
......@@ -221,7 +227,7 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
$reply = $event->getReply();
if ( ! isset($reply->cursor->firstBatch) || ! is_array($reply->cursor->firstBatch)) {
if (! isset($reply->cursor->firstBatch) || ! is_array($reply->cursor->firstBatch)) {
throw new UnexpectedValueException('aggregate command did not return a "cursor.firstBatch" array');
}
......@@ -250,7 +256,9 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
{
return new ChangeStream(
$this->createChangeStreamIterator($server),
function($resumeToken, $hasAdvanced) { return $this->resume($resumeToken, $hasAdvanced); }
function ($resumeToken, $hasAdvanced) {
return $this->resume($resumeToken, $hasAdvanced);
}
);
}
......@@ -296,12 +304,12 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
*/
private function executeAggregate(Server $server)
{
\MongoDB\Driver\Monitoring\addSubscriber($this);
addSubscriber($this);
try {
return $this->aggregate->execute($server);
} finally {
\MongoDB\Driver\Monitoring\removeSubscriber($this);
removeSubscriber($this);
}
}
......@@ -348,7 +356,7 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
// Select a new server using the original read preference
$server = $this->manager->selectServer($this->aggregateOptions['readPreference']);
$resumeOption = isset($this->changeStreamOptions['startAfter']) && !$hasAdvanced ? 'startAfter' : 'resumeAfter';
$resumeOption = isset($this->changeStreamOptions['startAfter']) && ! $hasAdvanced ? 'startAfter' : 'resumeAfter';
unset($this->changeStreamOptions['resumeAfter']);
unset($this->changeStreamOptions['startAfter']);
......@@ -395,7 +403,7 @@ class Watch implements Executable, /* @internal */ CommandSubscriber
return false;
}
if ( ! \MongoDB\server_supports_feature($server, self::$wireVersionForStartAtOperationTime)) {
if (! server_supports_feature($server, self::$wireVersionForStartAtOperationTime)) {
return false;
}
......
......@@ -29,8 +29,6 @@ class UpdateResult
private $isAcknowledged;
/**
* Constructor.
*
* @param WriteResult $writeResult
*/
public function __construct(WriteResult $writeResult)
......
......@@ -18,13 +18,22 @@
namespace MongoDB;
use MongoDB\BSON\Serializable;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Session;
use MongoDB\Exception\InvalidArgumentException;
use ReflectionException;
use stdClass;
use ReflectionClass;
use ReflectionException;
use function end;
use function get_object_vars;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function key;
use function MongoDB\BSON\fromPHP;
use function MongoDB\BSON\toPHP;
use function reset;
use function substr;
/**
* Applies a type map to a document.
......@@ -41,11 +50,11 @@ use ReflectionClass;
*/
function apply_type_map_to_document($document, array $typeMap)
{
if ( ! is_array($document) && ! is_object($document)) {
if (! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
return \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($document), $typeMap);
return toPHP(fromPHP($document), $typeMap);
}
/**
......@@ -67,7 +76,7 @@ function generate_index_name($document)
$document = get_object_vars($document);
}
if ( ! is_array($document)) {
if (! is_array($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
......@@ -100,14 +109,14 @@ function is_first_key_operator($document)
$document = get_object_vars($document);
}
if ( ! is_array($document)) {
if (! is_array($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
reset($document);
$firstKey = (string) key($document);
return (isset($firstKey[0]) && $firstKey[0] === '$');
return isset($firstKey[0]) && $firstKey[0] === '$';
}
/**
......@@ -119,7 +128,7 @@ function is_first_key_operator($document)
*/
function is_in_transaction(array $options)
{
if (isset($options['session']) && $options['session'] instanceof \MongoDB\Driver\Session && $options['session']->isInTransaction()) {
if (isset($options['session']) && $options['session'] instanceof Session && $options['session']->isInTransaction()) {
return true;
}
......@@ -162,7 +171,7 @@ function is_last_pipeline_operator_write(array $pipeline)
*/
function is_mapreduce_output_inline($out)
{
if ( ! is_array($out) && ! is_object($out)) {
if (! is_array($out) && ! is_object($out)) {
return false;
}
......@@ -174,7 +183,7 @@ function is_mapreduce_output_inline($out)
$out = get_object_vars($out);
}
if ( ! is_array($out)) {
if (! is_array($out)) {
throw InvalidArgumentException::invalidType('$out', $out, 'array or object');
}
......@@ -197,18 +206,20 @@ function server_supports_feature(Server $server, $feature)
$maxWireVersion = isset($info['maxWireVersion']) ? (integer) $info['maxWireVersion'] : 0;
$minWireVersion = isset($info['minWireVersion']) ? (integer) $info['minWireVersion'] : 0;
return ($minWireVersion <= $feature && $maxWireVersion >= $feature);
return $minWireVersion <= $feature && $maxWireVersion >= $feature;
}
function is_string_array($input) {
if (!is_array($input)){
function is_string_array($input)
{
if (! is_array($input)) {
return false;
}
foreach($input as $item) {
if (!is_string($item)) {
foreach ($input as $item) {
if (! is_string($item)) {
return false;
}
}
return true;
}
......@@ -223,19 +234,21 @@ function is_string_array($input) {
* @return mixed
* @throws ReflectionException
*/
function recursive_copy($element) {
function recursive_copy($element)
{
if (is_array($element)) {
foreach ($element as $key => $value) {
$element[$key] = recursive_copy($value);
}
return $element;
}
if ( ! is_object($element)) {
if (! is_object($element)) {
return $element;
}
if ( ! (new ReflectionClass($element))->isCloneable()) {
if (! (new ReflectionClass($element))->isCloneable()) {
return $element;
}
......
......@@ -4,9 +4,15 @@ namespace MongoDB\Tests;
use MongoDB\Client;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Command;
use MongoDB\Driver\Manager;
use MongoDB\Driver\Session;
use MongoDB\Model\DatabaseInfo;
use MongoDB\Model\DatabaseInfoIterator;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function call_user_func;
use function is_callable;
use function sprintf;
use function version_compare;
/**
* Functional tests for the Client class.
......@@ -27,7 +33,7 @@ class ClientFunctionalTest extends FunctionalTestCase
public function testGetManager()
{
$this->assertInstanceOf(\MongoDB\Driver\Manager::class, $this->client->getManager());
$this->assertInstanceOf(Manager::class, $this->client->getManager());
}
public function testDropDatabase()
......@@ -53,14 +59,14 @@ class ClientFunctionalTest extends FunctionalTestCase
$databases = $this->client->listDatabases();
$this->assertInstanceOf(\MongoDB\Model\DatabaseInfoIterator::class, $databases);
$this->assertInstanceOf(DatabaseInfoIterator::class, $databases);
foreach ($databases as $database) {
$this->assertInstanceOf(\MongoDB\Model\DatabaseInfo::class, $database);
$this->assertInstanceOf(DatabaseInfo::class, $database);
}
$that = $this;
$this->assertDatabaseExists($this->getDatabaseName(), function(DatabaseInfo $info) use ($that) {
$this->assertDatabaseExists($this->getDatabaseName(), function (DatabaseInfo $info) use ($that) {
$that->assertFalse($info->isEmpty());
$that->assertGreaterThan(0, $info->getSizeOnDisk());
});
......@@ -74,7 +80,7 @@ class ClientFunctionalTest extends FunctionalTestCase
* the given name is found, it will be passed to the callback, which may
* perform additional assertions.
*
* @param string $databaseName
* @param string $databaseName
* @param callable $callback
*/
private function assertDatabaseExists($databaseName, $callback = null)
......@@ -106,6 +112,6 @@ class ClientFunctionalTest extends FunctionalTestCase
if (version_compare($this->getFeatureCompatibilityVersion(), '3.6', '<')) {
$this->markTestSkipped('startSession() is only supported on FCV 3.6 or higher');
}
$this->assertInstanceOf(\MongoDB\Driver\Session::class, $this->client->startSession());
$this->assertInstanceOf(Session::class, $this->client->startSession());
}
}
......@@ -63,13 +63,13 @@ class ClientTest extends TestCase
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$debug = $collection->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -86,13 +86,13 @@ class ClientTest extends TestCase
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -105,7 +105,7 @@ class ClientTest extends TestCase
$debug = $database->__debugInfo();
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -125,13 +125,13 @@ class ClientTest extends TestCase
$database = $client->selectDatabase($this->getDatabaseName());
$debug = $database->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -148,13 +148,13 @@ class ClientTest extends TestCase
$database = $client->selectDatabase($this->getDatabaseName(), $databaseOptions);
$debug = $database->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
}
This diff is collapsed.
......@@ -3,7 +3,6 @@
namespace MongoDB\Tests\Collection;
use MongoDB\Collection;
use MongoDB\Driver\WriteConcern;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
......
......@@ -2,11 +2,14 @@
namespace MongoDB\Tests;
use Exception;
use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber;
use Exception;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
use function call_user_func;
use function MongoDB\Driver\Monitoring\addSubscriber;
use function MongoDB\Driver\Monitoring\removeSubscriber;
/**
* Observes command documents using the driver's monitoring API.
......@@ -19,13 +22,14 @@ class CommandObserver implements CommandSubscriber
{
$this->commands = [];
\MongoDB\Driver\Monitoring\addSubscriber($this);
addSubscriber($this);
try {
call_user_func($execution);
} catch (Exception $executionException) {}
} catch (Exception $executionException) {
}
\MongoDB\Driver\Monitoring\removeSubscriber($this);
removeSubscriber($this);
foreach ($this->commands as $command) {
call_user_func($commandCallback, $command);
......
......@@ -2,9 +2,13 @@
namespace MongoDB\Tests\Database;
use InvalidArgumentException;
use MongoDB\Driver\BulkWrite;
use MongoDB\Model\CollectionInfo;
use InvalidArgumentException;
use MongoDB\Model\CollectionInfoIterator;
use function call_user_func;
use function is_callable;
use function sprintf;
/**
* Functional tests for collection management methods.
......@@ -18,7 +22,7 @@ class CollectionManagementFunctionalTest extends FunctionalTestCase
$commandResult = $this->database->createCollection($basicCollectionName);
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionExists($basicCollectionName, function(CollectionInfo $info) use ($that) {
$this->assertCollectionExists($basicCollectionName, function (CollectionInfo $info) use ($that) {
$that->assertFalse($info->isCapped());
});
......@@ -31,7 +35,7 @@ class CollectionManagementFunctionalTest extends FunctionalTestCase
$commandResult = $this->database->createCollection($cappedCollectionName, $cappedCollectionOptions);
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionExists($cappedCollectionName, function(CollectionInfo $info) use ($that) {
$this->assertCollectionExists($cappedCollectionName, function (CollectionInfo $info) use ($that) {
$that->assertTrue($info->isCapped());
$that->assertEquals(100, $info->getCappedMax());
$that->assertEquals(1048576, $info->getCappedSize());
......@@ -57,10 +61,10 @@ class CollectionManagementFunctionalTest extends FunctionalTestCase
$this->assertCommandSucceeded($commandResult);
$collections = $this->database->listCollections();
$this->assertInstanceOf(\MongoDB\Model\CollectionInfoIterator::class, $collections);
$this->assertInstanceOf(CollectionInfoIterator::class, $collections);
foreach ($collections as $collection) {
$this->assertInstanceOf(\MongoDB\Model\CollectionInfo::class, $collection);
$this->assertInstanceOf(CollectionInfo::class, $collection);
}
}
......@@ -73,10 +77,10 @@ class CollectionManagementFunctionalTest extends FunctionalTestCase
$options = ['filter' => ['name' => $collectionName]];
$collections = $this->database->listCollections($options);
$this->assertInstanceOf(\MongoDB\Model\CollectionInfoIterator::class, $collections);
$this->assertInstanceOf(CollectionInfoIterator::class, $collections);
foreach ($collections as $collection) {
$this->assertInstanceOf(\MongoDB\Model\CollectionInfo::class, $collection);
$this->assertInstanceOf(CollectionInfo::class, $collection);
$this->assertEquals($collectionName, $collection->getName());
}
}
......
......@@ -4,11 +4,14 @@ namespace MongoDB\Tests\Database;
use MongoDB\Database;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Operation\CreateIndexes;
use function array_key_exists;
use function current;
/**
* Functional tests for the Database class.
......@@ -89,7 +92,7 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$cursor = $this->database->command($command, $options);
$this->assertInstanceOf(\MongoDB\Driver\Cursor::class, $cursor);
$this->assertInstanceOf(Cursor::class, $cursor);
$commandResult = current($cursor->toArray());
$this->assertCommandSucceeded($commandResult);
......@@ -107,7 +110,7 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$cursor = $this->database->command($command, $options);
$this->assertInstanceOf(\MongoDB\Driver\Cursor::class, $cursor);
$this->assertInstanceOf(Cursor::class, $cursor);
$commandResult = current($cursor->toArray());
$this->assertCommandSucceeded($commandResult);
......@@ -149,7 +152,7 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame($this->manager, $debug['manager']);
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame($this->getCollectionName(), $debug['collectionName']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -186,7 +189,6 @@ class DatabaseFunctionalTest extends FunctionalTestCase
}
}
public function testSelectCollectionInheritsOptions()
{
$databaseOptions = [
......@@ -203,13 +205,13 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame($this->manager, $debug['manager']);
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame($this->getCollectionName(), $debug['collectionName']);
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -225,13 +227,13 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
$debug = $collection->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -251,11 +253,11 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame('fs', $debug['bucketName']);
$this->assertSame(261120, $debug['chunkSizeBytes']);
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -276,11 +278,11 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame('custom_fs', $debug['bucketName']);
$this->assertSame(8192, $debug['chunkSizeBytes']);
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -299,13 +301,13 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$this->assertSame($this->manager, $debug['manager']);
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
......@@ -321,13 +323,13 @@ class DatabaseFunctionalTest extends FunctionalTestCase
$clone = $this->database->withOptions($databaseOptions);
$debug = $clone->__debugInfo();
$this->assertInstanceOf(\MongoDB\Driver\ReadConcern::class, $debug['readConcern']);
$this->assertInstanceOf(ReadConcern::class, $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf(\MongoDB\Driver\ReadPreference::class, $debug['readPreference']);
$this->assertInstanceOf(ReadPreference::class, $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertIsArray($debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf(\MongoDB\Driver\WriteConcern::class, $debug['writeConcern']);
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
}
......@@ -2,21 +2,33 @@
namespace MongoDB\Tests;
use InvalidArgumentException;
use MongoDB\BSON\ObjectId;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\CommandException;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Query;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\Exception\CommandException;
use MongoDB\Operation\CreateCollection;
use MongoDB\Operation\DatabaseCommand;
use MongoDB\Operation\DropCollection;
use InvalidArgumentException;
use stdClass;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use UnexpectedValueException;
use function array_merge;
use function count;
use function current;
use function explode;
use function implode;
use function is_array;
use function is_object;
use function is_string;
use function key;
use function parse_url;
use function preg_match;
use function version_compare;
abstract class FunctionalTestCase extends TestCase
{
......@@ -75,9 +87,7 @@ abstract class FunctionalTestCase extends TestCase
$hosts[$numHosts-1] .= ':' . $urlParts['port'];
}
$parts = [
'mongodb://'
];
$parts = ['mongodb://'];
if (isset($urlParts['user'], $urlParts['pass'])) {
$parts += [
......@@ -96,7 +106,7 @@ abstract class FunctionalTestCase extends TestCase
if (isset($urlParts['query'])) {
$parts = array_merge($parts, [
'?',
$urlParts['query']
$urlParts['query'],
]);
}
......@@ -125,8 +135,8 @@ abstract class FunctionalTestCase extends TestCase
protected function assertSameObjectId($expectedObjectId, $actualObjectId)
{
$this->assertInstanceOf(\MongoDB\BSON\ObjectId::class, $expectedObjectId);
$this->assertInstanceOf(\MongoDB\BSON\ObjectId::class, $actualObjectId);
$this->assertInstanceOf(ObjectId::class, $expectedObjectId);
$this->assertInstanceOf(ObjectId::class, $actualObjectId);
$this->assertEquals((string) $expectedObjectId, (string) $actualObjectId);
}
......@@ -153,7 +163,7 @@ abstract class FunctionalTestCase extends TestCase
$command = (object) $command;
}
if ( ! $command instanceof stdClass) {
if (! $command instanceof stdClass) {
throw new InvalidArgumentException('$command is not an array or stdClass instance');
}
......@@ -300,7 +310,7 @@ abstract class FunctionalTestCase extends TestCase
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());
if (! $document ) {
if (! $document) {
return false;
}
......@@ -314,13 +324,12 @@ abstract class FunctionalTestCase extends TestCase
protected function skipIfChangeStreamIsNotSupported()
{
switch ( $this->getPrimaryServer()->getType() )
{
switch ($this->getPrimaryServer()->getType()) {
case Server::TYPE_MONGOS:
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('$changeStream is only supported on MongoDB 3.6 or higher');
}
if (!$this->isShardedClusterUsingReplicasets()) {
if (! $this->isShardedClusterUsingReplicasets()) {
$this->markTestSkipped('$changeStream is only supported with replicasets');
}
......@@ -341,13 +350,12 @@ abstract class FunctionalTestCase extends TestCase
protected function skipIfCausalConsistencyIsNotSupported()
{
switch ( $this->getPrimaryServer()->getType() )
{
switch ($this->getPrimaryServer()->getType()) {
case Server::TYPE_MONGOS:
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
$this->markTestSkipped('Causal Consistency is only supported on MongoDB 3.6 or higher');
}
if (!$this->isShardedClusterUsingReplicasets()) {
if (! $this->isShardedClusterUsingReplicasets()) {
$this->markTestSkipped('Causal Consistency is only supported with replicasets');
}
break;
......
......@@ -2,11 +2,14 @@
namespace MongoDB\Tests;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use function MongoDB\apply_type_map_to_document;
use function MongoDB\create_field_path_type_map;
use function MongoDB\generate_index_name;
use function MongoDB\is_first_key_operator;
use function MongoDB\is_mapreduce_output_inline;
/**
* Unit tests for utility functions.
......@@ -18,7 +21,7 @@ class FunctionsTest extends TestCase
*/
public function testApplyTypeMapToDocument($document, array $typeMap, $expectedDocument)
{
$this->assertEquals($expectedDocument, \MongoDB\apply_type_map_to_document($document, $typeMap));
$this->assertEquals($expectedDocument, apply_type_map_to_document($document, $typeMap));
}
public function provideDocumentAndTypeMap()
......@@ -61,37 +64,27 @@ class FunctionsTest extends TestCase
[
[
'x' => 1,
'random' => [
'foo' => 'bar',
],
'random' => ['foo' => 'bar'],
'value' => [
'bar' => 'baz',
'embedded' => [
'foo' => 'bar',
],
'embedded' => ['foo' => 'bar'],
],
],
[
'root' => 'array',
'document' => 'stdClass',
'array' => 'array',
'fieldPaths' => [
'value' => 'array',
],
'fieldPaths' => ['value' => 'array'],
],
[
'x' => 1,
'random' => (object) [
'foo' => 'bar',
],
'random' => (object) ['foo' => 'bar'],
'value' => [
'bar' => 'baz',
'embedded' => (object) [
'foo' => 'bar',
],
'embedded' => (object) ['foo' => 'bar'],
],
],
]
],
];
}
......@@ -100,7 +93,7 @@ class FunctionsTest extends TestCase
*/
public function testGenerateIndexName($document, $expectedName)
{
$this->assertSame($expectedName, \MongoDB\generate_index_name($document));
$this->assertSame($expectedName, generate_index_name($document));
}
public function provideIndexSpecificationDocumentsAndGeneratedNames()
......@@ -120,7 +113,7 @@ class FunctionsTest extends TestCase
public function testGenerateIndexNameArgumentTypeCheck($document)
{
$this->expectException(InvalidArgumentException::class);
\MongoDB\generate_index_name($document);
generate_index_name($document);
}
/**
......@@ -128,7 +121,7 @@ class FunctionsTest extends TestCase
*/
public function testIsFirstKeyOperator($document, $isFirstKeyOperator)
{
$this->assertSame($isFirstKeyOperator, \MongoDB\is_first_key_operator($document));
$this->assertSame($isFirstKeyOperator, is_first_key_operator($document));
}
public function provideIsFirstKeyOperatorDocuments()
......@@ -149,7 +142,7 @@ class FunctionsTest extends TestCase
public function testIsFirstKeyOperatorArgumentTypeCheck($document)
{
$this->expectException(InvalidArgumentException::class);
\MongoDB\is_first_key_operator($document);
is_first_key_operator($document);
}
/**
......@@ -157,7 +150,7 @@ class FunctionsTest extends TestCase
*/
public function testIsMapReduceOutputInline($out, $isInline)
{
$this->assertSame($isInline, \MongoDB\is_mapreduce_output_inline($out));
$this->assertSame($isInline, is_mapreduce_output_inline($out));
}
public function provideMapReduceOutValues()
......@@ -175,7 +168,7 @@ class FunctionsTest extends TestCase
*/
public function testCreateFieldPathTypeMap(array $expected, array $typeMap, $fieldPath = 'field')
{
$this->assertEquals($expected, \MongoDB\create_field_path_type_map($typeMap, $fieldPath));
$this->assertEquals($expected, create_field_path_type_map($typeMap, $fieldPath));
}
public function provideTypeMapValues()
......@@ -205,14 +198,12 @@ class FunctionsTest extends TestCase
'field' => 'array',
'field.$' => 'object',
'field.$.nested' => 'array',
]
],
],
[
'root' => 'object',
'array' => 'MongoDB\Model\BSONArray',
'fieldPaths' => [
'nested' => 'array',
]
'fieldPaths' => ['nested' => 'array'],
],
'field.$',
],
......@@ -223,13 +214,11 @@ class FunctionsTest extends TestCase
'fieldPaths' => [
'field' => 'array',
'field.$.nested' => 'array',
]
],
],
[
'array' => 'MongoDB\Model\BSONArray',
'fieldPaths' => [
'nested' => 'array',
]
'fieldPaths' => ['nested' => 'array'],
],
'field.$',
],
......
......@@ -3,16 +3,32 @@
namespace MongoDB\Tests\GridFS;
use MongoDB\BSON\Binary;
use MongoDB\Collection;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\Bucket;
use MongoDB\GridFS\Exception\FileNotFoundException;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\IndexInfo;
use MongoDB\Operation\ListCollections;
use MongoDB\Operation\ListIndexes;
use PHPUnit\Framework\Error\Warning;
use function array_merge;
use function call_user_func;
use function current;
use function fclose;
use function fread;
use function fwrite;
use function hash_init;
use function is_callable;
use function min;
use function sprintf;
use function str_repeat;
use function stream_get_contents;
use function strlen;
use function substr;
/**
* Functional tests for the Bucket class.
......@@ -137,7 +153,8 @@ class BucketFunctionalTest extends FunctionalTestCase
try {
$this->bucket->delete($id);
$this->fail('FileNotFoundException was not thrown');
} catch (FileNotFoundException $e) {}
} catch (FileNotFoundException $e) {
}
$this->assertCollectionCount($this->chunksCollection, 0);
}
......@@ -323,7 +340,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$cursor = $this->bucket->find();
$fileDocument = current($cursor->toArray());
$this->assertInstanceOf(\MongoDB\Model\BSONDocument::class, $fileDocument);
$this->assertInstanceOf(BSONDocument::class, $fileDocument);
}
public function testFindOne()
......@@ -344,7 +361,7 @@ class BucketFunctionalTest extends FunctionalTestCase
]
);
$this->assertInstanceOf(\MongoDB\Model\BSONDocument::class, $fileDocument);
$this->assertInstanceOf(BSONDocument::class, $fileDocument);
$this->assertSameDocument(['filename' => 'b', 'length' => 6], $fileDocument);
}
......@@ -364,7 +381,7 @@ class BucketFunctionalTest extends FunctionalTestCase
{
$chunksCollection = $this->bucket->getChunksCollection();
$this->assertInstanceOf(\MongoDB\Collection::class, $chunksCollection);
$this->assertInstanceOf(Collection::class, $chunksCollection);
$this->assertEquals('fs.chunks', $chunksCollection->getCollectionName());
}
......@@ -392,8 +409,8 @@ class BucketFunctionalTest extends FunctionalTestCase
$fileDocument = $this->bucket->getFileDocumentForStream($stream);
$this->assertInstanceOf(\MongoDB\Model\BSONDocument::class, $fileDocument);
$this->assertInstanceOf(\MongoDB\Model\BSONDocument::class, $fileDocument['metadata']);
$this->assertInstanceOf(BSONDocument::class, $fileDocument);
$this->assertInstanceOf(BSONDocument::class, $fileDocument['metadata']);
$this->assertSame(['foo' => 'bar'], $fileDocument['metadata']->getArrayCopy());
}
......@@ -443,7 +460,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$id = $this->bucket->getFileIdForStream($stream);
$this->assertInstanceOf(\MongoDB\Model\BSONDocument::class, $id);
$this->assertInstanceOf(BSONDocument::class, $id);
$this->assertSame(['x' => 1], $id->getArrayCopy());
}
......@@ -475,7 +492,7 @@ class BucketFunctionalTest extends FunctionalTestCase
{
$filesCollection = $this->bucket->getFilesCollection();
$this->assertInstanceOf(\MongoDB\Collection::class, $filesCollection);
$this->assertInstanceOf(Collection::class, $filesCollection);
$this->assertEquals('fs.files', $filesCollection->getCollectionName());
}
......@@ -675,7 +692,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$this->bucket->uploadFromStream('filename', $this->createStream('foo'));
$this->assertIndexExists($this->filesCollection->getCollectionName(), 'filename_1_uploadDate_1');
$this->assertIndexExists($this->chunksCollection->getCollectionName(), 'files_id_1_n_1', function(IndexInfo $info) {
$this->assertIndexExists($this->chunksCollection->getCollectionName(), 'files_id_1_n_1', function (IndexInfo $info) {
$this->assertTrue($info->isUnique());
});
}
......
......@@ -6,6 +6,11 @@ use MongoDB\Collection;
use MongoDB\GridFS\Bucket;
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function fopen;
use function fwrite;
use function get_resource_type;
use function rewind;
use function stream_get_contents;
/**
* Base class for GridFS functional tests.
......
......@@ -5,11 +5,11 @@ namespace MongoDB\Tests\GridFS;
use MongoDB\BSON\Binary;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\CollectionWrapper;
use MongoDB\GridFS\ReadableStream;
use MongoDB\GridFS\Exception\CorruptFileException;
use MongoDB\GridFS\ReadableStream;
use MongoDB\Tests\CommandObserver;
use stdClass;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function array_filter;
/**
* Functional tests for the internal ReadableStream class.
......@@ -119,8 +119,9 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
public function provideFilteredFileIdAndExpectedBytes()
{
return array_filter($this->provideFileIdAndExpectedBytes(),
function(array $args) {
return array_filter(
$this->provideFileIdAndExpectedBytes(),
function (array $args) {
return $args[1] > 0;
}
);
......@@ -219,12 +220,12 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
$commands = [];
(new CommandObserver)->observe(
function() use ($stream, $offset, $length, $expectedBytes) {
(new CommandObserver())->observe(
function () use ($stream, $offset, $length, $expectedBytes) {
$stream->seek($offset);
$this->assertSame($expectedBytes, $stream->readBytes($length));
},
function(array $event) use (&$commands) {
function (array $event) use (&$commands) {
$commands[] = $event['started']->getCommandName();
}
);
......@@ -255,12 +256,12 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
$commands = [];
(new CommandObserver)->observe(
function() use ($stream, $offset, $length, $expectedBytes) {
(new CommandObserver())->observe(
function () use ($stream, $offset, $length, $expectedBytes) {
$stream->seek($offset);
$this->assertSame($expectedBytes, $stream->readBytes($length));
},
function(array $event) use (&$commands) {
function (array $event) use (&$commands) {
$commands[] = $event['started']->getCommandName();
}
);
......@@ -289,12 +290,12 @@ class ReadableStreamFunctionalTest extends FunctionalTestCase
$commands = [];
(new CommandObserver)->observe(
function() use ($stream, $offset, $length, $expectedBytes) {
(new CommandObserver())->observe(
function () use ($stream, $offset, $length, $expectedBytes) {
$stream->seek($offset);
$this->assertSame($expectedBytes, $stream->readBytes($length));
},
function(array $event) use (&$commands) {
function (array $event) use (&$commands) {
$commands[] = $event['started']->getCommandName();
}
);
......
This diff is collapsed.
......@@ -5,6 +5,15 @@ namespace MongoDB\Tests\GridFS;
use MongoDB\BSON\Binary;
use MongoDB\BSON\UTCDateTime;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function fclose;
use function feof;
use function fread;
use function fseek;
use function fstat;
use function fwrite;
use const SEEK_CUR;
use const SEEK_END;
use const SEEK_SET;
/**
* Functional tests for the internal StreamWrapper class.
......@@ -57,26 +66,26 @@ class StreamWrapperFunctionalTest extends FunctionalTestCase
{
$stream = $this->bucket->openDownloadStream('length-10');
$this->assertSame(0, fseek($stream, 2, \SEEK_SET));
$this->assertSame(0, fseek($stream, 2, SEEK_SET));
$this->assertSame('cde', fread($stream, 3));
$this->assertSame(0, fseek($stream, 10, \SEEK_SET));
$this->assertSame(0, fseek($stream, 10, SEEK_SET));
$this->assertSame('', fread($stream, 3));
$this->assertSame(-1, fseek($stream, -1, \SEEK_SET));
$this->assertSame(-1, fseek($stream, 11, \SEEK_SET));
$this->assertSame(-1, fseek($stream, -1, SEEK_SET));
$this->assertSame(-1, fseek($stream, 11, SEEK_SET));
$this->assertSame(0, fseek($stream, -5, \SEEK_CUR));
$this->assertSame(0, fseek($stream, -5, SEEK_CUR));
$this->assertSame('fgh', fread($stream, 3));
$this->assertSame(0, fseek($stream, 1, \SEEK_CUR));
$this->assertSame(0, fseek($stream, 1, SEEK_CUR));
$this->assertSame('j', fread($stream, 3));
$this->assertSame(-1, fseek($stream, 1, \SEEK_CUR));
$this->assertSame(-1, fseek($stream, -11, \SEEK_CUR));
$this->assertSame(-1, fseek($stream, 1, SEEK_CUR));
$this->assertSame(-1, fseek($stream, -11, SEEK_CUR));
$this->assertSame(0, fseek($stream, 0, \SEEK_END));
$this->assertSame(0, fseek($stream, 0, SEEK_END));
$this->assertSame('', fread($stream, 3));
$this->assertSame(0, fseek($stream, -8, \SEEK_END));
$this->assertSame(0, fseek($stream, -8, SEEK_END));
$this->assertSame('cde', fread($stream, 3));
$this->assertSame(-1, fseek($stream, -11, \SEEK_END));
$this->assertSame(-1, fseek($stream, 1, \SEEK_END));
$this->assertSame(-1, fseek($stream, -11, SEEK_END));
$this->assertSame(-1, fseek($stream, 1, SEEK_END));
}
public function testReadableStreamStat()
......@@ -137,17 +146,17 @@ class StreamWrapperFunctionalTest extends FunctionalTestCase
$this->assertSame(6, fwrite($stream, 'foobar'));
$this->assertSame(-1, fseek($stream, 0, \SEEK_SET));
$this->assertSame(-1, fseek($stream, 7, \SEEK_SET));
$this->assertSame(0, fseek($stream, 6, \SEEK_SET));
$this->assertSame(-1, fseek($stream, 0, SEEK_SET));
$this->assertSame(-1, fseek($stream, 7, SEEK_SET));
$this->assertSame(0, fseek($stream, 6, SEEK_SET));
$this->assertSame(0, fseek($stream, 0, \SEEK_CUR));
$this->assertSame(-1, fseek($stream, -1, \SEEK_CUR));
$this->assertSame(-1, fseek($stream, 1, \SEEK_CUR));
$this->assertSame(0, fseek($stream, 0, SEEK_CUR));
$this->assertSame(-1, fseek($stream, -1, SEEK_CUR));
$this->assertSame(-1, fseek($stream, 1, SEEK_CUR));
$this->assertSame(0, fseek($stream, 0, \SEEK_END));
$this->assertSame(-1, fseek($stream, -1, \SEEK_END));
$this->assertSame(-1, fseek($stream, 1, \SEEK_END));
$this->assertSame(0, fseek($stream, 0, SEEK_END));
$this->assertSame(-1, fseek($stream, -1, SEEK_END));
$this->assertSame(-1, fseek($stream, 1, SEEK_END));
}
public function testWritableStreamStatBeforeSaving()
......@@ -171,7 +180,7 @@ class StreamWrapperFunctionalTest extends FunctionalTestCase
$stat = fstat($stream);
$this->assertSame(6, $stat[7]);
$this->assertSame(6, $stat['size']);
}
}
public function testWritableStreamStatAfterSaving()
{
......
......@@ -6,6 +6,7 @@ use MongoDB\Exception\InvalidArgumentException;
use MongoDB\GridFS\CollectionWrapper;
use MongoDB\GridFS\WritableStream;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function str_repeat;
/**
* Functional tests for the internal WritableStream class.
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -2,9 +2,10 @@
namespace MongoDB\Tests\Model;
use Exception;
use MongoDB\Model\CachingIterator;
use MongoDB\Tests\TestCase;
use Exception;
use function iterator_to_array;
class CachingIteratorTest extends TestCase
{
......@@ -52,7 +53,7 @@ class CachingIteratorTest extends TestCase
public function testPartialIterationDoesNotExhaust()
{
$traversable = $this->getTraversableThatThrows([1, 2, new Exception]);
$traversable = $this->getTraversableThatThrows([1, 2, new Exception()]);
$iterator = new CachingIterator($traversable);
$expectedKey = 0;
......
This diff is collapsed.
......@@ -64,7 +64,7 @@ class CollectionInfoTest extends TestCase
{
$info = new CollectionInfo(['name' => 'foo', 'options' => ['capped' => true, 'size' => 1048576]]);
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage(CollectionInfo::class .' is immutable');
$this->expectExceptionMessage(CollectionInfo::class . ' is immutable');
$info['options'] = ['capped' => false];
}
......@@ -72,7 +72,7 @@ class CollectionInfoTest extends TestCase
{
$info = new CollectionInfo(['name' => 'foo', 'options' => ['capped' => true, 'size' => 1048576]]);
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage(CollectionInfo::class .' is immutable');
$this->expectExceptionMessage(CollectionInfo::class . ' is immutable');
unset($info['options']);
}
}
......@@ -53,7 +53,7 @@ class DatabaseInfoTest extends TestCase
{
$info = new DatabaseInfo(['name' => 'foo', 'sizeOnDisk' => 1048576, 'empty' => false]);
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage(DatabaseInfo::class .' is immutable');
$this->expectExceptionMessage(DatabaseInfo::class . ' is immutable');
$info['empty'] = true;
}
......@@ -61,7 +61,7 @@ class DatabaseInfoTest extends TestCase
{
$info = new DatabaseInfo(['name' => 'foo', 'sizeOnDisk' => 1048576, 'empty' => false]);
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage(DatabaseInfo::class .' is immutable');
$this->expectExceptionMessage(DatabaseInfo::class . ' is immutable');
unset($info['empty']);
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment