Skip to content

Conversation

@teal-bauer
Copy link

Summary

  • Fixes memory exhaustion when using unique()->optional()->method() or unique()->valid()->method() chains
  • Adds explicit optional() and valid() methods to UniqueGenerator that properly wrap the generator chain

Problem

When calling $faker->unique()->optional()->safeEmail(), the UniqueGenerator::__call() handler would:

  1. Call optional() on the underlying generator, returning a ChanceGenerator
  2. Attempt to serialize() this ChanceGenerator for uniqueness tracking
  3. The ChanceGenerator contains a reference to the entire Generator with all providers, causing massive serialized strings and exponential memory growth

Solution

By adding explicit optional() and valid() methods to UniqueGenerator, we ensure proper generator chaining:

  • ChanceGenerator now wraps UniqueGenerator (not the base Generator)
  • When the final method is called, ChanceGenerator delegates back to UniqueGenerator
  • UniqueGenerator then serializes only the final value (e.g., the email string), not generator objects

Test plan

Fixes #1027

…)->optional()

When calling unique()->optional()->method(), the __call handler would
serialize the returned ChanceGenerator object (which contains the entire
Generator with all providers) rather than the final value. This caused
exponential memory growth.

Adding explicit optional() and valid() methods ensures proper generator
chaining - ChanceGenerator now wraps UniqueGenerator, delegating back
for actual value generation while preserving uniqueness tracking.

Fixes FakerPHP#1027
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant