diff --git a/lib/classes/dml/recordset_walk.php b/lib/classes/dml/recordset_walk.php index 7e9ea692887fc..3d560cf8fd2bf 100644 --- a/lib/classes/dml/recordset_walk.php +++ b/lib/classes/dml/recordset_walk.php @@ -57,7 +57,7 @@ class recordset_walk implements \Iterator { protected $callback; /** - * @var array|false Extra params for the callback. + * @var mixed|null Extra param for the callback. */ protected $callbackextra; @@ -66,9 +66,9 @@ class recordset_walk implements \Iterator { * * @param \moodle_recordset $recordset Recordset to iterate. * @param callable $callback Apply this function to each record. If using a method, it should be public. - * @param array $callbackextra Array of arguments to pass to the callback. + * @param mixed $callbackextra An extra single parameter to pass to the callback. Use a container to pass multiple values. */ - public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = false) { + public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = null) { $this->recordset = $recordset; $this->callback = $callback; $this->callbackextra = $callbackextra; @@ -99,10 +99,10 @@ public function current() { } // Apply callback and return. - if ($this->callbackextra) { - return call_user_func($this->callback, $record); - } else { + if (!is_null($this->callbackextra)) { return call_user_func($this->callback, $record, $this->callbackextra); + } else { + return call_user_func($this->callback, $record); } } diff --git a/lib/dml/tests/recordset_walk_test.php b/lib/dml/tests/recordset_walk_test.php index bd9c804a449da..bab70450a55dc 100644 --- a/lib/dml/tests/recordset_walk_test.php +++ b/lib/dml/tests/recordset_walk_test.php @@ -45,11 +45,14 @@ public function test_no_data() { $recordset = $DB->get_recordset('assign'); $walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback')); - $this->assertEquals(0, iterator_count($walker)); $this->assertFalse($walker->valid()); + + $count = 0; foreach ($walker as $data) { // No error here. + $count++; } + $this->assertEquals(0, $count); $walker->close(); } @@ -65,11 +68,14 @@ public function test_simple_callback() { // Simple iteration. $recordset = $DB->get_recordset('assign'); $walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback')); - $this->assertEquals(10, iterator_count($walker)); + + $count = 0; foreach ($walker as $data) { // Checking that the callback is being executed on each iteration. $this->assertEquals($data->id . ' potatoes', $data->newfield); + $count++; } + $this->assertEquals(10, $count); // No exception if we double-close. $walker->close(); } @@ -85,17 +91,22 @@ public function test_extra_params_callback() { // Iteration with extra callback arguments. $recordset = $DB->get_recordset('assign'); + $walker = new \core\dml\recordset_walk( $recordset, array($this, 'extra_callback'), array('brown' => 'onions') ); - $this->assertEquals(10, iterator_count($walker)); + + $count = 0; foreach ($walker as $data) { // Checking that the callback is being executed on each // iteration and the param is being passed. $this->assertEquals('onions', $data->brown); + $count++; } + $this->assertEquals(10, $count); + $walker->close(); } @@ -105,7 +116,9 @@ public function test_extra_params_callback() { * @param stdClass $data * @return \Traversable */ - public function simple_callback($data) { + public function simple_callback($data, $nothing = 'notpassed') { + // Confirm nothing was passed. + $this->assertEquals('notpassed', $nothing); $data->newfield = $data->id . ' potatoes'; return $data; }