Skip to content

Commit

Permalink
refactor: create ObjectFactory with make:factory --no-persistence (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Jan 7, 2024
1 parent d07e575 commit 8b72332
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 86 deletions.
12 changes: 1 addition & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ parameters:
count: 1
path: src/AnonymousFactoryGenerator.php

-
message: "#^Property Zenstruck\\\\Foundry\\\\Bundle\\\\Maker\\\\Factory\\\\FactoryClassMap\\:\\:\\$classesWithFactories \\(array\\<class\\-string, class\\-string\\>\\) does not accept array\\.$#"
count: 1
path: src/Bundle/Maker/Factory/FactoryClassMap.php

-
message: "#^Match expression does not handle remaining value\\: array\\{bool, bool\\}$#"
count: 1
path: src/Bundle/Maker/Factory/MakeFactoryPHPDocMethod.php

-
message: "#^Parameter \\#1 \\$class of method Doctrine\\\\Persistence\\\\ManagerRegistry\\:\\:getManagerForClass\\(\\) expects class\\-string, string given\\.$#"
count: 1
Expand All @@ -31,7 +21,7 @@ parameters:
path: src/Factory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\FactoryCollection\\:\\:create\\(\\) should return array\\<int, TObject of object\\> but returns array\\<int, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\FactoryCollection\\:\\:create\\(\\) should return array\\<int, TObject of object\\> but returns array\\<int\\<0, max\\>, \\(TObject of object\\)\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\>\\>\\.$#"
count: 1
path: src/FactoryCollection.php

Expand Down
22 changes: 20 additions & 2 deletions src/Bundle/Maker/Factory/MakeFactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\RepositoryDecorator;
Expand All @@ -36,11 +38,14 @@ final class MakeFactoryData
public function __construct(private \ReflectionClass $object, private ClassNameDetails $factoryClassNameDetails, private ?\ReflectionClass $repository, private string $staticAnalysisTool, private bool $persisted)
{
$this->uses = [
PersistentProxyObjectFactory::class,
Proxy::class,
$this->getFactoryClass(),
$object->getName(),
];

if ($this->persisted) {
$this->uses[] = Proxy::class;
}

if ($repository) {
$this->uses[] = $repository->getName();
$this->uses[] = RepositoryDecorator::class;
Expand All @@ -59,6 +64,19 @@ public function getObjectShortName(): string
return $this->object->getShortName();
}

/**
* @return class-string<Factory>
*/
public function getFactoryClass(): string
{
return $this->isPersisted() ? PersistentProxyObjectFactory::class : ObjectFactory::class;
}

public function getFactoryClassShortName(): string
{
return (new \ReflectionClass($this->getFactoryClass()))->getShortName();
}

public function getFactoryClassNameDetails(): ClassNameDetails
{
return $this->factoryClassNameDetails;
Expand Down
25 changes: 12 additions & 13 deletions src/Bundle/Resources/skeleton/Factory.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@
?>

/**
* @extends PersistentProxyObjectFactory<<?php echo $makeFactoryData->getObjectShortName(); ?>>
*
* @extends <?php echo $makeFactoryData->getFactoryClassShortName(); ?><<?php echo $makeFactoryData->getObjectShortName(); ?>>
<?php
foreach ($makeFactoryData->getMethodsPHPDoc() as $methodPHPDoc) {
echo "{$methodPHPDoc->toString()}\n";
}

if ($makeFactoryData->hasStaticAnalysisTool()) {
if ($makeFactoryData->isPersisted()) {
echo " *\n";

foreach ($makeFactoryData->getMethodsPHPDoc() as $methodPHPDoc) {
echo "{$methodPHPDoc->toString($makeFactoryData->staticAnalysisTool())}\n";
echo "{$methodPHPDoc->toString()}\n";
}

if ($makeFactoryData->hasStaticAnalysisTool()) {
echo " *\n";

foreach ($makeFactoryData->getMethodsPHPDoc() as $methodPHPDoc) {
echo "{$methodPHPDoc->toString($makeFactoryData->staticAnalysisTool())}\n";
}
}
}
?>
*/
final class <?php echo $class_name; ?> extends PersistentProxyObjectFactory
final class <?php echo $class_name; ?> extends <?php echo $makeFactoryData->getFactoryClassShortName(); ?>
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -63,9 +65,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
<?php if (!$makeFactoryData->isPersisted()) { ?>
->withoutPersisting()
<?php } ?>
// ->afterInstantiate(function(<?php echo $makeFactoryData->getObjectShortName(); ?> $<?php echo \lcfirst($makeFactoryData->getObjectShortName()); ?>): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;

/**
* @extends PersistentProxyObjectFactory<SomeObject>
*
* @method SomeObject|Proxy create(array|callable $attributes = [])
* @method static SomeObject|Proxy createOne(array $attributes = [])
* @method static SomeObject[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static SomeObject[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<SomeObject>
*/
final class SomeObjectFactory extends PersistentProxyObjectFactory
final class SomeObjectFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -67,7 +61,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(SomeObject $someObject): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;

/**
* @extends PersistentProxyObjectFactory<SomeObject>
*
* @method SomeObject|Proxy create(array|callable $attributes = [])
* @method static SomeObject|Proxy createOne(array $attributes = [])
* @method static SomeObject[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static SomeObject[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<SomeObject>
*/
final class SomeObjectFactory extends PersistentProxyObjectFactory
final class SomeObjectFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -65,7 +59,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(SomeObject $someObject): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Document\ODMComment;
use Zenstruck\Foundry\Tests\Fixtures\Factories\ODM\UserFactory;

/**
* @extends PersistentProxyObjectFactory<ODMComment>
*
* @method ODMComment|Proxy create(array|callable $attributes = [])
* @method static ODMComment|Proxy createOne(array $attributes = [])
* @method static ODMComment[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static ODMComment[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<ODMComment>
*/
final class ODMCommentFactory extends PersistentProxyObjectFactory
final class ODMCommentFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -59,7 +53,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(ODMComment $oDMComment): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Address;

/**
* @extends PersistentProxyObjectFactory<Address>
*
* @method Address|Proxy create(array|callable $attributes = [])
* @method static Address|Proxy createOne(array $attributes = [])
* @method static Address[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Address[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<Address>
*/
final class AddressFactory extends PersistentProxyObjectFactory
final class AddressFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -57,7 +51,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(Address $address): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;

/**
* @extends PersistentProxyObjectFactory<Category>
*
* @method Category|Proxy create(array|callable $attributes = [])
* @method static Category|Proxy createOne(array $attributes = [])
* @method static Category[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static Category[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<Category>
*/
final class CategoryFactory extends PersistentProxyObjectFactory
final class CategoryFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -58,7 +52,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(Category $category): void {})
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@

namespace App\Factory;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\ObjectFactory;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\EntityWithEnum;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;

/**
* @extends PersistentProxyObjectFactory<EntityWithEnum>
*
* @method EntityWithEnum|Proxy create(array|callable $attributes = [])
* @method static EntityWithEnum|Proxy createOne(array $attributes = [])
* @method static EntityWithEnum[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static EntityWithEnum[]|Proxy[] createSequence(iterable|callable $sequence)
* @extends ObjectFactory<EntityWithEnum>
*/
final class EntityWithEnumFactory extends PersistentProxyObjectFactory
final class EntityWithEnumFactory extends ObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
Expand Down Expand Up @@ -58,7 +52,6 @@ protected function defaults(): array|callable
protected function initialize(): static
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(EntityWithEnum $entityWithEnum): void {})
;
}
Expand Down

0 comments on commit 8b72332

Please sign in to comment.