Skip to content

Commit

Permalink
fix psalm error
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Dec 2, 2023
1 parent 95a2060 commit d17627d
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/Set/FromGenerator.php
Expand Up @@ -39,10 +39,6 @@ private function __construct(
\Closure $predicate,
bool $immutable,
) {
if (!$generatorFactory(Random::mersenneTwister) instanceof \Generator) {
throw new \TypeError('Argument 1 must be of type callable(): \Generator');
}

$this->generatorFactory = \Closure::fromCallable($generatorFactory);
$this->size = $size;
$this->predicate = $predicate;
Expand All @@ -58,7 +54,12 @@ private function __construct(
*/
public static function of(callable $generatorFactory): self
{
return new self($generatorFactory, 100, static fn(): bool => true, true);
return new self(
self::guard($generatorFactory),
100,
static fn(): bool => true,
true,
);
}

/**
Expand All @@ -70,7 +71,12 @@ public static function of(callable $generatorFactory): self
*/
public static function mutable(callable $generatorFactory): self
{
return new self($generatorFactory, 100, static fn(): bool => true, false);
return new self(
self::guard($generatorFactory),
100,
static fn(): bool => true,
false,
);
}

/**
Expand Down Expand Up @@ -145,4 +151,20 @@ public function values(Random $random): \Generator
throw new EmptySet;
}
}

/**
* @template A
*
* @param callable(Random): \Generator<A> $generatorFactory
*
* @return callable(Random): \Generator<A>
*/
private static function guard(callable $generatorFactory): callable
{
if (!$generatorFactory(Random::mersenneTwister) instanceof \Generator) {
throw new \TypeError('Argument 1 must be of type callable(): \Generator');
}

return $generatorFactory;
}
}

0 comments on commit d17627d

Please sign in to comment.