Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't set cursor flags unnecessarily and cast arguments

If slaveOkay defaults to false in Doctrine's class and is set each time the cursor is initialized, there is no way to inherit from MongoDB or MongoCollection ancestors. Instead, initialize these fields with empty values and only set them on the cursor if a non-null value is available.

Casting input arguments explicitly will ensure that any method call is sufficient to remove the null default.
  • Loading branch information...
commit 1ef66b4f5f69c133a9ac69f5051f2be9cf151c4d 1 parent ae2c68b
@jmikola jmikola authored
View
37 lib/Doctrine/MongoDB/Cursor.php
@@ -43,15 +43,15 @@ class Cursor implements Iterator
protected $query = array();
protected $fields = array();
protected $hints = array();
- protected $immortal = false;
+ protected $immortal;
protected $options = array();
protected $batchSize;
protected $limit;
protected $skip;
- protected $slaveOkay = false;
+ protected $slaveOkay;
protected $snapshot;
protected $sorts = array();
- protected $tailable = false;
+ protected $tailable;
protected $timeout;
/**
@@ -100,7 +100,9 @@ public function recreate()
foreach ($this->hints as $hint) {
$this->mongoCursor->hint($hint);
}
- $this->mongoCursor->immortal($this->immortal);
+ if ($this->immortal !== null) {
+ $this->mongoCursor->immortal($this->immortal);
+ }
foreach ($this->options as $key => $value) {
$this->mongoCursor->addOption($key, $value);
}
@@ -113,14 +115,18 @@ public function recreate()
if ($this->skip !== null) {
$this->mongoCursor->skip($this->skip);
}
- $this->mongoCursor->slaveOkay($this->slaveOkay);
+ if ($this->slaveOkay !== null) {
+ $this->mongoCursor->slaveOkay($this->slaveOkay);
+ }
if ($this->snapshot) {
$this->mongoCursor->snapshot();
}
foreach ($this->sorts as $sort) {
$this->mongoCursor->sort($sort);
}
- $this->mongoCursor->tailable($this->tailable);
+ if ($this->tailable !== null) {
+ $this->mongoCursor->tailable($this->tailable);
+ }
if ($this->timeout !== null) {
$this->mongoCursor->timeout($this->timeout);
}
@@ -203,6 +209,7 @@ public function hint(array $keyPattern)
public function immortal($liveForever = true)
{
+ $liveForever = (boolean) $liveForever;
$this->immortal = $liveForever;
$this->mongoCursor->immortal($liveForever);
return $this;
@@ -251,6 +258,7 @@ public function addOption($key, $value)
public function batchSize($num)
{
+ $limit = (integer) $num;
$this->batchSize = $num;
$this->mongoCursor->batchSize($num);
return $this;
@@ -258,6 +266,7 @@ public function batchSize($num)
public function limit($num)
{
+ $limit = (integer) $num;
$this->limit = $num;
$this->mongoCursor->limit($num);
return $this;
@@ -265,15 +274,17 @@ public function limit($num)
public function skip($num)
{
+ $num = (integer) $num;
$this->skip = $num;
$this->mongoCursor->skip($num);
return $this;
}
- public function slaveOkay($okay = true)
+ public function slaveOkay($ok = true)
{
- $this->slaveOkay = $okay;
- $this->mongoCursor->slaveOkay($okay);
+ $ok = (boolean) $ok;
+ $this->slaveOkay = $ok;
+ $this->mongoCursor->slaveOkay($ok);
return $this;
}
@@ -290,8 +301,7 @@ public function sort($fields)
if (is_string($order)) {
$order = strtolower($order) === 'asc' ? 1 : -1;
}
- $order = (int) $order;
- $fields[$fieldName] = $order;
+ $fields[$fieldName] = (integer) $order;
}
$this->sorts[] = $fields;
$this->mongoCursor->sort($fields);
@@ -300,6 +310,7 @@ public function sort($fields)
public function tailable($tail = true)
{
+ $tail = (boolean) $tail;
$this->tailable = $tail;
$this->mongoCursor->tailable($tail);
return $this;
@@ -307,7 +318,7 @@ public function tailable($tail = true)
public function timeout($ms)
{
- $this->timeout = $ms;
+ $this->timeout = (integer) $ms;
$this->mongoCursor->timeout($ms);
return $this;
}
@@ -372,4 +383,4 @@ protected function retry(\Closure $retry, $recreate = false)
return $retry();
}
}
-}
+}
View
4 lib/Doctrine/MongoDB/Query/Builder.php
@@ -74,7 +74,7 @@ class Builder
'hints' => array(),
'immortal' => false,
'snapshot' => false,
- 'slaveOkay' => false,
+ 'slaveOkay' => null,
'eagerCursor' => false,
'mapReduce' => array(
'map' => null,
@@ -129,7 +129,7 @@ public function getType()
}
/**
- * Set slave okaye.
+ * Set slave okay.
*
* @param bool $bool
* @return Builder
View
11 lib/Doctrine/MongoDB/Query/Query.php
@@ -23,8 +23,8 @@
use Doctrine\MongoDB\Cursor;
use Doctrine\MongoDB\Database;
use Doctrine\MongoDB\EagerCursor;
-use Doctrine\MongoDB\IteratorAggregate;
use Doctrine\MongoDB\Iterator;
+use Doctrine\MongoDB\IteratorAggregate;
/**
* Query is responsible for executing and returning the results from queries built by the
@@ -236,16 +236,23 @@ protected function prepareCursor(Cursor $cursor)
$cursor->skip($this->query['skip']);
$cursor->sort($this->query['sort']);
$cursor->immortal($this->query['immortal']);
- $cursor->slaveOkay($this->query['slaveOkay']);
+
+ if (null !== $this->query['slaveOkay']) {
+ $cursor->slaveOkay($this->query['slaveOkay']);
+ }
+
if ($this->query['snapshot']) {
$cursor->snapshot();
}
+
foreach ($this->query['hints'] as $keyPattern) {
$cursor->hint($keyPattern);
}
+
if ($this->query['eagerCursor'] === true) {
$cursor = new EagerCursor($cursor);
}
+
return $cursor;
}
Please sign in to comment.
Something went wrong with that request. Please try again.