Skip to content

Commit 48c8290

Browse files
committed
PHPLIB-122: Checking "ok" field in command results is redundant
The driver (via libmongoc) already does this.
1 parent 9d087e5 commit 48c8290

File tree

10 files changed

+7
-61
lines changed

10 files changed

+7
-61
lines changed

src/Operation/Aggregate.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\Server;
88
use MongoDB\Exception\InvalidArgumentException;
99
use MongoDB\Exception\InvalidArgumentTypeException;
10-
use MongoDB\Exception\RuntimeException;
1110
use MongoDB\Exception\UnexpectedValueException;
1211
use ArrayIterator;
1312
use stdClass;
@@ -136,10 +135,6 @@ public function execute(Server $server)
136135

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

139-
if (empty($result->ok)) {
140-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
141-
}
142-
143138
if ( ! isset($result->result) || ! is_array($result->result)) {
144139
throw new UnexpectedValueException('aggregate command did not return a "result" array');
145140
}

src/Operation/Count.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\Server;
88
use MongoDB\Exception\InvalidArgumentException;
99
use MongoDB\Exception\InvalidArgumentTypeException;
10-
use MongoDB\Exception\RuntimeException;
1110
use MongoDB\Exception\UnexpectedValueException;
1211

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

103-
if (empty($result->ok)) {
104-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
105-
}
106-
107102
// Older server versions may return a float
108103
if ( ! isset($result->n) || ! (is_integer($result->n) || is_float($result->n))) {
109104
throw new UnexpectedValueException('count command did not return a numeric "n" value');

src/Operation/CreateCollection.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
77
use MongoDB\Exception\InvalidArgumentException;
8-
use MongoDB\Exception\RuntimeException;
98
use MongoDB\Exception\UnexpectedTypeException;
109
use MongoDB\Model\IndexInput;
1110

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

107-
if (empty($result->ok)) {
108-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
109-
}
110-
111-
return $result;
105+
return current($cursor->toArray());
112106
}
113107

114108
/**

src/Operation/CreateIndexes.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\BulkWrite as Bulk;
88
use MongoDB\Driver\WriteConcern;
99
use MongoDB\Exception\InvalidArgumentException;
10-
use MongoDB\Exception\RuntimeException;
1110
use MongoDB\Exception\UnexpectedTypeException;
1211
use MongoDB\Model\IndexInput;
1312

@@ -92,11 +91,8 @@ private function executeCommand(Server $server)
9291
]);
9392

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

97-
if (empty($result->ok)) {
98-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
99-
}
95+
return current($cursor->toArray());
10096
}
10197

10298
/**

src/Operation/Distinct.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\Server;
88
use MongoDB\Exception\InvalidArgumentException;
99
use MongoDB\Exception\InvalidArgumentTypeException;
10-
use MongoDB\Exception\RuntimeException;
1110
use MongoDB\Exception\UnexpectedValueException;
1211

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

80-
if (empty($result->ok)) {
81-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
82-
}
83-
8479
if ( ! isset($result->values) || ! is_array($result->values)) {
8580
throw new UnexpectedValueException('distinct command did not return a "values" array');
8681
}

src/Operation/DropCollection.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7-
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
8-
use MongoDB\Exception\RuntimeException;
7+
use MongoDB\Driver\Exception\RuntimeException;
98

109
/**
1110
* Operation for the drop command.
@@ -44,7 +43,7 @@ public function execute(Server $server)
4443
{
4544
try {
4645
$cursor = $server->executeCommand($this->databaseName, new Command(['drop' => $this->collectionName]));
47-
} catch (DriverRuntimeException $e) {
46+
} catch (RuntimeException $e) {
4847
/* The server may return an error if the collection does not exist.
4948
* Check for an error message (unfortunately, there isn't a code)
5049
* and NOP instead of throwing.
@@ -56,12 +55,6 @@ public function execute(Server $server)
5655
throw $e;
5756
}
5857

59-
$result = current($cursor->toArray());
60-
61-
if (empty($result->ok)) {
62-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
63-
}
64-
65-
return $result;
58+
return current($cursor->toArray());
6659
}
6760
}

src/Operation/DropDatabase.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7-
use MongoDB\Exception\RuntimeException;
87

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

44-
if (empty($result->ok)) {
45-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
46-
}
47-
48-
return $result;
42+
return current($cursor->toArray());
4943
}
5044
}

src/Operation/DropIndexes.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
77
use MongoDB\Exception\InvalidArgumentException;
8-
use MongoDB\Exception\RuntimeException;
98

109
/**
1110
* Operation for the dropIndexes command.
@@ -56,12 +55,7 @@ public function execute(Server $server)
5655
];
5756

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

61-
if (empty($result->ok)) {
62-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
63-
}
64-
65-
return $result;
59+
return current($cursor->toArray());
6660
}
6761
}

src/Operation/FindAndModify.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use MongoDB\Driver\Server;
77
use MongoDB\Exception\InvalidArgumentException;
88
use MongoDB\Exception\InvalidArgumentTypeException;
9-
use MongoDB\Exception\RuntimeException;
109
use MongoDB\Exception\UnexpectedValueException;
1110

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

123-
if (empty($result->ok)) {
124-
throw new RuntimeException(isset($result->errmsg) ? $result->errmsg : 'Unknown error');
125-
}
126-
127122
if ( ! isset($result->value)) {
128123
return null;
129124
}

src/Operation/ListDatabases.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Server;
7-
use MongoDB\Exception\RuntimeException;
87
use MongoDB\Exception\UnexpectedValueException;
98
use MongoDB\Model\DatabaseInfoIterator;
109
use MongoDB\Model\DatabaseInfoLegacyIterator;
@@ -58,10 +57,6 @@ public function execute(Server $server)
5857
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
5958
$result = current($cursor->toArray());
6059

61-
if (empty($result['ok'])) {
62-
throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error');
63-
}
64-
6560
if ( ! isset($result['databases']) || ! is_array($result['databases'])) {
6661
throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
6762
}

0 commit comments

Comments
 (0)