Skip to content

Commit

Permalink
PHPLIB-1533: Mark Collection::mapReduce as deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Sep 24, 2024
1 parent fb6a9c8 commit 09c78e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@
use function array_key_exists;
use function current;
use function is_array;
use function sprintf;
use function strlen;
use function trigger_error;

use const E_USER_DEPRECATED;

class Collection
{
Expand Down Expand Up @@ -952,6 +956,8 @@ public function listSearchIndexes(array $options = []): Iterator
*/
public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce, string|array|object $out, array $options = [])
{
@trigger_error(sprintf('The %s method is deprecated and will be removed in a future release.', __METHOD__), E_USER_DEPRECATED);

$hasOutputCollection = ! is_mapreduce_output_inline($out);

// Check if the out option is inline because we will want to coerce a primary read preference if not
Expand Down
4 changes: 3 additions & 1 deletion tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ public function testMapReduce(): void
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = ['inline' => 1];

$result = $this->collection->mapReduce($map, $reduce, $out);
$result = $this->assertDeprecated(
fn () => $this->collection->mapReduce($map, $reduce, $out),
);

$this->assertInstanceOf(MapReduceResult::class, $result);
$expected = [
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ final public static function provideInvalidStringValues(): array
return self::wrapValuesForDataProvider(self::getInvalidStringValues());
}

protected function assertDeprecated(callable $execution): void
protected function assertDeprecated(callable $execution)
{
$errors = [];

Expand All @@ -169,12 +169,14 @@ protected function assertDeprecated(callable $execution): void
}, E_USER_DEPRECATED);

try {
call_user_func($execution);
$result = call_user_func($execution);
} finally {
restore_error_handler();
}

$this->assertCount(1, $errors);

return $result;
}

protected static function createOptionDataProvider(array $options): array
Expand Down

0 comments on commit 09c78e3

Please sign in to comment.