Fix generating random fixtures#18201
Conversation
WalkthroughThe changes update several factory classes by simplifying the way default values for the 'name' option are assigned in their Changes
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
❌ Preview Environment deleted from BunnyshellAvailable commands:
|
There was a problem hiding this comment.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php (1)
95-97: Wrong placeholder produces labels like “Size #i1”The format string contains
#i%d; theilooks unintended and leaks into the generated value:sprintf('%s #i%d', $options['name'], $i); // → “Size #i1”Replace
#i%dwith#%d.- $values[sprintf('%s-option#%d', $options['code'], $i)] = sprintf('%s #i%d', $options['name'], $i); + $values[sprintf('%s-option#%d', $options['code'], $i)] = sprintf('%s #%d', $options['name'], $i);
🧹 Nitpick comments (11)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationTypeExampleFactory.php (1)
68-70: Drop the unused$optionsparameter in the first arrow function
setDefault('name', fn (Options $options): string => ...)never uses the$optionsargument, so keeping it is unnecessary noise. Omitting it makes the intent clearer while still satisfyingOptionsResolver(it accepts callables with zero parameters).- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (): string => $this->faker->words(3, true))src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php (1)
99-103: Avoid the unused$optionsparameter in the arrow function.
$optionsis not referenced in the closure body, so keeping it is slightly misleading and may trigger static-analysis “unused parameter” warnings. Removing it makes intent clearer and keeps the signature minimal.- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (): string => $this->faker->words(3, true)) - ->setDefault('description', fn (Options $options): string => $this->faker->sentence()) + ->setDefault('description', fn (): string => $this->faker->sentence())src/Sylius/Bundle/CoreBundle/Fixture/Factory/PaymentMethodExampleFactory.php (1)
88-88: Minor: avoid the unused$optionsparameter in the arrow function
$optionsis not referenced inside the closure. You can omit the parameter altogether (Symfony’sOptionsResolverwill still invoke the callable correctly) or prefix it with an underscore to silence static-analysis warnings.- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (): string => $this->faker->words(3, true))src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php (1)
55-55: Minor: drop the unused$optionsparameter for clarityBecause the closure doesn’t use the
Optionsargument, you can trim it to emphasise that the default is independent of other options:-->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) +->setDefault('name', fn (): string => $this->faker->words(3, true))(Not critical; just a readability nit.)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php (2)
89-90: RedundantsetDefault('values', …)call
setDefault('values', null)is immediately overridden by the nextsetDefault('values', fn …)call, so the first one has no effect and can be removed to avoid confusion.- ->setDefault('values', null)
86-87: Consider ensuring uniqueness for generated names
$this->faker->words(3, true)may yield duplicates across multiple fixture runs. If uniqueness is desirable, prefix with$this->faker->unique().src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAttributeExampleFactory.php (1)
72-76: Remove unused parameter in arrow function for a leaner default closureThe
$optionsargument is never referenced inside this arrow function, so it can be safely omitted for brevity.
OptionsResolverdoes not require the parameter when it isn’t used.- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (): string => $this->faker->words(3, true))src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php (2)
58-61: Closure-type mismatch resolved; you can further streamline by removing unused parameterSwitching to an arrow function fixes the “Closure given” type error by ensuring the default is a string.
Because the$optionsargument is not used, you can omit it for clarity:- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (): string => $this->faker->words(3, true))
55-61: Guard against future type regressions with explicit allowed typesThe root bug slipped through because
OptionsResolveraccepted any value.
Adding type constraints will fail fast if a non-string (e.g. aClosure) is ever injected again:$resolver ->setAllowedTypes('name', 'string') ->setAllowedTypes('code', 'string') ->setAllowedTypes('description', 'string');src/Sylius/Bundle/CoreBundle/Fixture/Factory/ChannelExampleFactory.php (1)
113-118: Consider guaranteeing uniqueness of generated channel names
$this->faker->words(3, true)can occasionally return the same string across multiple calls, leading to duplicate channel names (and therefore duplicate codes) when a large number of fixtures are generated. Leveraging Faker’sunique()modifier removes this risk with negligible overhead:- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (Options $options): string => $this->faker->unique()->words(3, true))src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php (1)
57-57: Unused$optionsparameter – replace with placeholderThe closure does not use the injected
Optionsinstance. Dropping the parameter (or renaming it to_) avoids a misleading “parameter-is-never-used” warning from static analysers.- ->setDefault('name', fn (Options $options): string => $this->faker->words(3, true)) + ->setDefault('name', fn (Options $_): string => $this->faker->words(3, true))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ChannelExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/PaymentMethodExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationTypeExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAttributeExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php(1 hunks)src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: Static checks / PHP 8.3, Symfony ^7.2
- GitHub Check: Static checks / PHP 8.4, Symfony ^7.2
- GitHub Check: Static checks / PHP 8.2, Symfony ^6.4
🔇 Additional comments (6)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php (1)
118-118: LGTM! This fixes the reported type error.The arrow function with explicit
: stringreturn type ensures that the OptionsResolver properly resolves the 'name' option to a string value before it's used in dependent options like the 'code' option on line 120. This prevents theStringInflector::nameToCode()method from receiving a Closure instead of a string, which was the root cause of the reported bug.src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php (1)
55-55: Bug resolved – default value now returns a string instead of a ClosureSwitching to a single-line arrow function guarantees the default is evaluated and fixes the
StringInflector::nameToCode(): Argument #1 must be of type string, Closure givenerror we were seeing.
Looks good.src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php (1)
73-73: LGTM! This fix correctly addresses the Closure type error.The arrow function syntax ensures that the default value is properly resolved as a string rather than potentially passing the Closure object itself to
StringInflector::nameToCode()on line 72. This resolves the reported type error where a Closure was being passed instead of the expected string parameter.src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php (1)
86-87: Arrow-function refactor looks goodThe old multi-line closure has been replaced by an arrow function without changing behaviour or type-safety. ✅
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php (2)
58-58: Good catch – now always feeds a string intonameToCode()Using
words(3, true)guarantees a single string, eliminating the “Closure/array passed toStringInflector::nameToCode()” type error that triggered the original bug.
Looks correct. ✨
57-60: Double-check faker->words flags in all ExampleFactory filesAutomated scans (
rg '\$this->faker->words\(' -g '*ExampleFactory.php') returned no instances missing thetrueflag—but since absence of output can be ambiguous, please manually verify that every call to$this->faker->words(…, true)in your
*ExampleFactory.phpfiles explicitly passestrueas the second argument.
NoResponseMate
left a comment
There was a problem hiding this comment.
Would be nice to have it in 2.0
I've opened it to this branch, because it's fine on 2.0, the problem here is that the |
Fixes the issue during generating random fixtures:
Summary by CodeRabbit