diff --git a/src/functions.php b/src/functions.php index 95c85380b..4f438ab45 100644 --- a/src/functions.php +++ b/src/functions.php @@ -34,6 +34,7 @@ use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionException; +use stdClass; use function array_is_list; use function array_key_first; @@ -58,6 +59,17 @@ function add_logger(LoggerInterface $logger): void PsrLogAdapter::addLogger($logger); } +/** + * Create a new stdClass instance with the provided properties. + * + * @psalm-suppress MoreSpecificReturnType + * @psalm-suppress LessSpecificReturnStatement + */ +function object(mixed ...$values): stdClass +{ + return (object) $values; +} + /** * Unregisters a PSR-3 logger. * diff --git a/tests/Builder/BuilderEncoderTest.php b/tests/Builder/BuilderEncoderTest.php index df94d7b6d..9c0230deb 100644 --- a/tests/Builder/BuilderEncoderTest.php +++ b/tests/Builder/BuilderEncoderTest.php @@ -15,6 +15,7 @@ use function array_merge; use function array_walk; use function is_array; +use function MongoDB\object; use function var_export; /** @@ -28,7 +29,7 @@ public function testPipeline(): void { $pipeline = new Pipeline( // @todo array is accepted by the stage class, but we expect an object. The driver accepts both. - Stage::match((object) ['author' => 'dave']), + Stage::match(object(author: 'dave')), Stage::limit(1), ); @@ -44,7 +45,7 @@ public function testPipeline(): void public function testSort(): void { $pipeline = new Pipeline( - Stage::sort((object) ['age' => -1, 'posts' => 1]), + Stage::sort(object(age: -1, posts: 1)), ); $expected = [ @@ -60,8 +61,8 @@ public function testPerformCount(): void $pipeline = new Pipeline( Stage::match( Query::or( - (object) ['score' => [Query::gt(70), Query::lt(90)]], - (object) ['views' => Query::gte(1000)], + object(score: [Query::gt(70), Query::lt(90)]), + object(views: Query::gte(1000)), ), ), Stage::group( @@ -99,14 +100,14 @@ public function testPerformCount(): void public function testAggregationFilter(array $limit, array $expectedLimit): void { $pipeline = new Pipeline( - Stage::project(...[ - 'items' => Aggregation::filter( + Stage::project( + items: Aggregation::filter( Expression::arrayFieldPath('items'), Aggregation::gte(Expression::variable('item.price'), 100), 'item', ...$limit, ), - ]), + ), ); $expected = [