Fix FakerStubResolver optional() wrapping: nullsafe chain with ?? exa…#113
Merged
Conversation
…mple fallback; skip optional for required/non-nullable fields
cebe
approved these changes
May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix:
optional()wrapping for faker stubs — nullsafe chain +??fallback1. The bug:
optional(weight, default)->dateTimeX()->format()fatal error$faker->optional($weight, $default)is a proxy. On a miss (8 %), it returns$defaultas the result of the next proxied method call — without actually calling it.
The previous strategy embedded
$exampleas the default:On the 8 % miss,
dateTimeThisCenturyreturns the string'2020-03-14 21:42:17',then
->format()is called on it →Call to a member function format() on string.2. The fix: nullsafe chain +
?? $exampleoptional()without a default returnsnullon a miss. All subsequent calls are madenullsafe (
?->) so the chain collapses tonull, then?? $exampleprovides the fallback.Two
str_replacepasses turn the raw stub into the wrapped form:->→?->(makes the whole chain nullsafe)$faker?->→$faker->optional(0.92)->(restores a normal->to$faker, insertsoptional)Generated output — before / after:
3. Condition for skipping optional wrapping
Before: wrapping was skipped for any field without an
example— including nullable ones.After: wrapping is skipped only when a real value is mandatory:
Nullable fields without an
examplenow generatenull8 % of the time (semantically correct).uniqueItemsfields are excluded because an optional fallback would break uniqueness.