From d7e5830391dc756344087fca409f530759ed9b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 13 Mar 2024 11:55:33 +0100 Subject: [PATCH] Convert class to trait --- generator/src/FluentStageFactoryGenerator.php | 26 +++++++++---------- ...uentFactory.php => FluentFactoryTrait.php} | 2 +- tests/Builder/FluentPipelineFactoryTest.php | 7 +++-- 3 files changed, 19 insertions(+), 16 deletions(-) rename src/Builder/Stage/{FluentFactory.php => FluentFactoryTrait.php} (99%) diff --git a/generator/src/FluentStageFactoryGenerator.php b/generator/src/FluentStageFactoryGenerator.php index 1efd882..3bd377d 100644 --- a/generator/src/FluentStageFactoryGenerator.php +++ b/generator/src/FluentStageFactoryGenerator.php @@ -40,17 +40,17 @@ public function generate(GeneratorDefinition $definition): void private function createFluentFactoryClass(GeneratorDefinition $definition): PhpNamespace { $namespace = new PhpNamespace($definition->namespace); - $class = $namespace->addClass('FluentFactory'); + $trait = $namespace->addTrait('FluentFactoryTrait'); $namespace->addUse(self::FACTORY_CLASS); $namespace->addUse(StageInterface::class); $namespace->addUse(Pipeline::class); - $class->addProperty('pipeline') + $trait->addProperty('pipeline') ->setType('array') ->setComment('@var list') ->setValue([]); - $class->addMethod('getPipeline') + $trait->addMethod('getPipeline') ->setReturnType(Pipeline::class) ->setBody(<<<'PHP' return new Pipeline(...$this->pipeline); @@ -62,23 +62,23 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN // Import the methods customized in the factory class foreach ($staticFactory->getMethods() as $method) { - $this->addMethod($method, $class); + $this->addMethod($method, $trait); } // Import the other methods provided by the generated trait - foreach ($staticFactory->getTraits() as $trait) { - $this->addUsesFrom($trait->getName(), $namespace); - $staticFactory = TraitType::from($trait->getName()); + foreach ($staticFactory->getTraits() as $usedTrait) { + $this->addUsesFrom($usedTrait->getName(), $namespace); + $staticFactory = TraitType::from($usedTrait->getName()); assert($staticFactory instanceof TraitType); foreach ($staticFactory->getMethods() as $method) { - $this->addMethod($method, $class); + $this->addMethod($method, $trait); } } return $namespace; } - private function addMethod(Method $factoryMethod, ClassType $class): void + private function addMethod(Method $factoryMethod, TraitType $trait): void { // Non-public methods are not part of the API if (! $factoryMethod->isPublic()) { @@ -87,11 +87,11 @@ private function addMethod(Method $factoryMethod, ClassType $class): void // Some methods can be overridden in the class, so we skip them // when importing the methods provided by the trait. - if ($class->hasMethod($factoryMethod->getName())) { + if ($trait->hasMethod($factoryMethod->getName())) { return; } - $method = $class->addMethod($factoryMethod->getName()); + $method = $trait->addMethod($factoryMethod->getName()); $method->setComment($factoryMethod->getComment()); $method->setParameters($factoryMethod->getParameters()); @@ -119,9 +119,9 @@ private function addMethod(Method $factoryMethod, ClassType $class): void )); } - private static function addUsesFrom(string $class, PhpNamespace $namespace): void + private static function addUsesFrom(string $classLike, PhpNamespace $namespace): void { - $file = PhpFile::fromCode(file_get_contents((new ReflectionClass($class))->getFileName())); + $file = PhpFile::fromCode(file_get_contents((new ReflectionClass($classLike))->getFileName())); foreach ($file->getNamespaces() as $ns) { foreach ($ns->getUses() as $use) { diff --git a/src/Builder/Stage/FluentFactory.php b/src/Builder/Stage/FluentFactoryTrait.php similarity index 99% rename from src/Builder/Stage/FluentFactory.php rename to src/Builder/Stage/FluentFactoryTrait.php index 1ae4d2c..0ffbcd4 100644 --- a/src/Builder/Stage/FluentFactory.php +++ b/src/Builder/Stage/FluentFactoryTrait.php @@ -31,7 +31,7 @@ use MongoDB\Model\BSONArray; use stdClass; -class FluentFactory +trait FluentFactoryTrait { /** @var list */ public array $pipeline = []; diff --git a/tests/Builder/FluentPipelineFactoryTest.php b/tests/Builder/FluentPipelineFactoryTest.php index 5218505..e1d84a2 100644 --- a/tests/Builder/FluentPipelineFactoryTest.php +++ b/tests/Builder/FluentPipelineFactoryTest.php @@ -5,14 +5,17 @@ namespace MongoDB\Tests\Builder; use MongoDB\Builder\Query; -use MongoDB\Builder\Stage\FluentFactory; +use MongoDB\Builder\Stage\FluentFactoryTrait; use MongoDB\Builder\Type\Sort; class FluentPipelineFactoryTest extends PipelineTestCase { public function testFluentPipelineFactory(): void { - $pipeline = (new FluentFactory()) + $factory = new class { + use FluentFactoryTrait; + }; + $pipeline = $factory ->match(x: Query::eq(1)) ->project(_id: false, x: true) ->sort(x: Sort::Asc)