Skip to content

Commit

Permalink
refactor: add #[\ReturnTypeWillChange] to PersistentProxyObjectFactor…
Browse files Browse the repository at this point in the history
…y::initialize()
  • Loading branch information
nikophil committed Nov 4, 2023
1 parent eaea435 commit dc871b7
Show file tree
Hide file tree
Showing 32 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ parameters:
ignoreErrors:
-
message: '#Unsafe usage of new static\(\).*#'
path: ./src/ModelFactory.php
path: ./src/Persistence/PersistentProxyObjectFactory.php
2 changes: 1 addition & 1 deletion src/Bundle/Resources/skeleton/Factory.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php if (!$makeFactoryData->isPersisted()): ?>
Expand Down
5 changes: 1 addition & 4 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
* @abstract
*
* @author Kevin Bond <kevinbond@gmail.com>
*
* @phpstan-type Parameters = array<string,mixed>
* @phpstan-type Attributes = Parameters|callable(int):Parameters
*/
class Factory
{
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @phpstan-method static list<Proxy<TModel>> createMany(int $number, array|callable $attributes = [])
*
* @author Kevin Bond <kevinbond@gmail.com>
*
* @deprecated use PersistentProxyObjectFactory instead
*/
abstract class ModelFactory extends PersistentProxyObjectFactory
{
Expand Down
3 changes: 2 additions & 1 deletion src/Persistence/PersistentProxyObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -286,6 +286,7 @@ abstract public static function class(): string;
*
* @return static
*/
#[\ReturnTypeWillChange]
protected function initialize()
{
return $this;
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Factories/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function defaults(): array|callable
return ['name' => self::faker()->sentence()];
}

protected function initialize()
protected function initialize(): static
{
return $this
->instantiateWith(
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function defaults(): array|callable
];
}

protected function initialize()
protected function initialize(): static
{
return $this
->instantiateWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class PostFactoryWithInvalidInitialize extends PostFactory
final class PostFactoryWithInvalidInitialize extends LegacyPostFactory
{
protected function initialize()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Factories/PostFactoryWithNullInitialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class PostFactoryWithNullInitialize extends PostFactory
final class PostFactoryWithNullInitialize extends LegacyPostFactory
{
protected function initialize()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
final class PostFactoryWithValidInitialize extends PostFactory
{
protected function initialize(): self
protected function initialize(): static
{
return $this->published();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Maker/expected/can_create_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down
2 changes: 2 additions & 0 deletions tests/Functional/ORMModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit dc871b7

Please sign in to comment.