Skip to content

Commit

Permalink
Convert class to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Mar 13, 2024
1 parent 7f8a396 commit d7e5830
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
26 changes: 13 additions & 13 deletions generator/src/FluentStageFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<StageInterface>')
->setValue([]);
$class->addMethod('getPipeline')
$trait->addMethod('getPipeline')
->setReturnType(Pipeline::class)
->setBody(<<<'PHP'
return new Pipeline(...$this->pipeline);
Expand All @@ -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()) {
Expand All @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions tests/Builder/FluentPipelineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d7e5830

Please sign in to comment.