Skip to content

PHPLIB-122: Checking "ok" field in command results is redundant #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ env:
- MONGO_REPO_TYPE="precise/mongodb-enterprise/"
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
matrix:
- DRIVER_VERSION=1.0 SERVER_VERSION=2.4
- DRIVER_VERSION=1.0 SERVER_VERSION=2.6
- DRIVER_VERSION=1.0 SERVER_VERSION=3.0
- DRIVER_VERSION=1.0.0 SERVER_VERSION=2.4
- DRIVER_VERSION=1.0.0 SERVER_VERSION=2.6
- DRIVER_VERSION=1.0.0 SERVER_VERSION=3.0

before_install:
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv 7F0CEB10
Expand All @@ -32,6 +32,7 @@ before_script:
- if ! nc -z localhost 27017; then sudo service ${SERVER_SERVICE} start; fi
- mongod --version
- pecl install -f mongodb-${DRIVER_VERSION}
- if [ "$(php -v | grep 'PHP 5.4')" ]; then echo 'extension = mongodb.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
- php --ri mongodb
- composer install --dev --no-interaction --prefer-source
- ulimit -c
Expand Down
5 changes: 0 additions & 5 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;
use ArrayIterator;
use stdClass;
Expand Down Expand Up @@ -136,10 +135,6 @@ public function execute(Server $server)

$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

if ( ! isset($result->result) || ! is_array($result->result)) {
throw new UnexpectedValueException('aggregate command did not return a "result" array');
}
Expand Down
5 changes: 0 additions & 5 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;

/**
Expand Down Expand Up @@ -100,10 +99,6 @@ public function execute(Server $server)
$cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

// Older server versions may return a float
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
throw new UnexpectedValueException('count command did not return a numeric "n" value');
Expand Down
8 changes: 1 addition & 7 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Model\IndexInput;

Expand Down Expand Up @@ -102,13 +101,8 @@ public function __construct($databaseName, $collectionName, array $options = [])
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

return $result;
return current($cursor->toArray());
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MongoDB\Driver\BulkWrite as Bulk;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Model\IndexInput;

Expand Down Expand Up @@ -92,11 +91,8 @@ private function executeCommand(Server $server)
]);

$cursor = $server->executeCommand($this->databaseName, $command);
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}
return current($cursor->toArray());
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;

/**
Expand Down Expand Up @@ -77,10 +76,6 @@ public function execute(Server $server)
$cursor = $server->executeCommand($this->databaseName, $this->createCommand(), $readPreference);
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

if ( ! isset($result->values) || ! is_array($result->values)) {
throw new UnexpectedValueException('distinct command did not return a "values" array');
}
Expand Down
15 changes: 4 additions & 11 deletions src/Operation/DropCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Driver\Exception\RuntimeException;

/**
* Operation for the drop command.
Expand Down Expand Up @@ -44,24 +43,18 @@ public function execute(Server $server)
{
try {
$cursor = $server->executeCommand($this->databaseName, new Command(['drop' => $this->collectionName]));
} catch (DriverRuntimeException $e) {
} catch (RuntimeException $e) {
/* The server may return an error if the collection does not exist.
* Check for an error message (unfortunately, there isn't a code)
* and NOP instead of throwing.
*/
if ($e->getMessage() === self::$errorMessageNamespaceNotFound) {
return (object) ['ok' => 0, 'errmsg' => 'ns not found'];
return (object) ['ok' => 0, 'errmsg' => self::$errorMessageNamespaceNotFound];
}

throw $e;
}

$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

return $result;
return current($cursor->toArray());
}
}
8 changes: 1 addition & 7 deletions src/Operation/DropDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\RuntimeException;

/**
* Operation for the dropDatabase command.
Expand Down Expand Up @@ -39,12 +38,7 @@ public function __construct($databaseName)
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, new Command(['dropDatabase' => 1]));
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

return $result;
return current($cursor->toArray());
}
}
8 changes: 1 addition & 7 deletions src/Operation/DropIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\RuntimeException;

/**
* Operation for the dropIndexes command.
Expand Down Expand Up @@ -56,12 +55,7 @@ public function execute(Server $server)
];

$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

return $result;
return current($cursor->toArray());
}
}
5 changes: 0 additions & 5 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\InvalidArgumentTypeException;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;

/**
Expand Down Expand Up @@ -120,10 +119,6 @@ public function execute(Server $server)
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$result = current($cursor->toArray());

if (empty($result->ok)) {
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
}

if ( ! isset($result->value)) {
return null;
}
Expand Down
5 changes: 0 additions & 5 deletions src/Operation/ListDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\RuntimeException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Model\DatabaseInfoLegacyIterator;
Expand Down Expand Up @@ -58,10 +57,6 @@ public function execute(Server $server)
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$result = current($cursor->toArray());

if (empty($result['ok'])) {
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
}

if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
}
Expand Down