From 1f530bf858e0dc837cc0ef1f089c55af687adeae Mon Sep 17 00:00:00 2001 From: ppelgrims Date: Mon, 13 Oct 2025 21:04:00 +0200 Subject: [PATCH 1/3] Add of object to template --- src/Factories/InstanceFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factories/InstanceFactory.php b/src/Factories/InstanceFactory.php index 36371b2..21b414d 100644 --- a/src/Factories/InstanceFactory.php +++ b/src/Factories/InstanceFactory.php @@ -8,7 +8,7 @@ use ReflectionProperty; /** - * @template TClass + * @template TClass of object */ final readonly class InstanceFactory { From c1747859d111fdc1101bf96249d19168b196ae4c Mon Sep 17 00:00:00 2001 From: ppelgrims Date: Mon, 13 Oct 2025 21:04:13 +0200 Subject: [PATCH 2/3] Make class pass phpstan --- src/Factories/ImmutableFactory.php | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Factories/ImmutableFactory.php b/src/Factories/ImmutableFactory.php index b6303f5..309dd46 100644 --- a/src/Factories/ImmutableFactory.php +++ b/src/Factories/ImmutableFactory.php @@ -13,7 +13,7 @@ use function iterator_to_array; /** - * @template TClass + * @template TClass of object */ abstract class ImmutableFactory { @@ -30,19 +30,25 @@ final public function __construct( /** * @param array $state - * @return static + * @return static */ public function state(array $state): static { - return new static($this->faker, [...$this->state, ...$state], $this->count); + /** @var static $next */ + $next = new static($this->faker, [...$this->state, ...$state], $this->count); + + return $next; } /** - * @return static + * @return static */ public function times(int $count): static { - return new static($this->faker, $this->state, $count); + /** @var static $next */ + $next = new static($this->faker, $this->state, $count); + + return $next; } /** @@ -66,10 +72,9 @@ private function resolveValue(mixed $value): mixed return $value; } - return match ($value->count > 1) { - true => $value->makeMany(), - default => $value->makeOne(), - }; + return $value->count > 1 + ? $value->makeMany() + : $value->makeOne(); } /** @@ -87,7 +92,7 @@ public function raw(array $attributes = []): array /** * @param array $attributes - * @return array + * @return list> */ public function rawMany(array $attributes = []): array { @@ -107,14 +112,14 @@ public function rawCollection(array $attributes = []): Collection * @param array $attributes * @return TClass */ - public function makeOne(array $attributes = []): mixed + public function makeOne(array $attributes = []): object { return $this->instance($this->raw($attributes)); } /** * @param array $attributes - * @return array + * @return list */ public function makeMany(array $attributes = []): array { @@ -127,6 +132,6 @@ public function makeMany(array $attributes = []): array */ public function makeCollection(array $attributes = []): Collection { - return Collection::times($this->count, fn (): mixed => $this->makeOne($attributes)); + return Collection::times($this->count, fn (): object => $this->makeOne($attributes)); } -} +} \ No newline at end of file From 35c87e7a25cfefa6e0d6f1d2f5ea2c9de35783c6 Mon Sep 17 00:00:00 2001 From: ppelgrims Date: Mon, 13 Oct 2025 21:04:18 +0200 Subject: [PATCH 3/3] Make class pass phpstan --- src/PHPUnit/DataProviders/EnumCase.php | 76 +++++++++++++++++--------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/src/PHPUnit/DataProviders/EnumCase.php b/src/PHPUnit/DataProviders/EnumCase.php index b2c7101..c4eb479 100644 --- a/src/PHPUnit/DataProviders/EnumCase.php +++ b/src/PHPUnit/DataProviders/EnumCase.php @@ -21,17 +21,18 @@ */ final readonly class EnumCase { - /** - * @var array - */ + /** @var TValue */ + public UnitEnum $instance; + + /** @var array */ private array $options; - public function __construct( - /* @var TValue */ - public UnitEnum $instance, - /* @param array ...$options */ - UnitEnum ...$options, - ) { + /** + * @param TValue $instance + * @param TValue ...$options + */ + public function __construct(UnitEnum $instance, UnitEnum ...$options) + { in_array($instance, $options, true) or throw new ValueError('Options should contain the given instance.'); foreach ($options as $option) { @@ -40,6 +41,7 @@ public function __construct( ); } + $this->instance = $instance; $this->options = $options; } @@ -51,51 +53,71 @@ public function differentInstance(): UnitEnum count($this->options) > 1 or throw new LogicException( self::class . ' was configured with a single option and can therefore not return a different instance.', ); - $differentOptions = array_filter($this->options, fn (UnitEnum $option): bool => $option !== $this->instance); + + $differentOptions = array_filter( + $this->options, + fn (UnitEnum $option): bool => $option !== $this->instance, + ); return $differentOptions[array_rand($differentOptions)]; } /** * @param class-string $enumFQCN - * @return iterable>> + * @return iterable}> */ public static function cases(string $enumFQCN): iterable { + /** @var ReflectionEnumUnitCase $case */ foreach (new ReflectionEnum($enumFQCN)->getCases() as $case) { - // @phpstan-ignore generator.valueType - yield "$enumFQCN::$case->name" => [ - new self($case->getValue(), ...$enumFQCN::cases()), - ]; + /** @var TValue $value */ + $value = $case->getValue(); + /** @var array $all */ + $all = $enumFQCN::cases(); + /** @var self $enumCase */ + $enumCase = new self($value, ...$all); + + yield "$enumFQCN::{$case->name}" => [$enumCase]; } } /** * @param TValue ...$options - * @return iterable>> + * @return iterable}> */ public static function options(UnitEnum ...$options): iterable { + /** @var array $options */ foreach ($options as $case) { - yield "$case->name" => [new self($case, ...$options)]; + /** @var TValue $case */ + /** @var self $enumCase */ + $enumCase = new self($case, ...$options); + yield "{$case->name}" => [$enumCase]; } } /** * @param class-string $enumFQCN - * @return iterable>> + * @param TValue ...$except + * @return iterable}> */ public static function except(string $enumFQCN, UnitEnum ...$except): iterable { - $options = array_map(function (ReflectionEnumUnitCase $reflection) use ($except): ?UnitEnum { - $case = $reflection->getValue(); + /** @var class-string $enumFQCN */ + /** @var array $except */ + $options = array_map( + function (ReflectionEnumUnitCase $reflection) use ($except): ?UnitEnum { + $case = $reflection->getValue(); + + return in_array($case, $except, true) ? null : $case; + }, + new ReflectionEnum($enumFQCN)->getCases(), + ); - return match (in_array($case, $except, true)) { - true => null, - false => $case, - }; - }, new ReflectionEnum($enumFQCN)->getCases()); + /** @var array $options */ + /** @var array $filtered */ + $filtered = array_filter($options); - yield from self::options(...array_filter($options)); + yield from self::options(...$filtered); } -} +} \ No newline at end of file