Skip to content

Fix generating random fixtures#18201

Merged
NoResponseMate merged 1 commit intoSylius:2.1from
GSadee:SYL-5148-fix-generating-random-fixtures
Jul 2, 2025
Merged

Fix generating random fixtures#18201
NoResponseMate merged 1 commit intoSylius:2.1from
GSadee:SYL-5148-fix-generating-random-fixtures

Conversation

@GSadee
Copy link
Copy Markdown
Member

@GSadee GSadee commented Jul 2, 2025

Q A
Branch? 2.1
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets
License MIT

Fixes the issue during generating random fixtures:

Sylius\Component\Core\Formatter\StringInflector::nameToCode(): Argument #1 ($value) must be of type string, Closure given

Summary by CodeRabbit

  • Refactor
    • Improved internal code readability and consistency by simplifying how default names are generated for various fixture factories. No changes to functionality or user-facing behavior.

@GSadee GSadee requested a review from a team as a code owner July 2, 2025 04:40
@GSadee GSadee added the Bug Confirmed bugs or bugfixes. label Jul 2, 2025
@GSadee GSadee requested a review from a team as a code owner July 2, 2025 04:40
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jul 2, 2025

Walkthrough

The changes update several factory classes by simplifying the way default values for the 'name' option are assigned in their configureOptions methods. Multi-line anonymous functions are replaced with concise arrow functions, maintaining identical logic and output throughout.

Changes

Files (grouped) Change Summary
.../ChannelExampleFactory.php, .../CustomerGroupExampleFactory.php, .../PaymentMethodExampleFactory.php,
.../ProductAssociationTypeExampleFactory.php, .../ProductAttributeExampleFactory.php,
.../ProductExampleFactory.php, .../ProductOptionExampleFactory.php,
.../ShippingCategoryExampleFactory.php, .../ShippingMethodExampleFactory.php,
.../TaxCategoryExampleFactory.php, .../TaxRateExampleFactory.php Replaced multi-line anonymous functions with single-line arrow functions for 'name' option defaults.

Poem

In fields of code where factories grow,
Rabbits hop and functions flow.
Arrows leap where closures stood,
Concise and clear, as all code should.
With every hop, the syntax shines—
Three magic words, in tidy lines!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 2, 2025

❌ Preview Environment deleted from Bunnyshell

Available commands:

  • 🚀 /bns:deploy to redeploy the environment

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; the i looks unintended and leaks into the generated value:

sprintf('%s #i%d', $options['name'], $i); // → “Size #i1”

Replace #i%d with #%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 $options parameter in the first arrow function

setDefault('name', fn (Options $options): string => ...) never uses the $options argument, so keeping it is unnecessary noise. Omitting it makes the intent clearer while still satisfying OptionsResolver (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 $options parameter in the arrow function.

$options is 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 $options parameter in the arrow function

$options is not referenced inside the closure. You can omit the parameter altogether (Symfony’s OptionsResolver will 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 $options parameter for clarity

Because the closure doesn’t use the Options argument, 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: Redundant setDefault('values', …) call

setDefault('values', null) is immediately overridden by the next setDefault('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 closure

The $options argument is never referenced inside this arrow function, so it can be safely omitted for brevity.
OptionsResolver does 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 parameter

Switching to an arrow function fixes the “Closure given” type error by ensuring the default is a string.
Because the $options argument 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 types

The root bug slipped through because OptionsResolver accepted any value.
Adding type constraints will fail fast if a non-string (e.g. a Closure) 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’s unique() 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 $options parameter – replace with placeholder

The closure does not use the injected Options instance. 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

📥 Commits

Reviewing files that changed from the base of the PR and between 7daece5 and 50e2634.

📒 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 : string return 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 the StringInflector::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 Closure

Switching to a single-line arrow function guarantees the default is evaluated and fixes the StringInflector::nameToCode(): Argument #1 must be of type string, Closure given error 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 good

The 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 into nameToCode()

Using words(3, true) guarantees a single string, eliminating the “Closure/array passed to StringInflector::nameToCode()” type error that triggered the original bug.
Looks correct. ✨


57-60: Double-check faker->words flags in all ExampleFactory files

Automated scans (rg '\$this->faker->words\(' -g '*ExampleFactory.php') returned no instances missing the true flag—but since absence of output can be ambiguous, please manually verify that every call to

$this->faker->words(…, true)

in your *ExampleFactory.php files explicitly passes true as the second argument.

Copy link
Copy Markdown
Contributor

@NoResponseMate NoResponseMate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have it in 2.0

@GSadee
Copy link
Copy Markdown
Member Author

GSadee commented Jul 2, 2025

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 Options $options argument is missing, which may have disappeared with some static analysis improvements on 2.1 🤔

@NoResponseMate NoResponseMate merged commit 9a0273d into Sylius:2.1 Jul 2, 2025
79 of 83 checks passed
@GSadee GSadee deleted the SYL-5148-fix-generating-random-fixtures branch July 2, 2025 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Confirmed bugs or bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants