Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11821 from cakephp/3.next-depr
Remove combined getter/setter on QueryTrait.
  • Loading branch information
markstory committed Mar 14, 2018
2 parents 0dbe1cf + b3edbb6 commit ff1c0d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Iterator/MapReduce.php
Expand Up @@ -67,7 +67,7 @@ class MapReduce implements IteratorAggregate
* A callable that will be executed for each intermediate record emitted during
* the Map phase
*
* @var callable
* @var callable|null
*/
protected $_reducer;

Expand Down
38 changes: 36 additions & 2 deletions src/Datasource/QueryTrait.php
Expand Up @@ -318,7 +318,7 @@ public function toArray()
* result is attempted to be fetched.
*
* If the first argument is set to null, it will return the list of previously
* registered map reduce routines.
* registered map reduce routines. This is deprecated as of 3.6.0 - use getMapReducers() instead.
*
* If the third argument is set to true, it will erase previous map reducers
* and replace it with the arguments passed.
Expand All @@ -335,13 +335,30 @@ public function mapReduce(callable $mapper = null, callable $reducer = null, $ov
$this->_mapReduce = [];
}
if ($mapper === null) {
if (!$overwrite) {
deprecationWarning(
'Using QueryTrait::mapReduce() as a getter is deprecated. ' .
'Use getMapReducers() instead.'
);
}

return $this->_mapReduce;
}
$this->_mapReduce[] = compact('mapper', 'reducer');

return $this;
}

/**
* Returns the list of previously registered map reduce routines.
*
* @return array
*/
public function getMapReducers()
{
return $this->_mapReduce;
}

/**
* Registers a new formatter callback function that is to be executed when trying
* to fetch the results from the database.
Expand All @@ -354,7 +371,7 @@ public function mapReduce(callable $mapper = null, callable $reducer = null, $ov
* after all the `MapReduce` routines for this query have been executed.
*
* If the first argument is set to null, it will return the list of previously
* registered map reduce routines.
* registered format routines. This is deprecated as of 3.6.0 - use getResultFormatters() instead.
*
* If the second argument is set to true, it will erase previous formatters
* and replace them with the passed first argument.
Expand Down Expand Up @@ -386,6 +403,13 @@ public function formatResults(callable $formatter = null, $mode = 0)
$this->_formatters = [];
}
if ($formatter === null) {
if ($mode !== self::OVERWRITE) {
deprecationWarning(
'Using QueryTrait::formatResults() as a getter is deprecated. ' .
'Use getResultFormatters() instead.'
);
}

return $this->_formatters;
}

Expand All @@ -400,6 +424,16 @@ public function formatResults(callable $formatter = null, $mode = 0)
return $this;
}

/**
* Returns the list of previously registered format routines.
*
* @return array
*/
public function getResultFormatters()
{
return $this->_formatters;
}

/**
* Returns the first result out of executing this query, if the query has not been
* executed before, it will set the limit clause to 1 for performance reasons.
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association.php
Expand Up @@ -1216,7 +1216,7 @@ protected function _appendFields($query, $surrogate, $options)
*/
protected function _formatAssociationResults($query, $surrogate, $options)
{
$formatters = $surrogate->formatResults();
$formatters = $surrogate->getResultFormatters();

if (!$formatters || empty($options['propertyPath'])) {
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/CompositeKeysTest.php
Expand Up @@ -606,7 +606,7 @@ public function testFindThreadedCompositeKeys()
['id' => 8, 'name' => 'a', 'site_id' => 2, 'parent_id' => 4],
]);
$query->find('threaded', ['parentField' => ['parent_id', 'site_id']]);
$formatter = $query->formatResults()[0];
$formatter = $query->getResultFormatters()[0];

$expected = [
[
Expand Down
22 changes: 11 additions & 11 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -991,11 +991,11 @@ public function testMapReduceOnlyMapper()
$this->assertSame($query, $query->mapReduce($mapper1));
$this->assertEquals(
[['mapper' => $mapper1, 'reducer' => null]],
$query->mapReduce()
$query->getMapReducers()
);

$this->assertEquals($query, $query->mapReduce($mapper2));
$result = $query->mapReduce();
$result = $query->getMapReducers();
$this->assertSame(
[
['mapper' => $mapper1, 'reducer' => null],
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public function testMapReduceBothMethods()
$this->assertSame($query, $query->mapReduce($mapper1, $reducer1));
$this->assertEquals(
[['mapper' => $mapper1, 'reducer' => $reducer1]],
$query->mapReduce()
$query->getMapReducers()
);

$this->assertSame($query, $query->mapReduce($mapper2, $reducer2));
Expand All @@ -1033,7 +1033,7 @@ public function testMapReduceBothMethods()
['mapper' => $mapper1, 'reducer' => $reducer1],
['mapper' => $mapper2, 'reducer' => $reducer2]
],
$query->mapReduce()
$query->getMapReducers()
);
}

Expand All @@ -1056,13 +1056,13 @@ public function testOverwriteMapReduce()
$this->assertEquals($query, $query->mapReduce($mapper1, $reducer1));
$this->assertEquals(
[['mapper' => $mapper1, 'reducer' => $reducer1]],
$query->mapReduce()
$query->getMapReducers()
);

$this->assertEquals($query, $query->mapReduce($mapper2, $reducer2, true));
$this->assertEquals(
[['mapper' => $mapper2, 'reducer' => $reducer2]],
$query->mapReduce()
$query->getMapReducers()
);
}

Expand Down Expand Up @@ -2141,17 +2141,17 @@ public function testFormatResults()
$table = $this->getTableLocator()->get('authors');
$query = new Query($this->connection, $table);
$this->assertSame($query, $query->formatResults($callback1));
$this->assertSame([$callback1], $query->formatResults());
$this->assertSame([$callback1], $query->getResultFormatters());
$this->assertSame($query, $query->formatResults($callback2));
$this->assertSame([$callback1, $callback2], $query->formatResults());
$this->assertSame([$callback1, $callback2], $query->getResultFormatters());
$query->formatResults($callback2, true);
$this->assertSame([$callback2], $query->formatResults());
$this->assertSame([$callback2], $query->getResultFormatters());
$query->formatResults(null, true);
$this->assertSame([], $query->formatResults());
$this->assertSame([], $query->getResultFormatters());

$query->formatResults($callback1);
$query->formatResults($callback2, $query::PREPEND);
$this->assertSame([$callback2, $callback1], $query->formatResults());
$this->assertSame([$callback2, $callback1], $query->getResultFormatters());
}

/**
Expand Down

0 comments on commit ff1c0d2

Please sign in to comment.