diff --git a/phpstan.neon b/phpstan.neon index fd4a7ef4..64e207f7 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -37,4 +37,4 @@ parameters: ignoreErrors: - message: '#Unsafe usage of new static\(\).*#' - path: ./src/ModelFactory.php + path: ./src/Persistence/PersistentProxyObjectFactory.php diff --git a/src/Bundle/Resources/skeleton/Factory.tpl.php b/src/Bundle/Resources/skeleton/Factory.tpl.php index 0faf0fcf..6fa78b49 100644 --- a/src/Bundle/Resources/skeleton/Factory.tpl.php +++ b/src/Bundle/Resources/skeleton/Factory.tpl.php @@ -60,7 +60,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this isPersisted()): ?> diff --git a/src/Factory.php b/src/Factory.php index 4b3df68f..914a2a4a 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -24,9 +24,6 @@ * @abstract * * @author Kevin Bond - * - * @phpstan-type Parameters = array - * @phpstan-type Attributes = Parameters|callable(int):Parameters */ class Factory { @@ -226,7 +223,7 @@ final public function withAttributes($attributes = []): self } /** - * @param Attributes $attributes + * @param array|callable $attributes */ final public function with(array|callable $attributes = []): static { diff --git a/src/ModelFactory.php b/src/ModelFactory.php index 1f84faea..01794a0a 100644 --- a/src/ModelFactory.php +++ b/src/ModelFactory.php @@ -21,6 +21,8 @@ * @phpstan-method static list> createMany(int $number, array|callable $attributes = []) * * @author Kevin Bond + * + * @deprecated use PersistentProxyObjectFactory instead */ abstract class ModelFactory extends PersistentProxyObjectFactory { diff --git a/src/Persistence/PersistentProxyObjectFactory.php b/src/Persistence/PersistentProxyObjectFactory.php index dd6f4fc1..c7a78e3d 100644 --- a/src/Persistence/PersistentProxyObjectFactory.php +++ b/src/Persistence/PersistentProxyObjectFactory.php @@ -56,7 +56,7 @@ final public static function new(array|callable|string $defaultAttributes = [], } $factory = $factory - ->with(static fn(): array => $factory->defaults()) + ->with(static fn(): array|callable => $factory->defaults()) ->with($defaultAttributes); try { @@ -286,6 +286,7 @@ abstract public static function class(): string; * * @return static */ + #[\ReturnTypeWillChange] protected function initialize() { return $this; diff --git a/tests/Fixtures/Factories/CategoryFactory.php b/tests/Fixtures/Factories/CategoryFactory.php index 868c5568..70956fd2 100644 --- a/tests/Fixtures/Factories/CategoryFactory.php +++ b/tests/Fixtures/Factories/CategoryFactory.php @@ -21,7 +21,7 @@ protected function defaults(): array|callable return ['name' => self::faker()->sentence()]; } - protected function initialize() + protected function initialize(): static { return $this ->instantiateWith( diff --git a/tests/Fixtures/Factories/PostFactory.php b/tests/Fixtures/Factories/PostFactory.php index 44dc30a7..0dc00fa0 100644 --- a/tests/Fixtures/Factories/PostFactory.php +++ b/tests/Fixtures/Factories/PostFactory.php @@ -34,7 +34,7 @@ protected function defaults(): array|callable ]; } - protected function initialize() + protected function initialize(): static { return $this ->instantiateWith( diff --git a/tests/Fixtures/Factories/PostFactoryWithInvalidInitialize.php b/tests/Fixtures/Factories/PostFactoryWithInvalidInitialize.php index be590641..9a8001ac 100644 --- a/tests/Fixtures/Factories/PostFactoryWithInvalidInitialize.php +++ b/tests/Fixtures/Factories/PostFactoryWithInvalidInitialize.php @@ -5,7 +5,7 @@ /** * @author Kevin Bond */ -final class PostFactoryWithInvalidInitialize extends PostFactory +final class PostFactoryWithInvalidInitialize extends LegacyPostFactory { protected function initialize() { diff --git a/tests/Fixtures/Factories/PostFactoryWithNullInitialize.php b/tests/Fixtures/Factories/PostFactoryWithNullInitialize.php index 74eacc33..36a925b5 100644 --- a/tests/Fixtures/Factories/PostFactoryWithNullInitialize.php +++ b/tests/Fixtures/Factories/PostFactoryWithNullInitialize.php @@ -5,7 +5,7 @@ /** * @author Kevin Bond */ -final class PostFactoryWithNullInitialize extends PostFactory +final class PostFactoryWithNullInitialize extends LegacyPostFactory { protected function initialize() { diff --git a/tests/Fixtures/Factories/PostFactoryWithValidInitialize.php b/tests/Fixtures/Factories/PostFactoryWithValidInitialize.php index 45284a6d..29ea1ba9 100644 --- a/tests/Fixtures/Factories/PostFactoryWithValidInitialize.php +++ b/tests/Fixtures/Factories/PostFactoryWithValidInitialize.php @@ -7,7 +7,7 @@ */ final class PostFactoryWithValidInitialize extends PostFactory { - protected function initialize(): self + protected function initialize(): static { return $this->published(); } diff --git a/tests/Fixtures/Maker/expected/can_create_factory.php b/tests/Fixtures/Maker/expected/can_create_factory.php index d2038932..cc1b28fa 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory.php +++ b/tests/Fixtures/Maker/expected/can_create_factory.php @@ -67,7 +67,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Category $category): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_phpstan.php b/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_phpstan.php index 262dca96..b35e09eb 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_phpstan.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_phpstan.php @@ -86,7 +86,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Post $post): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php b/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php index b28fec2a..735e1767 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php @@ -86,7 +86,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Post $post): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class.php b/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class.php index 5e12b29e..d7d0c492 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class.php @@ -64,7 +64,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class_interactively.php b/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class_interactively.php index f66f00ce..206f7728 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class_interactively.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_not_persisted_class_interactively.php @@ -62,7 +62,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_document.php b/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_document.php index 3727326e..10bd0347 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_document.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_document.php @@ -70,7 +70,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(ODMPost $oDMPost): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_embedded_document.php b/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_embedded_document.php index f0de8de3..40d03f89 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_embedded_document.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_odm_with_data_set_embedded_document.php @@ -56,7 +56,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_for_orm_embedded_class.php b/tests/Fixtures/Maker/expected/can_create_factory_for_orm_embedded_class.php index a69754e6..19c7881f 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_for_orm_embedded_class.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_for_orm_embedded_class.php @@ -54,7 +54,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir.php b/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir.php index 366b7e8c..683af2a2 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir.php @@ -67,7 +67,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Category $category): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir_interactively.php b/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir_interactively.php index 18e8b475..16a3e42f 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir_interactively.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_in_test_dir_interactively.php @@ -67,7 +67,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Tag $tag): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_interactively.php b/tests/Fixtures/Maker/expected/can_create_factory_interactively.php index dc7ced3f..24c02ec9 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_interactively.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_interactively.php @@ -71,7 +71,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Comment $comment): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_auto_activated_not_persisted_option.php b/tests/Fixtures/Maker/expected/can_create_factory_with_auto_activated_not_persisted_option.php index 29b4aab4..8ed0a51f 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_auto_activated_not_persisted_option.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_auto_activated_not_persisted_option.php @@ -55,7 +55,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_odm.php b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_odm.php index 3fdc926e..bd76f01c 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_odm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_odm.php @@ -68,7 +68,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(DocumentWithEnum $documentWithEnum): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_orm.php b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_orm.php index 1b66dacc..2966553a 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_orm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_orm.php @@ -68,7 +68,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(EntityWithEnum $entityWithEnum): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_without_persistence.php b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_without_persistence.php index 3b7d9a1e..4a61d791 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_without_persistence.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_default_enum_with_data_set_without_persistence.php @@ -55,7 +55,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this ->withoutPersisting() diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_odm.php b/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_odm.php index 7ace0ffb..66ff0883 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_odm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_odm.php @@ -73,7 +73,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(ODMPost $oDMPost): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_orm.php b/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_orm.php index b29716c7..89f189a0 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_orm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_embeddable_with_data_set_orm.php @@ -68,7 +68,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Contact $contact): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_relation_defaults.php b/tests/Fixtures/Maker/expected/can_create_factory_with_relation_defaults.php index 6b0bf7ca..5af8e1d5 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_relation_defaults.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_relation_defaults.php @@ -71,7 +71,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(EntityWithRelations $entityWithRelations): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_relation_for_all_fields.php b/tests/Fixtures/Maker/expected/can_create_factory_with_relation_for_all_fields.php index 6b0bf7ca..5af8e1d5 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_relation_for_all_fields.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_relation_for_all_fields.php @@ -71,7 +71,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(EntityWithRelations $entityWithRelations): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_phpstan.php b/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_phpstan.php index a5210606..e742ef70 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_phpstan.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_phpstan.php @@ -83,7 +83,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Category $category): void {}) diff --git a/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php b/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php index 9b67a182..02235d6c 100644 --- a/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php +++ b/tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php @@ -83,7 +83,7 @@ protected function defaults(): array|callable /** * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization */ - protected function initialize(): self + protected function initialize(): static { return $this // ->afterInstantiate(function(Category $category): void {}) diff --git a/tests/Functional/ORMModelFactoryTest.php b/tests/Functional/ORMModelFactoryTest.php index 84a7328a..20c4a569 100644 --- a/tests/Functional/ORMModelFactoryTest.php +++ b/tests/Functional/ORMModelFactoryTest.php @@ -53,6 +53,7 @@ public function can_override_initialize(): void /** * @test + * @group legacy */ public function initialize_must_return_an_instance_of_the_current_factory(): void { @@ -64,6 +65,7 @@ public function initialize_must_return_an_instance_of_the_current_factory(): voi /** * @test + * @group legacy */ public function initialize_must_return_a_value(): void {