Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit d7e5830

Browse files
committed
Convert class to trait
1 parent 7f8a396 commit d7e5830

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

generator/src/FluentStageFactoryGenerator.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public function generate(GeneratorDefinition $definition): void
4040
private function createFluentFactoryClass(GeneratorDefinition $definition): PhpNamespace
4141
{
4242
$namespace = new PhpNamespace($definition->namespace);
43-
$class = $namespace->addClass('FluentFactory');
43+
$trait = $namespace->addTrait('FluentFactoryTrait');
4444

4545
$namespace->addUse(self::FACTORY_CLASS);
4646
$namespace->addUse(StageInterface::class);
4747
$namespace->addUse(Pipeline::class);
4848

49-
$class->addProperty('pipeline')
49+
$trait->addProperty('pipeline')
5050
->setType('array')
5151
->setComment('@var list<StageInterface>')
5252
->setValue([]);
53-
$class->addMethod('getPipeline')
53+
$trait->addMethod('getPipeline')
5454
->setReturnType(Pipeline::class)
5555
->setBody(<<<'PHP'
5656
return new Pipeline(...$this->pipeline);
@@ -62,23 +62,23 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
6262

6363
// Import the methods customized in the factory class
6464
foreach ($staticFactory->getMethods() as $method) {
65-
$this->addMethod($method, $class);
65+
$this->addMethod($method, $trait);
6666
}
6767

6868
// Import the other methods provided by the generated trait
69-
foreach ($staticFactory->getTraits() as $trait) {
70-
$this->addUsesFrom($trait->getName(), $namespace);
71-
$staticFactory = TraitType::from($trait->getName());
69+
foreach ($staticFactory->getTraits() as $usedTrait) {
70+
$this->addUsesFrom($usedTrait->getName(), $namespace);
71+
$staticFactory = TraitType::from($usedTrait->getName());
7272
assert($staticFactory instanceof TraitType);
7373
foreach ($staticFactory->getMethods() as $method) {
74-
$this->addMethod($method, $class);
74+
$this->addMethod($method, $trait);
7575
}
7676
}
7777

7878
return $namespace;
7979
}
8080

81-
private function addMethod(Method $factoryMethod, ClassType $class): void
81+
private function addMethod(Method $factoryMethod, TraitType $trait): void
8282
{
8383
// Non-public methods are not part of the API
8484
if (! $factoryMethod->isPublic()) {
@@ -87,11 +87,11 @@ private function addMethod(Method $factoryMethod, ClassType $class): void
8787

8888
// Some methods can be overridden in the class, so we skip them
8989
// when importing the methods provided by the trait.
90-
if ($class->hasMethod($factoryMethod->getName())) {
90+
if ($trait->hasMethod($factoryMethod->getName())) {
9191
return;
9292
}
9393

94-
$method = $class->addMethod($factoryMethod->getName());
94+
$method = $trait->addMethod($factoryMethod->getName());
9595

9696
$method->setComment($factoryMethod->getComment());
9797
$method->setParameters($factoryMethod->getParameters());
@@ -119,9 +119,9 @@ private function addMethod(Method $factoryMethod, ClassType $class): void
119119
));
120120
}
121121

122-
private static function addUsesFrom(string $class, PhpNamespace $namespace): void
122+
private static function addUsesFrom(string $classLike, PhpNamespace $namespace): void
123123
{
124-
$file = PhpFile::fromCode(file_get_contents((new ReflectionClass($class))->getFileName()));
124+
$file = PhpFile::fromCode(file_get_contents((new ReflectionClass($classLike))->getFileName()));
125125

126126
foreach ($file->getNamespaces() as $ns) {
127127
foreach ($ns->getUses() as $use) {

src/Builder/Stage/FluentFactory.php renamed to src/Builder/Stage/FluentFactoryTrait.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/FluentPipelineFactoryTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
namespace MongoDB\Tests\Builder;
66

77
use MongoDB\Builder\Query;
8-
use MongoDB\Builder\Stage\FluentFactory;
8+
use MongoDB\Builder\Stage\FluentFactoryTrait;
99
use MongoDB\Builder\Type\Sort;
1010

1111
class FluentPipelineFactoryTest extends PipelineTestCase
1212
{
1313
public function testFluentPipelineFactory(): void
1414
{
15-
$pipeline = (new FluentFactory())
15+
$factory = new class {
16+
use FluentFactoryTrait;
17+
};
18+
$pipeline = $factory
1619
->match(x: Query::eq(1))
1720
->project(_id: false, x: true)
1821
->sort(x: Sort::Asc)

0 commit comments

Comments
 (0)