diff --git a/CHANGELOG.md b/CHANGELOG.md index 97d80c85..88192a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ v0.8.3 (2013-xx-xx) returning `Predis\Connection\ConnectionInterface`. Users can create their own self-contained strategies to create and set up the underlying connection. +- Users should return `0` from `Predis\Command\ScriptedCommand::getKeysCount()` + instead of `FALSE` to indicate that all of the arguments of a Lua script must + be used to populate `ARGV[]`. This does not represent a breaking change. + v0.8.2 (2013-02-03) =============================================================================== diff --git a/lib/Predis/Command/ScriptedCommand.php b/lib/Predis/Command/ScriptedCommand.php index 22371fbc..9c0065c5 100644 --- a/lib/Predis/Command/ScriptedCommand.php +++ b/lib/Predis/Command/ScriptedCommand.php @@ -30,19 +30,15 @@ abstract class ScriptedCommand extends ServerEvalSHA /** * Specifies the number of arguments that should be considered as keys. * - * The default behaviour for the base class is to return FALSE to indicate that + * The default behaviour for the base class is to return 0 to indicate that * all the elements of the arguments array should be considered as keys, but * subclasses can enforce a static number of keys. * - * @todo How about returning 1 by default to make scripted commands act like - * variadic ones where the first argument is the key (KEYS[1]) and the - * rest are values (ARGV)? - * - * @return int|Boolean + * @return int */ protected function getKeysCount() { - return false; + return 0; } /** @@ -60,13 +56,11 @@ abstract class ScriptedCommand extends ServerEvalSHA */ protected function filterArguments(Array $arguments) { - if (false !== $numkeys = $this->getKeysCount()) { - $numkeys = $numkeys >= 0 ? $numkeys : count($arguments) + $numkeys; - } else { - $numkeys = count($arguments); + if (($numkeys = $this->getKeysCount()) && $numkeys < 0) { + $numkeys = count($arguments) + $numkeys; } - return array_merge(array(sha1($this->getScript()), $numkeys), $arguments); + return array_merge(array(sha1($this->getScript()), (int) $numkeys), $arguments); } /** diff --git a/tests/Predis/Command/ScriptedCommandTest.php b/tests/Predis/Command/ScriptedCommandTest.php index 918ac86d..95c31447 100644 --- a/tests/Predis/Command/ScriptedCommandTest.php +++ b/tests/Predis/Command/ScriptedCommandTest.php @@ -60,6 +60,25 @@ class ScriptedCommandTest extends StandardTestCase $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 2), $arguments), $command->getArguments()); } + /** + * @group disconnected + */ + public function testGetArgumentsWithZeroKeysCount() + { + $arguments = array('value1', 'value2', 'value3'); + + $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command->expects($this->once()) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->expects($this->once()) + ->method('getKeysCount') + ->will($this->returnValue(0)); + $command->setArguments($arguments); + + $this->assertSame(array_merge(array(self::LUA_SCRIPT_SHA1, 0), $arguments), $command->getArguments()); + } + /** * @group disconnected */ @@ -79,6 +98,25 @@ class ScriptedCommandTest extends StandardTestCase $this->assertSame(array('key1', 'key2'), $command->getKeys()); } + /** + * @group disconnected + */ + public function testGetKeysWithZeroKeysCount() + { + $arguments = array('value1', 'value2', 'value3'); + + $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command->expects($this->once()) + ->method('getScript') + ->will($this->returnValue(self::LUA_SCRIPT)); + $command->expects($this->exactly(2)) + ->method('getKeysCount') + ->will($this->returnValue(0)); + $command->setArguments($arguments); + + $this->assertSame(array(), $command->getKeys()); + } + /** * @group disconnected */