Skip to content

[Core] Refine ExampleFactoryInterface with Templated Object Type#17753

Merged
mpysiak merged 2 commits intoSylius:2.0from
Rafikooo:2.0-example-factory-template
Mar 31, 2025
Merged

[Core] Refine ExampleFactoryInterface with Templated Object Type#17753
mpysiak merged 2 commits intoSylius:2.0from
Rafikooo:2.0-example-factory-template

Conversation

@Rafikooo
Copy link
Copy Markdown
Contributor

@Rafikooo Rafikooo commented Mar 16, 2025

Q A
Branch? 2.0
Bug fix? no
New feature? kinda (DX)
BC breaks? no
Deprecations? no
License MIT

We have already introduced templating in Factories and Fixtures.

Summary by CodeRabbit

  • Refactor

    • Enhanced immutability and type safety across data generation components for more consistent and reliable behavior.
    • Streamlined default configuration handling to ensure correct and predictable outcomes.
  • Bug Fixes

    • Resolved minor issues in configuration logic to prevent discrepancies during entity creation.
  • Documentation

    • Updated inline documentation and annotations to improve clarity and maintainability.
  • Chores

    • Added conflict declarations in composer.json files to manage compatibility with api-platform/jsonld package versions.
    • Introduced a new CONFLICTS.md document to outline specific conflicts related to package dependencies.

@Rafikooo Rafikooo requested review from a team as code owners March 16, 2025 14:25
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 16, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes enhance type safety, immutability, and documentation across many factory classes in the Sylius Core Bundle. Updates include refining PHPDoc annotations with generic type parameters, modifying method signatures to return a generic type, and enforcing constructor dependency immutability by adding the readonly modifier. Several closures in configureOptions methods have been simplified by removing unnecessary parameters, and minor logic corrections and additional import statements have been added to ensure proper configuration handling.

Changes

File(s) Change Summary
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ExampleFactoryInterface.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/AbstractExampleFactory.php
Updated PHPDoc comments in the interface and abstract class to use a generic return type (T), clarifying method contracts.
src/Sylius/Bundle/CoreBundle/Fixture/Factory/AddressExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/AdminUserExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionActionExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionScopeExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ChannelExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/OrderExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/PaymentMethodExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationTypeExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAttributeExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductReviewExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionActionExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionRuleExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShopUserExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxonExampleFactory.php
Updated constructor parameters by adding the readonly modifier, ensuring injected dependencies remain immutable; added or refined generic type annotations via class-level PHPDoc comments.
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php
src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php
Simplified closures in configureOptions methods by removing the unused Options parameter and streamlining default value callbacks.
src/Sylius/Bundle/CoreBundle/Fixture/AbstractResourceFixture.php Updated constructor parameter type hints to specify ExampleFactoryInterface<ResourceInterface> and added a new import statement for ResourceInterface.
composer.json
src/Sylius/Bundle/ApiBundle/composer.json
Added a "conflict" section specifying that the package api-platform/jsonld is in conflict with versions >=4.1.1.
CONFLICTS.md Introduced a new document outlining conflicts related to the composer.json file, particularly with the api-platform/jsonld package.

Poem

I’m a bunny in a code garden, hopping with delight,
Reading freshly minted types that make our code so bright.
Readonly fields and PHPDocs, neat as a row of carrots,
Each change a tiny nudge to banish old debacles and parrots.
With every commit I bound along, stable and quick,
Celebrating refactored code—a true rabbit’s magic trick!


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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 Mar 16, 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

🧹 Nitpick comments (1)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ExampleFactoryInterface.php (1)

24-24: Consider adding explicit return type to the method signature.

The PHPDoc now indicates the return type as T, but the method signature doesn't reflect this change. For consistency and to fully leverage PHP's type system, consider updating the method signature:

- public function create(array $options = []);
+ public function create(array $options = []): T;

This change would align with modern PHP practices and enforce the return type at runtime, not just for static analysis.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f6ee17 and bf1196e.

📒 Files selected for processing (1)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ExampleFactoryInterface.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Static checks / PHP 8.2, Symfony ^6.4
🔇 Additional comments (2)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ExampleFactoryInterface.php (2)

16-16: Excellent improvement to the interface's type safety.

Adding the template annotation @template T of object makes the interface generic, allowing implementers to specify the exact object type they'll be working with. This enhances static analysis capabilities and improves IDE autocomplete support.


20-22: Great enhancement to parameter and return type documentation.

The parameter annotation clearly defines that $options should be an associative array with string keys and mixed values. The return type now properly references the template parameter T instead of a generic object, which significantly improves type safety for implementers.

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

🧹 Nitpick comments (2)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionExampleFactory.php (1)

144-144: Fix PHPDoc annotation typo

There's a typo in the PHPDoc comment: @retrun should be @return.

-    /** @retrun iterable<string> */
+    /** @return iterable<string> */
src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php (1)

297-300: Fix typo in PHPDoc annotation

There's a typo in the PHPDoc annotation: @retrun should be @return.

 /**
  * @param array<string, mixed> $productAttributes
- * @retrun ProductAttributeValueInterface[]
+ * @return ProductAttributeValueInterface[]
  */
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf1196e and f708b72.

📒 Files selected for processing (26)
  • src/Sylius/Bundle/CoreBundle/Fixture/AbstractResourceFixture.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/AbstractExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/AddressExampleFactory.php (4 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/AdminUserExampleFactory.php (3 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionActionExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionExampleFactory.php (4 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionScopeExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ChannelExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/OrderExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/PaymentMethodExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationTypeExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAttributeExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php (15 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductReviewExampleFactory.php (3 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionActionExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionRuleExampleFactory.php (1 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php (3 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShopUserExampleFactory.php (3 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php (2 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php (3 hunks)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxonExampleFactory.php (6 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/Sylius/Bundle/CoreBundle/Fixture/Factory/AbstractExampleFactory.php
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Static checks / PHP 8.3, Symfony ^7.1
  • GitHub Check: Static checks / PHP 8.2, Symfony ^6.4
🔇 Additional comments (78)
src/Sylius/Bundle/CoreBundle/Fixture/Factory/CustomerGroupExampleFactory.php (2)

24-24: Improved type safety with generic interface annotation

The PHPDoc annotation clearly specifies that this factory implements ExampleFactoryInterface<CustomerGroupInterface>, which enhances type safety and makes it explicit what kind of objects this factory creates.


32-32: Enhanced immutability with readonly modifier

Making the $customerGroupFactory property readonly ensures it cannot be modified after initialization, which is a good practice for dependencies injected via the constructor.

src/Sylius/Bundle/CoreBundle/Fixture/AbstractResourceFixture.php (1)

28-32: Improved type safety and immutability in AbstractResourceFixture

The changes enhance this base class in two important ways:

  1. The PHPDoc annotation clearly specifies that $exampleFactory is an ExampleFactoryInterface<object>, improving type safety.
  2. The readonly modifiers on both constructor parameters enforce immutability, ensuring these dependencies cannot be modified after initialization.

These improvements align with modern PHP best practices and provide better guarantees about the class's behavior.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxCategoryExampleFactory.php (3)

24-24: Improved type safety with generic interface annotation

The PHPDoc annotation clearly specifies that this factory implements ExampleFactoryInterface<TaxCategoryInterface>, enhancing type safety by making it explicit what kind of objects this factory creates.


32-32: Enhanced immutability with readonly modifier

Making the $taxCategoryFactory property readonly ensures it cannot be modified after initialization, which is a good practice for dependencies injected via the constructor.


57-57: Removed unused parameter from closure

Good cleanup by removing the unused Options parameter from the closure that generates the default name. This simplifies the code without changing its functionality.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAttributeExampleFactory.php (2)

26-26: Improved type safety with generic interface annotation

The PHPDoc annotation clearly specifies that this factory implements ExampleFactoryInterface<ProductAttributeInterface>, enhancing type safety and making it explicit what kind of objects this factory creates.


38-40: Enhanced immutability with readonly modifiers

Making all constructor-injected dependencies readonly ensures they cannot be modified after initialization:

  • $productAttributeFactory
  • $localeRepository
  • $attributeTypes

This is a good practice that enforces immutability and prevents accidental modifications of these dependencies.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionScopeExampleFactory.php (2)

21-21: Clear type declaration improves code comprehension.

Adding the PHPDoc annotation @implements ExampleFactoryInterface<CatalogPromotionScopeInterface> provides explicit type information about what objects this factory creates. This enhances static analysis capabilities and improves IDE support.


27-27: Good use of readonly modifier for dependency immutability.

Adding the readonly modifier to the injected dependency ensures the property cannot be modified after initialization, which is a good practice for constructor-injected services. This prevents accidental modification and makes the code more robust.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionActionExampleFactory.php (3)

23-23: Clear type declaration enhances static analysis.

The PHPDoc annotation @implements ExampleFactoryInterface<CatalogPromotionActionInterface> explicitly defines what object type this factory creates. This helps static analysis tools and improves developer experience through better IDE support.


29-29: Good use of readonly modifier for dependency immutability.

Adding the readonly modifier to the injected dependency ensures the property cannot be modified after initialization, which is a good practice for constructor-injected services.


62-62: The code correctly accesses the amount from the channel configuration.

This line properly uses $channelConfiguration['amount'] to access the amount value from the current iteration variable in the foreach loop, ensuring the correct data is multiplied by 100 for the fixed discount calculation.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ChannelExampleFactory.php (2)

35-35: Type parameter declaration enhances code clarity.

The PHPDoc annotation @implements ExampleFactoryInterface<ChannelInterface> explicitly defines that this factory creates ChannelInterface objects. This improves static analysis and IDE support.


49-54: Consistent application of readonly modifier improves immutability.

Adding the readonly modifier to all six injected dependencies ensures they cannot be modified after initialization. This is a good practice for maintaining immutability of service dependencies and prevents accidental modifications.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationTypeExampleFactory.php (2)

26-26: Generic type declaration enhances static analysis support.

The PHPDoc annotation @implements ExampleFactoryInterface<ProductAssociationTypeInterface> explicitly defines the return type of this factory, improving static analysis and IDE support.


38-39: Good use of readonly modifier for dependency immutability.

Adding the readonly modifier to both injected dependencies ensures they cannot be modified after initialization, following good practices for dependency immutability.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/OrderExampleFactory.php (2)

44-44: Great addition of generic type parameter!

Adding the @implements ExampleFactoryInterface<OrderInterface> annotation improves type safety by explicitly specifying that this factory creates OrderInterface objects. This aligns well with the PR objectives of refining the ExampleFactoryInterface with templated object types.


47-47: Good use of typed properties

Adding explicit type declarations for the $optionsResolver and $faker properties improves code clarity and type safety. This change is consistent with modern PHP best practices.

Also applies to: 49-49

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingCategoryExampleFactory.php (3)

24-24: Great addition of generic type parameter!

Adding the @implements ExampleFactoryInterface<ShippingCategoryInterface> annotation improves type safety by explicitly specifying that this factory creates ShippingCategoryInterface objects.


32-34: Good use of readonly modifier for dependency

Using the readonly modifier for the $shippingCategoryFactory property enhances immutability, ensuring that the dependency cannot be modified after initialization. This is a good practice for constructor-injected dependencies.


58-59: Clean closure simplification

Removing the unused Options $options parameter from the closure simplifies the code without affecting functionality. Good cleanup!

src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionActionExampleFactory.php (3)

24-24: Great addition of generic type parameter!

Adding the @implements ExampleFactoryInterface<PromotionActionInterface> annotation improves type safety by explicitly specifying that this factory creates PromotionActionInterface objects.


31-32: Good use of type annotation and readonly modifier

Adding the detailed PHPDoc annotation for $promotionActionFactory along with the readonly modifier enhances both type safety and immutability.


63-63:

Details

✅ Verification successful

Potential inconsistency in variable usage

The line is using $channelConfiguration['amount'] for the calculation but assigning to $configuration[$channelCode]['amount']. Consider whether both places should consistently use $channelConfiguration for clarity and to avoid potential bugs.


🏁 Script executed:

#!/bin/bash
# Check how this code is used in the application
rg --type php "PromotionActionExampleFactory" -A 10 -B 10

Length of output: 11775


Clarification on the Variable Usage in PromotionActionExampleFactory

After reviewing the context and how the configuration arrays are populated, it appears that the current implementation is intentional. In the typical pattern used here, the code is iterating over a set of channel configurations—where each iteration provides a $channelCode paired with its corresponding $channelConfiguration. The line in question:

$configuration[$channelCode]['amount'] = (int) ($channelConfiguration['amount'] * 100);

correctly uses $channelConfiguration['amount'] (the raw input) to perform the calculation, while assigning the transformed value to the consolidated $configuration array keyed by $channelCode. This separation of concerns is common (and intentional) when processing configuration data, and no inconsistency was found across similar factory classes in the codebase.

No changes are required at this time.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductOptionExampleFactory.php (2)

27-27: Great addition of generic type parameter!

Adding the @implements ExampleFactoryInterface<ProductOptionInterface> annotation improves type safety by explicitly specifying that this factory creates ProductOptionInterface objects.


40-43: Good use of readonly modifiers for dependencies

Using the readonly modifier for all constructor parameters enhances immutability, ensuring that these dependencies cannot be modified after initialization. This is a good practice that aligns with modern PHP development standards.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductAssociationExampleFactory.php (2)

25-25: Good Type Clarity Enhancement

Adding the @implements ExampleFactoryInterface<ProductAssociationInterface> PHPDoc annotation improves type clarity for both static analysis tools and developers.


32-34: Good Immutability Enhancement

Making the dependency properties readonly is a positive change that enforces immutability, preventing accidental modifications after construction and making the code more robust.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionRuleExampleFactory.php (2)

24-24: Good Type Clarity Enhancement

Adding the @implements ExampleFactoryInterface<PromotionRuleInterface> PHPDoc annotation improves type clarity for static analysis tools and developers.


31-34: Good Immutability and Type Safety Improvements

The PHPDoc annotation for the $promotionRuleFactory parameter adds valuable type information, and making the property readonly enhances immutability. This change aligns with modern PHP best practices.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxRateExampleFactory.php (3)

28-28: Good Type Clarity Enhancement

Adding the @implements ExampleFactoryInterface<TaxRateInterface> PHPDoc annotation improves type clarity for static analysis tools and developers.


41-43: Good Immutability Enhancement

Making the dependency properties readonly enforces immutability, preventing accidental modifications after construction and making the code more robust.


73-74: Good Code Simplification

Removing the unused Options $options parameter from the closure simplifies the code. Since the parameter wasn't being used in the function body, this is a positive cleanup.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/PaymentMethodExampleFactory.php (2)

29-30: Good Type Safety and Interface Implementation

Adding both the PHPDoc annotation @implements ExampleFactoryInterface<PaymentMethodInterface> and explicitly implementing the interface in the class declaration enhances type safety and makes the relationship clearer.


40-42: Good Immutability Enhancement

Making the dependency properties readonly enforces immutability, preventing accidental modifications after construction and making the code more robust.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductReviewExampleFactory.php (6)

20-22: Added improved type imports

These new imports enhance type safety by explicitly importing interfaces for customers, products, and product reviews.


31-31: Enhanced type safety with generic interface implementation

Good addition of the PHPDoc annotation to explicitly specify that this factory creates ProductReviewInterface objects. This improves type safety and code clarity.


38-42: Improved type hints with generics

Enhanced PHPDoc annotations with generic type parameters for repository interfaces provide better type information for static analysis tools.


44-47: Added readonly property modifiers

The addition of the readonly modifier to constructor parameters enhances immutability by ensuring these dependencies cannot be modified after initialization.


76-77: Simplified closure by removing unused parameter

Good refactoring to remove the unused $options parameter from the closure, as it wasn't being used in the implementation.


83-83: Simplified closure by removing unused parameter

Similarly to the previous comment, this is a good clean-up of unused parameters.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShopUserExampleFactory.php (3)

28-28: Enhanced type safety with generic interface implementation

The addition of the PHPDoc annotation explicitly specifies that this factory creates ShopUserInterface objects, improving type safety and code clarity.


41-43: Added readonly property modifiers

The addition of the readonly modifier to constructor parameters enhances immutability by ensuring these dependencies cannot be modified after initialization.


84-84:

Details

✅ Verification successful

Verify the behavior change in customer_group option

The parameter 100 has been removed from LazyOption::randomOneOrNull(). This could potentially change the behavior of how customer groups are randomly selected.


🏁 Script executed:

#!/bin/bash
# Check the implementation of LazyOption::randomOneOrNull to understand if removing the parameter changes the behavior

# First, let's find the implementation of LazyOption class
echo "Looking for LazyOption class implementation..."
rg -l "LazyOption" --type php

# Then, let's look at the randomOneOrNull method
echo "Examining the randomOneOrNull method..."
rg "randomOneOrNull\s*\(" --type php -A 15

Length of output: 8458


Review Comment Verification – No Behavior Change Detected

After reviewing the implementation of LazyOption::randomOneOrNull, we confirm that omitting the explicit 100 has no impact. The method’s signature defaults the $chanceOfRandomOne parameter to 100, so the behavior remains consistent.

  • The default parameter in LazyOption::randomOneOrNull ensures a 100% chance, making the explicit 100 redundant.
  • No changes to the behavior of the customer_group option are observed.
src/Sylius/Bundle/CoreBundle/Fixture/Factory/AddressExampleFactory.php (4)

30-30: Enhanced type safety with generic interface implementation

The addition of the PHPDoc annotation explicitly specifies that this factory creates AddressInterface objects, improving type safety and code clarity.


43-45: Added readonly property modifiers

The addition of the readonly modifier to constructor parameters enhances immutability by ensuring these dependencies cannot be modified after initialization.


63-63: Simplified closure by removing unused parameter

The removal of the unused Options $options parameter from the closure simplifies the code without affecting functionality, as the parameter wasn't used within the function body.


133-133: Improved type documentation for method parameter

Added a PHPDoc comment to properly document the type of the $options parameter as array<string, mixed>, enhancing code clarity and type safety.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/CatalogPromotionExampleFactory.php (4)

22-22: Added ChannelInterface import

This import improves type safety by explicitly importing the interface for channels.


31-31: Enhanced type safety with generic interface implementation

The addition of the PHPDoc annotation explicitly specifies that this factory creates CatalogPromotionInterface objects, improving type safety and code clarity.


41-43: Improved type hints with generics

Enhanced PHPDoc annotations with generic type parameters for repository and factory interfaces provide better type information for static analysis tools.


46-50: Added readonly property modifiers

The addition of the readonly modifier to constructor parameters enhances immutability by ensuring these dependencies cannot be modified after initialization.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/AdminUserExampleFactory.php (3)

27-27: Well-typed implementation of ExampleFactoryInterface.

The new generic type implementation enhances type safety by explicitly declaring that this factory creates AdminUserInterface objects. This improves static analysis and IDE support.


39-44: Great improvement with readonly properties.

Converting the constructor parameters to readonly properties enforces immutability and ensures these dependencies can't be modified after initialization. This promotes safer code by preventing accidental property modifications.


99-99: Improved type documentation for options parameter.

Adding the array type annotation clarifies that the $options parameter is an associative array with string keys and mixed values, enhancing code readability and type checking.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/PromotionExampleFactory.php (4)

21-21: Explicit import of ChannelInterface.

Adding this import improves code clarity and readability, especially when working with generic type annotations.


33-33: Well-typed implementation of ExampleFactoryInterface.

The generic type implementation enhances type safety by explicitly declaring that this factory creates PromotionInterface objects, improving static analysis and IDE support.


41-45: Enhanced type annotations for constructor parameters.

The updated PHPDoc comments with generic type information provide better clarity about the expected types of the repository and factory interfaces, improving code documentation and type safety.


49-54: Great improvement with readonly properties.

Converting the constructor parameters to readonly properties enforces immutability and ensures these dependencies can't be modified after initialization. This promotes safer code and follows modern PHP practices.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ShippingMethodExampleFactory.php (3)

33-33: Well-typed implementation of ExampleFactoryInterface.

The generic type implementation enhances type safety by explicitly declaring that this factory creates ShippingMethodInterface objects, improving static analysis and IDE support.


48-54: Great improvement with readonly properties.

Converting the constructor parameters to readonly properties enforces immutability and ensures these dependencies can't be modified after initialization. This promotes safer code and follows modern PHP practices.


101-101: Simplified closure signature.

Removing the unused Options parameter from the closure simplifies the code and makes it more maintainable. This is a nice cleanup.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/TaxonExampleFactory.php (6)

28-28: Well-typed implementation of ExampleFactoryInterface.

The generic type implementation enhances type safety by explicitly declaring that this factory creates TaxonInterface objects, improving static analysis and IDE support.


37-37: Enhanced type annotation for taxonRepository.

The updated PHPDoc comment with generic type information provides better clarity about the expected type of the repository, improving code documentation and type safety.


41-44: Great improvement with readonly properties.

Converting the constructor parameters to readonly properties enforces immutability and ensures these dependencies can't be modified after initialization. This promotes safer code and follows modern PHP practices.


57-57: Improved type documentation for options parameters.

Adding the array type annotations clarifies that the $options parameters are associative arrays with string keys and mixed values, enhancing code readability and type checking.

Also applies to: 93-93


109-109: Simplified closure signature.

Removing the unused Options parameter from the closure simplifies the code and makes it more maintainable. This is a nice cleanup.


125-125: Improved return type documentation.

Adding the return type annotation for the getLocales method clarifies that it returns an iterable of strings, enhancing code readability and type checking.

src/Sylius/Bundle/CoreBundle/Fixture/Factory/ProductExampleFactory.php (9)

20-20: Good refactoring of import statements

Updating the import to use Sylius\Component\Attribute\Model\AttributeValueInterface instead of the product-specific attribute interface aligns with the overall goal of making the code more generic and less tied to specific implementations.


49-49: Great addition of generic type annotation

Adding @implements ExampleFactoryInterface<ProductInterface> explicitly defines the contract that this factory fulfills, improving type safety and code clarity. This is directly aligned with the PR objective of refining the ExampleFactoryInterface with templated object types.


71-86: Good use of readonly properties for constructor parameters

The addition of the readonly modifier to all constructor parameters is a good practice that enforces immutability, making the code more robust and less prone to unintended modifications. This is a PHP 8.1+ feature that helps prevent accidental property mutations after initialization.


118-123: Good simplification of callback functions

Removing the Options $options parameter from the callbacks for 'name' and 'description' simplifies the code since these functions don't depend on other options.

Also applies to: 137-142


180-180: Good addition of PHPDoc type annotations

Adding specific type annotations for array parameters (array<string, mixed>) and return values (iterable<string>) improves code documentation and type safety, making it easier for developers to understand expected input/output types.

Also applies to: 194-194, 210-210, 253-253, 274-274, 287-287


324-324: Good type clarification for parameter

Using mixed type for the $value parameter in configureProductAttributeValue accurately reflects that this parameter can accept values of different types, improving type clarity.


330-330: Good consistent use of interface constants

Replacing references to ProductAttributeValueInterface::STORAGE_* with AttributeValueInterface::STORAGE_* maintains consistency with the import changes and supports the move toward more generic interfaces.

Also applies to: 346-358


343-343: Good return type clarification

Specifying mixed as the return type for getRandomValueForProductAttribute accurately reflects that this method can return values of different types depending on the attribute's storage type.


104-104:

Details

✅ Verification successful

Verify changes to date generation

The dateTimeBetween call has been modified to remove the second parameter, which changes the date range. Please verify this change won't impact any dependent code that relies on specific date range behavior.


🏁 Script executed:

#!/bin/bash
# Check for usages of dateTimeBetween in tests or similar code
rg -A 2 -B 2 "dateTimeBetween.*-1 week" --type php

Length of output: 644


Final Review Comment: No Impact on Date Range Behavior

The change in ProductExampleFactory.php now uses a single-parameter call to dateTimeBetween('-1 week'), which by default sets the upper bound to "now." Our verification with the grep search revealed that this is the only usage of dateTimeBetween with a "-1 week" argument, and there are no dependent tests or code that rely on an explicitly provided second parameter. As long as the underlying Faker implementation defaults to "now" (which is expected), the behavior remains unchanged.

@Rafikooo Rafikooo force-pushed the 2.0-example-factory-template branch 2 times, most recently from d7b4890 to 0d06966 Compare March 22, 2025 13:51
@probot-autolabeler probot-autolabeler bot added API APIs related issues and PRs. Maintenance CI configurations, READMEs, releases, etc. labels Mar 22, 2025
@Rafikooo Rafikooo force-pushed the 2.0-example-factory-template branch from 58e4d13 to dd118ae Compare March 28, 2025 01:08
@probot-autolabeler probot-autolabeler bot added the Documentation Documentation related issues and PRs - requests, fixes, proposals. label Mar 28, 2025
@Rafikooo Rafikooo force-pushed the 2.0-example-factory-template branch from dd118ae to a296544 Compare March 28, 2025 01:11
@Rafikooo Rafikooo changed the base branch from 2.1 to 2.0 March 28, 2025 01:11
@Rafikooo Rafikooo force-pushed the 2.0-example-factory-template branch from a296544 to dc8b6c0 Compare March 28, 2025 12:25
@mpysiak mpysiak merged commit 3c1b2c0 into Sylius:2.0 Mar 31, 2025
27 checks passed
Wojdylak added a commit that referenced this pull request Mar 31, 2025
…ype" (#17812)

Reverts #17753 as this may break static analysis on end
applications, we will introduce this change in 2.1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API APIs related issues and PRs. Documentation Documentation related issues and PRs - requests, fixes, proposals. Maintenance CI configurations, READMEs, releases, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants