Skip to content

HP-1751 Create configurations for billing types #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

VadymHrechukha
Copy link
Contributor

@VadymHrechukha VadymHrechukha commented Nov 28, 2024

Summary by CodeRabbit

  • New Features

    • Expanded billing functionality with enhanced tariff and pricing management, including automated invoice description generation and dynamic quantity formatting.
    • Introduced mechanisms to support flexible behavior configuration and refined error handling.
    • Added new interfaces and classes for managing tariff types, behaviors, and price definitions.
  • Tests

    • Added comprehensive unit tests covering tariff, billing, and invoice features to ensure robust performance.
  • Chores

    • Upgraded development dependencies and CI workflows for improved stability and build performance.

Copy link
Contributor

coderabbitai bot commented Nov 28, 2024

Walkthrough

This pull request introduces a large set of new classes, interfaces, and exception types across the billing product module. It adds functionality for managing tariff types, price types, quantity formatting, invoice representations, behaviors, and locking mechanisms. New test classes and mock implementations have also been added to support the enhanced functionality. In addition, there are updates to composer dependencies and GitHub workflow action versions.

Changes

File(s) Change Summary
src/product/BillingRegistry.php, src/product/BillingRegistryInterface.php, src/product/TariffTypeDefinition.php, src/product/TariffTypeDefinitionFactory.php, src/product/TariffTypeDefinitionInterface.php Introduced new classes and interfaces for managing the billing registry, tariff type definitions, and related factories.
src/product/InvoiceDescriptionsBuilder.php, src/product/ProductInterface.php, src/product/invoice/RepresentationInterface.php, src/product/invoice/RepresentationCollection.php, src/product/invoice/InvalidRepresentationException.php Added an invoice descriptions builder, a product interface, and invoice representation components.
src/product/Domain/Model/TariffTypeInterface.php, src/product/Domain/Model/Unit/FractionUnitInterface.php, src/product/Domain/Model/Unit/UnitInterface.php, src/product/GTypeInterface.php Added new domain interfaces for tariff types, unit definitions, and group types.
src/product/behavior/BehaviorCollection.php, src/product/behavior/BehaviorCollectionInterface.php, src/product/behavior/BehaviorInterface.php, src/product/behavior/BehaviorNotFoundException.php, src/product/behavior/BehaviorTariffTypeCollection.php, src/product/behavior/TariffTypeBehaviorRegistry.php, src/product/behavior/InvalidBehaviorException.php, src/product/behavior/BehaviorPriceTypeDefinitionCollection.php, src/product/behavior/HasBehaviorsInterface.php, src/product/behavior/Behavior.php Defined new behavior management classes, abstract classes, interfaces, exceptions, and collections for handling billing behaviors and tariff behavior registries.
src/product/quantity/QuantityFormatterInterface.php, src/product/quantity/FractionQuantityData.php, src/product/quantity/QuantityFormatterDefinition.php, src/product/quantity/QuantityFormatterFactory.php, src/product/quantity/InvalidQuantityFormatterException.php, src/product/price/PriceTypeDefinition.php, src/product/price/PriceTypeDefinitionCollection.php, src/product/price/PriceTypeInterface.php, src/product/price/PriceTypeStorage.php, src/product/price/PriceTypeDefinitionCollectionInterface.php, src/product/price/PriceTypeDefinitionFactory.php, src/product/price/PriceTypeDefinitionFactoryInterface.php, src/product/price/PriceTypeDefinitionInterface.php Added new classes, interfaces, collections, and factories for managing price type definitions, quantity formatters, and price storage, along with associated exceptions.
src/product/Exception/AggregateNotDefinedException.php, src/product/Exception/AggregateNotFoundException.php, src/product/Exception/ProductNotDefinedException.php, src/product/Exception/TariffTypeDefinitionNotFoundException.php, src/product/Exception/LockedException.php, src/Exception/LogicException.php Added multiple exception classes for handling aggregate, product, tariff definition errors and locking issues.
composer.json, .github/workflows/behat-tests.yml, .github/workflows/phpunit-tests.yml Updated dev dependencies and upgraded GitHub Actions (checkout and cache) versions.
tests/unit/product/* (e.g., TariffTypeDefinitionTest.php, BillingRegistryTest.php, TestBehavior.php, FakeBehavior.php, DummyTariffType.php, FakeTariffType.php, TariffTypeBehaviorRegistryTest.php, MockTariffType.php) Introduced extensive unit tests and test mocks to verify new functionality in tariff types, billing registry, behaviors, and domain models.
src/product/trait/HasLock.php, src/product/trait/HasLockInterface.php Established a locking mechanism by adding a trait and a corresponding interface to prevent modifications post-locking.

Sequence Diagram(s)

sequenceDiagram
  participant IDB as InvoiceDescriptionsBuilder
  participant BR as BillingRegistry
  participant PT as PriceTypeDefinition
  IDB->>BR: Call priceTypes()
  BR-->>IDB: Return generator of price types
  loop For each price type
    IDB->>PT: Invoke documentRepresentation()
    PT-->>IDB: Return description
  end
  IDB->>IDB: Aggregate and return descriptions array
Loading

Suggested reviewers

  • SilverFire

Poem

I'm a little code rabbit with a hop and a skip,
New classes and tests make my excitement flip!
Tariffs and prices, behaviors so neat,
Locking and invoices—oh, what a treat!
With every commit, my paws do a joyful tap 🐇
Keeping our code garden flourishing in a happy rap!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.

Copy link
Contributor

@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: 21

🧹 Outside diff range and nitpick comments (15)
src/product/ServerProduct.php (2)

1-6: Add class documentation.

Please add PHPDoc documentation to describe the purpose and responsibility of this class within the billing system. This will help other developers understand how ServerProduct fits into the billing type configurations.

 <?php declare(strict_types=1);

 namespace hiqdev\php\billing\product;

+/**
+ * Represents a server product in the billing system.
+ * This class handles billing configuration for server-related products.
+ */
 class ServerProduct implements ProductInterface

5-11: Consider expanding the server product implementation.

The current implementation seems minimal for a server product in a billing system. Consider adding:

  1. Properties to store server specifications (e.g., CPU, RAM, storage)
  2. Methods to handle pricing tiers based on specifications
  3. Integration with the billing type configurations mentioned in the PR title

This will make the class more useful for real-world billing scenarios.

src/product/InvoiceDescriptionsBuilder.php (2)

7-12: Consider adding validation in the constructor.

While the dependency injection is good, consider validating that the registry has price types to fail fast if the registry is empty or invalid.

 public function __construct(BillingRegistry $registry)
 {
+    if (empty($registry->priceTypes())) {
+        throw new \InvalidArgumentException('Registry must contain price types');
+    }
     $this->registry = $registry;
 }

5-6: Consider defining interfaces for better abstraction.

Since this is part of a billing system that other modules might need to extend:

  1. Consider extracting an interface (e.g., InvoiceDescriptionsBuilderInterface)
  2. Consider making the class final if it's not meant to be extended
interface InvoiceDescriptionsBuilderInterface
{
    public function build(): array;
}

final class InvoiceDescriptionsBuilder implements InvoiceDescriptionsBuilderInterface
{
    // ... existing implementation
}
src/product/PriceTypesCollection.php (3)

1-6: Add class documentation.

Please add a PHPDoc block to describe the class purpose, responsibilities, and usage.

 <?php declare(strict_types=1);

 namespace hiqdev\php\billing\product;

+/**
+ * Collection of price type definitions for a tariff.
+ * Manages the creation and storage of different price types (monthly, overuse)
+ * within the context of a tariff configuration.
+ */
 class PriceTypesCollection

7-7: Enhance property type safety and documentation.

The property type could be more specific to ensure type safety.

-    private array $prices = [];
+    /** @var array<string, PriceTypeDefinition> */
+    private array $prices = [];

5-29: Consider adding helper methods for price type management.

The collection would benefit from additional methods to improve its functionality and maintainability.

Consider adding these methods:

  1. getPriceType(string $type): ?PriceTypeDefinition - Retrieve a specific price type
  2. hasPriceType(string $type): bool - Check if a price type exists
  3. getAllPriceTypes(): array - Get all defined price types
  4. removePriceType(string $type): void - Remove a price type

This would make the collection more flexible and easier to work with in different contexts.

src/product/BillingRegistry.php (3)

5-9: Add PHPDoc documentation for class and properties.

The class and its properties would benefit from comprehensive PHPDoc blocks to improve code maintainability and IDE support.

+/**
+ * Registry for managing tariff types and their associated price types.
+ * Once locked, the registry prevents further modifications to ensure data integrity.
+ */
 class BillingRegistry
 {
+    /**
+     * @var TariffType[] Array of registered tariff types
+     */
     private array $tariffTypes = [];
+
+    /**
+     * @var bool Whether the registry is locked for modifications
+     */
     private bool $locked = false;

19-22: Add method to check lock state.

Consider adding a public method to check if the registry is locked, which would be useful for conditional logic in consuming code.

 public function lock(): void
 {
     $this->locked = true;
 }
+
+/**
+ * @return bool Whether the registry is locked for modifications
+ */
+public function isLocked(): bool
+{
+    return $this->locked;
+}

5-32: Consider architectural improvements for dependency injection and testing.

As a central component of the billing system, consider these architectural improvements:

  1. Make the class implement an interface for better dependency injection and testing
  2. Consider adding a factory method for creating pre-configured instances
  3. Add methods to export the configuration for validation or persistence

Example interface:

interface BillingRegistryInterface
{
    public function addTariffType(TariffType $tariffType): void;
    public function lock(): void;
    public function isLocked(): bool;
    public function priceTypes(): \Generator;
    public function export(): array;
}
src/product/PriceTypeDefinition.php (1)

5-48: Consider architectural improvements

A few architectural suggestions to enhance the robustness of this class:

  1. Consider making the class immutable by cloning in builder methods
  2. Consider extracting an interface to define the contract
  3. Consider adding a build() method that returns a PriceType and keeping collection building separate

This would improve thread safety and separation of concerns.

src/product/TariffType.php (2)

39-42: Remove unnecessary method

The withBehaviors method doesn't serve any purpose as it only returns $this. Consider removing it or implementing actual functionality.


5-56: Consider architectural improvements

Several architectural improvements could make the class more robust:

  1. Define a TariffTypeInterface to make the contract explicit
  2. Add getters for accessing the configured state
  3. Make the class immutable after finalization
  4. Add proper exception classes instead of using generic ones

Would you like me to help with implementing these architectural improvements?

tests/unit/product/TariffTypeTest.php (2)

21-22: Use ::class constant instead of string literal

Using string literals for class names makes the test brittle to refactoring. Consider using the ::class constant for better maintainability.

 $tariffType = new TariffType('server');
-$tariffType->ofProduct('ServerProductClass');
+$tariffType->ofProduct(ServerProduct::class);

67-76: Improve helper method documentation and reusability

The helper method could benefit from:

  1. Complete PHPDoc with parameter and return types
  2. Moving to a shared test trait for reuse across test classes
 /**
  * Helper function to access private properties for testing.
+ * 
+ * @param object $object Object to inspect
+ * @param string $propertyName Name of the private property
+ * @return mixed Value of the private property
  */
 private function getPrivateProperty($object, $propertyName)

Consider creating a TestHelperTrait with this method to avoid duplication across test classes.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 54f3d75 and 818174f.

📒 Files selected for processing (9)
  • src/product/BehaviorInterface.php (1 hunks)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/InvoiceDescriptionsBuilder.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
  • src/product/ProductInterface.php (1 hunks)
  • src/product/ServerProduct.php (1 hunks)
  • src/product/TariffType.php (1 hunks)
  • tests/unit/product/TariffTypeTest.php (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/product/BehaviorInterface.php
  • src/product/ProductInterface.php
🔇 Additional comments (2)
src/product/InvoiceDescriptionsBuilder.php (1)

1-6: LGTM! Clean and well-structured class declaration.

The namespace is properly defined and the class name clearly indicates its responsibility.

tests/unit/product/TariffTypeTest.php (1)

11-17: LGTM! Well-structured initialization test

The test properly verifies both the name initialization and the prices collection type.

Comment on lines 7 to 10
public function name(): string
{
// TODO: Implement name() method.
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Implement the required name() method to fulfill the ProductInterface contract

The verification confirms that:

  • ServerProduct implements ProductInterface
  • name(): string is a required interface method
  • No other implementations exist in the codebase to reference
  • The current TODO implementation will cause a runtime error

The method must be properly implemented to return a string value that identifies the server product.

🔗 Analysis chain

Implement the required name() method.

The current TODO implementation will cause a runtime error as it doesn't return a string value as required by the return type declaration. This method is crucial for product identification in the billing system.

Please implement this method before merging. The implementation should return a meaningful name that identifies the server product in the billing context.

Let's verify if there are any existing implementations we can reference:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other implementations of the name() method in the codebase
ast-grep --pattern 'function name(): string { $$$$ }'

Length of output: 55


Script:

#!/bin/bash
# Let's try a broader search for name() method implementations
rg "function name\(\)" -A 3

# Also search for the interface definition to understand the contract
rg "interface.*Product" -A 10

Length of output: 576

Comment on lines 14 to 22
public function build(): array
{
$descriptions = [];
foreach ($this->registry->priceTypes() as $priceType) {
$descriptions[] = $priceType->representInvoice();
}

return $descriptions;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling and consider using array_map.

The build method could benefit from several improvements:

  1. Error handling for potential failures in representInvoice()
  2. Using array_map for a more functional approach
  3. PHPDoc explaining the return format
+/**
+ * Builds an array of invoice descriptions from registered price types.
+ *
+ * @return array<int, mixed> Array of invoice descriptions
+ * @throws \RuntimeException When failed to represent a price type
+ */
 public function build(): array
 {
-    $descriptions = [];
-    foreach ($this->registry->priceTypes() as $priceType) {
-        $descriptions[] = $priceType->representInvoice();
+    try {
+        return array_map(
+            static fn($priceType) => $priceType->representInvoice(),
+            $this->registry->priceTypes()
+        );
+    } catch (\Throwable $e) {
+        throw new \RuntimeException(
+            'Failed to build invoice descriptions: ' . $e->getMessage(),
+            0,
+            $e
+        );
     }
-
-    return $descriptions;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function build(): array
{
$descriptions = [];
foreach ($this->registry->priceTypes() as $priceType) {
$descriptions[] = $priceType->representInvoice();
}
return $descriptions;
}
/**
* Builds an array of invoice descriptions from registered price types.
*
* @return array<int, mixed> Array of invoice descriptions
* @throws \RuntimeException When failed to represent a price type
*/
public function build(): array
{
try {
return array_map(
static fn($priceType) => $priceType->representInvoice(),
$this->registry->priceTypes()
);
} catch (\Throwable $e) {
throw new \RuntimeException(
'Failed to build invoice descriptions: ' . $e->getMessage(),
0,
$e
);
}
}

Comment on lines 17 to 19
foreach ($this->registry->priceTypes() as $priceType) {
$descriptions[] = $priceType->representInvoice();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Integration verification reveals potential issues

The codebase analysis shows a potential mismatch in the implementation. While InvoiceDescriptionsBuilder calls representInvoice() on price types, the actual implementation shows:

  • BillingRegistry::priceTypes() exists but returns a Generator
  • Price types are managed through PriceTypesCollection and PriceTypeDefinition
  • PriceTypeDefinition has invoiceRepresentation() method, but no representInvoice() method
  • No class or interface was found implementing the required representInvoice() method

This suggests that either:

  1. The representInvoice() method is missing from the implementation
  2. There's a naming mismatch between the expected and actual method names
  3. The implementation might be using a different pattern than what's being called
🔗 Analysis chain

Verify integration with dependent components.

Let's ensure the required methods exist in the dependent classes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if BillingRegistry and price types have required methods

# Check BillingRegistry for priceTypes method
ast-grep --pattern 'class BillingRegistry {
  $$$
  public function priceTypes() {
    $$$
  }
  $$$
}'

# Check for classes implementing representInvoice method
ast-grep --pattern 'class $_ {
  $$$
  public function representInvoice() {
    $$$
  }
  $$$
}'

Length of output: 204


Script:

#!/bin/bash
# Let's try a different approach using ripgrep to find these methods

# Search for BillingRegistry class and priceTypes method
echo "=== Searching for BillingRegistry and priceTypes ==="
rg "class BillingRegistry" -A 10
rg "function priceTypes"

# Search for representInvoice method
echo -e "\n=== Searching for representInvoice method ==="
rg "function representInvoice"

# Search for any interface that might define representInvoice
echo -e "\n=== Searching for interfaces that might define representInvoice ==="
rg "interface.*Price" -A 10

Length of output: 4025


Script:

#!/bin/bash
# Let's check for TariffType class and its relationship with representInvoice

# Search for TariffType class/interface
echo "=== Searching for TariffType definition ==="
rg "class TariffType|interface TariffType" -A 10

# Search for files that might contain representInvoice in price or tariff related files
echo -e "\n=== Searching for representInvoice in price/tariff related files ==="
fd -e php | grep -i "price\|tariff" | xargs rg "representInvoice"

# Let's also check if there's an interface that might define this method
echo -e "\n=== Searching for interfaces that might define invoice representation ==="
rg "interface.*Invoice" -A 10

Length of output: 1992


Script:

#!/bin/bash
# Let's check PriceTypesCollection and its relationship with invoice representation

# Search for PriceTypesCollection
echo "=== Searching for PriceTypesCollection ==="
rg "class PriceTypesCollection" -A 10

# Search for any invoice-related methods in price types
echo -e "\n=== Searching for invoice-related methods in price types ==="
rg -i "invoice.*function|function.*invoice" -A 3

# Let's also check if there are any invoice-related traits
echo -e "\n=== Searching for invoice-related traits ==="
rg "trait.*Invoice" -A 10

Length of output: 1407

Comment on lines 25 to 28
public function end(): TariffType
{
// Return to the parent TariffType context
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Implement the end() method.

The end() method is incomplete and will cause a fatal error when called.

The method needs proper implementation to return to the parent TariffType context. Based on the builder pattern suggested by the method name, it likely needs a reference to the parent TariffType:

 class PriceTypesCollection
 {
+    private TariffType $parent;
+
+    public function __construct(TariffType $parent)
+    {
+        $this->parent = $parent;
+    }
+
     // ... other methods ...

     public function end(): TariffType
     {
-        // Return to the parent TariffType context
+        return $this->parent;
     }
 }

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines 9 to 23
public function monthly(string $type): PriceTypeDefinition
{
$priceType = new PriceTypeDefinition();
$this->prices[$type] = $priceType;

return $priceType;
}

public function overuse(string $type): PriceTypeDefinition
{
$priceType = new PriceTypeDefinition();
$this->prices[$type] = $priceType;

return $priceType;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor duplicate code and add validation.

The monthly() and overuse() methods contain identical implementation, which violates the DRY principle. Additionally, there's no validation of the input type or handling of duplicate entries.

Consider refactoring like this:

+    private function createPriceType(string $type): PriceTypeDefinition
+    {
+        if (empty($type)) {
+            throw new \InvalidArgumentException('Price type cannot be empty');
+        }
+        
+        if (isset($this->prices[$type])) {
+            throw new \RuntimeException("Price type '$type' already exists");
+        }
+        
+        $priceType = new PriceTypeDefinition();
+        $this->prices[$type] = $priceType;
+        
+        return $priceType;
+    }
+
     public function monthly(string $type): PriceTypeDefinition
     {
-        $priceType = new PriceTypeDefinition();
-        $this->prices[$type] = $priceType;
-
-        return $priceType;
+        return $this->createPriceType($type);
     }

     public function overuse(string $type): PriceTypeDefinition
     {
-        $priceType = new PriceTypeDefinition();
-        $this->prices[$type] = $priceType;
-
-        return $priceType;
+        return $this->createPriceType($type);
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function monthly(string $type): PriceTypeDefinition
{
$priceType = new PriceTypeDefinition();
$this->prices[$type] = $priceType;
return $priceType;
}
public function overuse(string $type): PriceTypeDefinition
{
$priceType = new PriceTypeDefinition();
$this->prices[$type] = $priceType;
return $priceType;
}
private function createPriceType(string $type): PriceTypeDefinition
{
if (empty($type)) {
throw new \InvalidArgumentException('Price type cannot be empty');
}
if (isset($this->prices[$type])) {
throw new \RuntimeException("Price type '$type' already exists");
}
$priceType = new PriceTypeDefinition();
$this->prices[$type] = $priceType;
return $priceType;
}
public function monthly(string $type): PriceTypeDefinition
{
return $this->createPriceType($type);
}
public function overuse(string $type): PriceTypeDefinition
{
return $this->createPriceType($type);
}

Comment on lines 15 to 19
public function __construct(string $name)
{
$this->name = $name;
$this->prices = new PriceTypesCollection();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add input validation and handle required properties

The constructor should:

  • Validate that name is not empty
  • Either require productClass in constructor or mark it nullable

Apply this diff:

     public function __construct(string $name)
     {
+        if (empty(trim($name))) {
+            throw new \InvalidArgumentException('Tariff name cannot be empty');
+        }
         $this->name = $name;
         $this->prices = new PriceTypesCollection();
     }

Consider also adding productClass to constructor since it appears to be required:

-    private string $productClass;
+    private ?string $productClass = null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function __construct(string $name)
{
$this->name = $name;
$this->prices = new PriceTypesCollection();
}
public function __construct(string $name)
{
if (empty(trim($name))) {
throw new \InvalidArgumentException('Tariff name cannot be empty');
}
$this->name = $name;
$this->prices = new PriceTypesCollection();
}

Comment on lines 33 to 35
$tariffType = new TariffType('server');
$behavior = new OncePerMonthPlanChangeBehavior();
$tariffType->attach($behavior);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add type hints for better type safety

The behavior attachment test should verify that the behavior implements BehaviorInterface. Consider adding type hints and assertions.

 $tariffType = new TariffType('server');
 $behavior = new OncePerMonthPlanChangeBehavior();
+$this->assertInstanceOf(BehaviorInterface::class, $behavior);
 $tariffType->attach($behavior);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$tariffType = new TariffType('server');
$behavior = new OncePerMonthPlanChangeBehavior();
$tariffType->attach($behavior);
$tariffType = new TariffType('server');
$behavior = new OncePerMonthPlanChangeBehavior();
$this->assertInstanceOf(BehaviorInterface::class, $behavior);
$tariffType->attach($behavior);

Comment on lines 50 to 52
$priceType = $prices->monthly('support_time');
$priceType->unit('hour')->description('Monthly fee for support time');
$priceType->end();
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add assertions for price type configuration

The test configures the price type but doesn't verify if the unit and description were set correctly.

 $priceType = $prices->monthly('support_time');
 $priceType->unit('hour')->description('Monthly fee for support time');
 $priceType->end();
+
+$this->assertEquals('hour', $this->getPrivateProperty($priceType, 'unit'));
+$this->assertEquals('Monthly fee for support time', $this->getPrivateProperty($priceType, 'description'));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$priceType = $prices->monthly('support_time');
$priceType->unit('hour')->description('Monthly fee for support time');
$priceType->end();
$priceType = $prices->monthly('support_time');
$priceType->unit('hour')->description('Monthly fee for support time');
$priceType->end();
$this->assertEquals('hour', $this->getPrivateProperty($priceType, 'unit'));
$this->assertEquals('Monthly fee for support time', $this->getPrivateProperty($priceType, 'description'));

Comment on lines 57 to 65
public function testEndLocksTariffType(): void
{
$tariffType = new TariffType('server');
$tariffType->end();

// Assuming TariffType has a `locked` private property
$isLocked = $this->getPrivateProperty($tariffType, 'locked');
$this->assertTrue($isLocked, 'TariffType should be locked after calling end().');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add test cases for locked state behavior

The test verifies that the tariff type is locked but doesn't test that operations after locking throw exceptions. Consider adding test cases for operations attempted after locking.

 $tariffType = new TariffType('server');
 $tariffType->end();

-// Assuming TariffType has a `locked` private property
 $isLocked = $this->getPrivateProperty($tariffType, 'locked');
 $this->assertTrue($isLocked, 'TariffType should be locked after calling end().');
+
+$this->expectException(\RuntimeException::class);
+$tariffType->ofProduct(ServerProduct::class);

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (4)
tests/unit/product/InvoiceDescriptionsBuilderTest.php (4)

19-22: Consider extracting hardcoded values to constants or data providers.

The test contains hardcoded values in closures ($100, $50). This makes the test less maintainable and harder to update if these values need to change.

Consider refactoring like this:

+ private const MONTHLY_SUPPORT_FEE = 100;
+ private const OVERUSE_SUPPORT_FEE = 50;

- ->invoiceRepresentation(function () {
-     return 'Invoice for support_time (monthly): $100';
- })
+ ->invoiceRepresentation(function () {
+     return sprintf('Invoice for support_time (monthly): $%d', self::MONTHLY_SUPPORT_FEE);
+ })

Also applies to: 27-30


11-35: Consider breaking down the test setup.

The test setup is quite complex with multiple configurations. Consider breaking it down into smaller, focused test methods or using data providers.

You could split this into separate test methods:

  • testMonthlyInvoiceDescription()
  • testOveruseInvoiceDescription()

38-40: Add assertions for registry state.

The test should verify that the registry is properly configured and locked.

Consider adding these assertions:

 $billingRegistry->addTariffType($serverTariffType);
 $billingRegistry->lock();
+$this->assertTrue($billingRegistry->isLocked(), 'Registry should be locked');
+$this->assertCount(1, $billingRegistry->getTariffTypes(), 'Registry should contain exactly one tariff type');

47-60: Consider adding more robust assertions.

While the current assertions verify the basic functionality, consider adding:

  • Null/empty checks
  • Structure validation instead of exact string matches
  • Edge case handling

Example additions:

+$this->assertNotEmpty($invoiceDescriptions, 'Invoice descriptions should not be empty');
+$this->assertMatchesRegularExpression(
+    '/Invoice for support_time \(monthly\): \$\d+/',
+    $invoiceDescriptions[0],
+    'Monthly invoice should match expected format'
+);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 818174f and 7aa8fcc.

📒 Files selected for processing (1)
  • tests/unit/product/InvoiceDescriptionsBuilderTest.php (1 hunks)
🔇 Additional comments (2)
tests/unit/product/InvoiceDescriptionsBuilderTest.php (2)

1-8: LGTM! Class declaration follows best practices.

The class is properly set up with strict types, correct namespace, and proper TestCase extension.


14-14: Verify the external class reference.

The class \hiapi\legacy\lib\billing\price\suggester\device\ServerPricesSuggester is referenced but might not be available in the test environment.

Comment on lines 9 to 68
public function testInvoiceDescriptionsBuilderWithRealClasses()
{
// Create TariffType with real prices and behaviors
$serverTariffType = (new TariffType('server'))
->ofProduct(ServerProduct::class)
->setPricesSuggester(\hiapi\legacy\lib\billing\price\suggester\device\ServerPricesSuggester::class)
->withPrices()
->monthly('support_time')
->unit('hour')
->description('Monthly fee for support time')
->quantityFormatter(MonthlyQuantityFormatter::class)
->invoiceRepresentation(function () {
return 'Invoice for support_time (monthly): $100';
})
->end()
->overuse('support_time')
->unit('hour')
->description('Support time overuse')
->quantityFormatter(HourBasedOveruseQuantityFormatter::class)
->invoiceRepresentation(function () {
return 'Invoice for support_time (overuse): $50';
})
->end()
->end() // Returns control to TariffType
->withBehaviors()
->attach(new OncePerMonthPlanChangeBehavior())
->end();

// Create BillingRegistry and add the TariffType
$billingRegistry = new BillingRegistry();
$billingRegistry->addTariffType($serverTariffType);
$billingRegistry->lock();

// Build invoice descriptions
$builder = new InvoiceDescriptionsBuilder($billingRegistry);
$invoiceDescriptions = $builder->build();

// Verify the results
$this->assertIsArray($invoiceDescriptions, 'build() should return an array of invoice descriptions.');
$this->assertCount(2, $invoiceDescriptions, 'There should be 2 invoice descriptions generated.');

$this->assertSame(
'Invoice for support_time (monthly): $100',
$invoiceDescriptions[0],
'The first description should match the expected monthly invoice description.'
);

$this->assertSame(
'Invoice for support_time (overuse): $50',
$invoiceDescriptions[1],
'The second description should match the expected overuse invoice description.'
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Based on the implementation findings, I can now provide the final response:

Additional test scenarios needed for InvoiceDescriptionsBuilder

The current test coverage is insufficient. Based on the implementation, add test cases for:

  • Attempting to build descriptions with an unlocked registry (verify proper handling)
  • Empty registry (no tariff types added)
  • Registry with multiple tariff types
  • Registry throwing RuntimeException when attempting modifications after being locked

The implementation shows that BillingRegistry has important validation logic that should be covered in the tests, particularly around the locking mechanism and empty states.

🔗 Analysis chain

Verify test coverage for InvoiceDescriptionsBuilder.

The class only tests the happy path with valid inputs. Consider adding tests for:

  • Invalid configurations
  • Empty registry
  • Unlocked registry

Let me gather more information about the test coverage by checking the actual implementation of InvoiceDescriptionsBuilder and its usage.


Let me check the actual implementation to understand what scenarios need testing.


Let me check the BillingRegistry implementation since it's a dependency of InvoiceDescriptionsBuilder.


Let me check the BillingRegistry implementation directly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for other test files related to InvoiceDescriptionsBuilder
fd -e php -g "*InvoiceDescriptionsBuilder*Test.php"

# Check for test methods in this file
ast-grep --pattern 'function test[A-Za-z]*\(\)'

Length of output: 157


Script:

#!/bin/bash
# Find the implementation file
fd -e php InvoiceDescriptionsBuilder.php

# Check for usage of InvoiceDescriptionsBuilder in the codebase
rg "InvoiceDescriptionsBuilder" -l

# Look for the implementation to understand what scenarios need testing
ast-grep --pattern 'class InvoiceDescriptionsBuilder'

Length of output: 273


Script:

#!/bin/bash
# Check the implementation of InvoiceDescriptionsBuilder
cat src/product/InvoiceDescriptionsBuilder.php

# Look for any error handling or validation in the implementation
rg -A 5 "throw|assert" src/product/InvoiceDescriptionsBuilder.php

Length of output: 615


Script:

#!/bin/bash
# Find and check BillingRegistry implementation
fd -e php BillingRegistry.php

# Look for validation or error handling in BillingRegistry
rg -A 5 "throw|assert|Exception" --type php BillingRegistry

Length of output: 181


Script:

#!/bin/bash
# Check BillingRegistry implementation
cat src/product/BillingRegistry.php

# Look for any test files related to BillingRegistry
fd -e php -g "*BillingRegistry*Test.php"

Length of output: 805

Copy link
Contributor

@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: 2

🧹 Outside diff range and nitpick comments (3)
src/product/PriceTypeDefinition.php (1)

43-47: Improve the end() method implementation

The current implementation has several architectural issues:

  1. It creates a new empty collection instead of returning to the parent collection
  2. No validation of required properties
  3. The state isn't properly locked after finalization

Consider these architectural improvements:

  1. Add a reference to the parent collection
  2. Validate all required properties before finalization
  3. Implement state locking to prevent modifications after end() is called
  4. Return the parent collection for continued builder chain

Example implementation structure:

class PriceTypeDefinition
{
    private PriceTypesCollection $parent;
    private bool $isFinalized = false;

    public function __construct(PriceTypesCollection $parent)
    {
        $this->parent = $parent;
    }

    private function ensureNotFinalized(): void
    {
        if ($this->isFinalized) {
            throw new \RuntimeException('Cannot modify finalized price type definition');
        }
    }

    public function end(): PriceTypesCollection
    {
        $this->validate();
        $this->isFinalized = true;
        return $this->parent;
    }

    private function validate(): void
    {
        // Validate all required properties
    }
}
src/product/TariffType.php (1)

41-44: Implement withBehaviors method

The method currently returns self without any implementation. Consider adding behavior validation or initialization logic.

tests/unit/product/InvoiceDescriptionsBuilderTest.php (1)

27-29: Simplify representation logic

The document representation setup is complex and might be better extracted into a separate helper method or fixture.

Consider creating a helper method to set up document representations to improve test readability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7aa8fcc and d8d3535.

📒 Files selected for processing (6)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/BillingRegistryInterface.php (1 hunks)
  • src/product/DocumentRepresentationInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/TariffType.php (1 hunks)
  • tests/unit/product/InvoiceDescriptionsBuilderTest.php (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/product/DocumentRepresentationInterface.php
  • src/product/BillingRegistryInterface.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/BillingRegistry.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/TariffType.php

[failure] 11-11: UndefinedClass
src/product/TariffType.php:11:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Product does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/TariffType.php:23:31: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Product does not exist (see https://psalm.dev/019)

🔇 Additional comments (11)
src/product/PriceTypeDefinition.php (3)

5-14: Add class-level documentation and property initialization

The class lacks PHPDoc documentation and property initialization, which could lead to undefined property access.


15-41: Add input validation and method documentation

The builder methods need input validation and PHPDoc documentation. The formatter and representation classes should be validated.


49-52: Clarify the purpose of documentRepresentation method

The documentRepresentation method appears to be a no-op as it simply returns $this without any state changes. If this is a placeholder for future implementation, it should be documented.

Let's check if this method is used in the codebase:

Consider adding documentation to clarify the method's purpose:

+    /**
+     * Placeholder for document representation configuration.
+     * TODO: Implement document representation logic
+     *
+     * @return self
+     */
     public function documentRepresentation(): self
     {
         return $this;
     }
src/product/TariffType.php (7)

30-34: ⚠️ Potential issue

Implement price suggester functionality

The method contains only a placeholder comment. This could lead to unexpected behavior.

Would you like me to help implement the price suggester validation and storage functionality?


46-51: 🛠️ Refactor suggestion

Add validation for behavior attachment

The method should validate behavior uniqueness and compatibility.

     public function attach(BehaviorInterface $behavior): self
     {
+        $behaviorClass = get_class($behavior);
+        foreach ($this->behaviors as $existingBehavior) {
+            if ($behaviorClass === get_class($existingBehavior)) {
+                throw new \InvalidArgumentException("Behavior of type '$behaviorClass' is already attached");
+            }
+        }
         $this->behaviors[] = $behavior;
 
         return $this;
     }

53-57: ⚠️ Potential issue

Implement state validation and locking

The end method needs proper implementation for validation and state locking.

-    public function end(): self
+    public function finalize(): self
     {
-        // Validate the TariffType and lock its state
+        if ($this->product === null) {
+            throw new \RuntimeException('Product must be set before finalizing');
+        }
+        if ($this->prices->isEmpty()) {
+            throw new \RuntimeException('At least one price type must be defined');
+        }
+        $this->locked = true;
         return $this;
     }

15-15: 🛠️ Refactor suggestion

Add type hint for behaviors array

The behaviors array should have a proper type hint to ensure type safety.

-    private array $behaviors = [];
+    /** @var BehaviorInterface[] */
+    private array $behaviors = [];

Likely invalid or redundant comment.


17-21: 🛠️ Refactor suggestion

Add name validation in constructor

The constructor should validate that the name is not empty.

     public function __construct(string $name)
     {
+        if (empty(trim($name))) {
+            throw new \InvalidArgumentException('Tariff name cannot be empty');
+        }
         $this->name = $name;
         $this->prices = new PriceTypesCollection();
     }

Likely invalid or redundant comment.


36-39: 🛠️ Refactor suggestion

Add validation in withPrices method

The method should validate that the product is set before allowing price manipulation.

     public function withPrices(): PriceTypesCollection
     {
+        if ($this->product === null) {
+            throw new \RuntimeException('Product must be set before configuring prices');
+        }
         return $this->prices;
     }

Likely invalid or redundant comment.


23-28: 🛠️ Refactor suggestion

Add product validation in ofProduct method

The method should validate that the product is not null.

     public function ofProduct(Product $product): self
     {
+        if ($product === null) {
+            throw new \InvalidArgumentException('Product cannot be null');
+        }
         $this->product = $product;
 
         return $this;
     }

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 23-23: UndefinedClass
src/product/TariffType.php:23:31: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Product does not exist (see https://psalm.dev/019)

tests/unit/product/InvoiceDescriptionsBuilderTest.php (1)

12-68: 🛠️ Refactor suggestion

Enhance test coverage with additional scenarios

The current test only covers the happy path. Consider adding test cases for:

  • Invalid configurations
  • Empty registry
  • Unlocked registry
  • Registry with multiple tariff types

Would you like me to help generate additional test cases?

// Create TariffType with real prices and behaviors
$serverTariffType = (new TariffType('server'))
->ofProduct(ServerProduct::class)
->setPricesSuggester(\hiapi\legacy\lib\billing\price\suggester\device\ServerPricesSuggester::class)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Legacy class ServerPricesSuggester appears to be missing

The class \hiapi\legacy\lib\billing\price\suggester\device\ServerPricesSuggester is referenced in the test but doesn't exist in the codebase. This could lead to test failures. You should either:

  • Move the actual class implementation to the specified legacy path
  • Update the test to use a mock or a different existing price suggester
  • Update the namespace if the class has been moved to a new location
🔗 Analysis chain

Verify class existence in test

The test uses ServerPricesSuggester from a legacy path. Consider moving this to a more maintainable location or mocking it for tests.


Let me check if this class actually exists in the legacy path or if it's just referenced in the test.


Let me try one more search to find any references or implementations of this class in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the legacy suggester class exists
rg -l "ServerPricesSuggester" 

Length of output: 83


Script:

#!/bin/bash
# Check for the actual class file in the legacy path
fd -e php ServerPricesSuggester

# Also search for any class definition
ast-grep --pattern 'class ServerPricesSuggester'

Length of output: 84


Script:

#!/bin/bash
# Search for any file containing the full namespace path
rg -l "hiapi.legacy.lib.billing.price.suggester.device.ServerPricesSuggester"

# Search with different path separators
rg -l "hiapi/legacy/lib/billing/price/suggester/device/ServerPricesSuggester"

# Get context around the usage in the test file
rg -C 5 "ServerPricesSuggester" tests/unit/product/InvoiceDescriptionsBuilderTest.php

Length of output: 885

Copy link
Contributor

@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: 4

🧹 Outside diff range and nitpick comments (2)
src/product/PriceTypesCollection.php (1)

7-7: Initialize prices array with type hint.

Add a type hint to the prices array for better type safety and code readability.

-    private array $prices = [];
+    /** @var array<string, PriceTypeDefinition> */
+    private array $prices = [];
src/product/PriceTypeDefinition.php (1)

9-15: Initialize properties with default values.

Properties should be initialized to prevent undefined property access.

-    private string $unit;
+    private string $unit = '';

-    private string $description;
+    private string $description = '';

-    private string $quantityFormatter;
+    private string $quantityFormatter = '';

-    private string $invoiceRepresentation;
+    private string $invoiceRepresentation = '';
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d8d3535 and cbc66f2.

📒 Files selected for processing (3)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
  • src/product/TariffType.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/TariffType.php

[failure] 11-11: UndefinedClass
src/product/TariffType.php:11:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Product does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/TariffType.php:23:31: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Product does not exist (see https://psalm.dev/019)

src/product/PriceTypeDefinition.php

[failure] 55-55: UndefinedClass
src/product/PriceTypeDefinition.php:55:47: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 57-57: UndefinedClass
src/product/PriceTypeDefinition.php:57:20: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 60-60: UndefinedClass
src/product/PriceTypeDefinition.php:60:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

🔇 Additional comments (4)
src/product/PriceTypesCollection.php (1)

21-24: LGTM!

The helper method is well-designed, encapsulating the creation logic and maintaining the parent-child relationship.

src/product/TariffType.php (2)

46-51: 🛠️ Refactor suggestion

Add validation for behavior attachment.

The method should validate behavior uniqueness and compatibility.

     public function attach(BehaviorInterface $behavior): self
     {
+        $behaviorClass = get_class($behavior);
+        foreach ($this->behaviors as $existingBehavior) {
+            if ($behaviorClass === get_class($existingBehavior)) {
+                throw new \InvalidArgumentException("Behavior of type '$behaviorClass' is already attached");
+            }
+        }
         $this->behaviors[] = $behavior;
 
         return $this;
     }

Likely invalid or redundant comment.


53-57: ⚠️ Potential issue

Implement tariff validation and state locking.

The end method needs proper validation of required properties and state locking mechanism.

     public function end(): self
     {
-        // Validate the TariffType and lock its state
+        if (!isset($this->product)) {
+            throw new \RuntimeException('Product must be set before finalizing');
+        }
+        if (empty($this->prices)) {
+            throw new \RuntimeException('At least one price type must be defined');
+        }
         return $this;
     }

Likely invalid or redundant comment.

src/product/PriceTypeDefinition.php (1)

49-53: ⚠️ Potential issue

Add validation in end method.

The end method should validate that all required properties are set before returning to parent.

     public function end(): PriceTypesCollection
     {
-        // Validate the PriceType and lock its state
+        $requiredProps = ['unit', 'description', 'quantityFormatter', 'invoiceRepresentation'];
+        foreach ($requiredProps as $prop) {
+            if (empty($this->$prop)) {
+                throw new \RuntimeException("$prop is required but not set");
+            }
+        }
         return $this->parent;
     }

Likely invalid or redundant comment.

Comment on lines 13 to 19
public function monthly(string $type): PriceTypeDefinition
{
$priceType = $this->createPriceTypeDefinition();
$this->prices[$type] = $priceType;

return $priceType;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for duplicate price types.

The monthly and overuse methods should validate that the price type doesn't already exist to prevent overwriting.

     public function monthly(string $type): PriceTypeDefinition
     {
+        if (isset($this->prices[$type])) {
+            throw new \InvalidArgumentException("Price type '$type' already exists");
+        }
         $priceType = $this->createPriceTypeDefinition();
         $this->prices[$type] = $priceType;
 
         return $priceType;
     }

Apply the same validation to the overuse method.

Also applies to: 26-32

Comment on lines 30 to 34
public function setPricesSuggester(string $suggesterClass): self
{
// Validate or store the suggester class
return $this;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Implement price suggester functionality.

The method contains only a placeholder comment. This could lead to unexpected behavior.

Would you like me to help implement the price suggester validation and storage functionality?


namespace hiqdev\php\billing\product;

use hiqdev\billing\registry\product\Product;
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

The Product class import path is incorrect

The import statement is using hiqdev\billing\registry\product\Product, but based on the codebase analysis, the actual namespace for product-related classes is hiqdev\php\billing\product. The Product class itself is not found, but there is a ProductInterface and ServerProduct in this namespace.

  • Change the import in src/product/TariffType.php from:
    use hiqdev\billing\registry\product\Product;
    to either:
    use hiqdev\php\billing\product\ProductInterface;
    or
    use hiqdev\php\billing\product\ServerProduct;
    depending on which one is needed in the context.
🔗 Analysis chain

Fix undefined Product class import.

The static analysis indicates that the Product class doesn't exist in the specified namespace.

Also applies to: 11-11


Let me gather more information about the Product class and its namespace.


Let me try one more search to find any potential Product class definition in vendor directories or related repositories.


Let me check the actual namespace of the found Product-related files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Product class definition
rg -t php "class Product"

# Search for Product usage
rg -t php "use.*Product"

Length of output: 231


Script:

#!/bin/bash
# Search for the complete namespace path
fd . -e php -x grep -l "namespace.*billing\\registry\\product"

# Search for any references to Product class usage
rg -t php "Product::" -A 2
rg -t php "instanceof Product" -A 2

Length of output: 474


Script:

#!/bin/bash
# Search for composer.json to check dependencies
cat composer.json

# Search for any PHP files containing Product in their name
fd -e php -g '*Product*.php'

Length of output: 3040


Script:

#!/bin/bash
# Check the namespace declarations in the found files
cat src/product/ProductInterface.php
cat src/product/ServerProduct.php

Length of output: 423

Comment on lines 55 to 98
public function documentRepresentation(): InvoiceRepresentationCollection
{
return new InvoiceRepresentationCollection($this);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Missing required dependency for invoice and measure classes

The classes InvoiceRepresentationCollection and RcpTrafCollector are referenced from the hiqdev\billing\registry namespace, but there's no corresponding dependency in composer.json that would provide these classes. This appears to be a missing dependency issue rather than undefined classes.

  • Add the required dependency that provides the hiqdev\billing\registry namespace to composer.json
  • The dependency is likely a separate package from the same vendor (hiqdev)
🔗 Analysis chain

Fix undefined classes and add method documentation.

The static analysis indicates that InvoiceRepresentationCollection and RcpTrafCollector classes don't exist in the specified namespaces.

Also applies to: 60-63

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for the undefined classes
rg -t php "class (InvoiceRepresentationCollection|RcpTrafCollector)"

# Search for their usage
rg -t php "use.*(InvoiceRepresentationCollection|RcpTrafCollector)"

Length of output: 245


Script:

#!/bin/bash
# Check if the class exists in the vendor directory
fd -t f "InvoiceRepresentationCollection.php" 

# Check for any references to the namespace
rg -t php "hiqdev\\\\billing\\\\registry\\\\invoice"

# Check for RcpTrafCollector references
rg -t php "RcpTrafCollector"

Length of output: 594


Script:

#!/bin/bash
# Check for the measure namespace and RcpTrafCollector in vendor or source
fd -t f "RcpTrafCollector.php"

# Look for other classes in the measure namespace
rg -t php "hiqdev\\\\billing\\\\registry\\\\measure"

# Check composer.json for relevant dependencies
cat composer.json

Length of output: 3159

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 55-55: UndefinedClass
src/product/PriceTypeDefinition.php:55:47: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 57-57: UndefinedClass
src/product/PriceTypeDefinition.php:57:20: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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: 2

🧹 Outside diff range and nitpick comments (2)
src/product/PriceTypeDefinition.php (2)

11-17: Initialize properties with default values.

Properties should be initialized to prevent undefined property access.

-    private Unit $unit;
+    private ?Unit $unit = null;

-    private string $description;
+    private string $description = '';

-    private string $quantityFormatter;
+    private string $quantityFormatter = '';
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 11-11: UndefinedClass
src/product/PriceTypeDefinition.php:11:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Unit does not exist (see https://psalm.dev/019)


[failure] 17-17: UndefinedClass
src/product/PriceTypeDefinition.php:17:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


45-50: Remove or implement commented code.

The commented out invoiceRepresentation method should either be implemented or removed.

If the method is needed, implement it. Otherwise, remove the commented code to maintain cleanliness.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between cbc66f2 and 1e3d171.

📒 Files selected for processing (3)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/BillingRegistry.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/PriceTypeDefinition.php

[failure] 11-11: UndefinedClass
src/product/PriceTypeDefinition.php:11:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Unit does not exist (see https://psalm.dev/019)


[failure] 17-17: UndefinedClass
src/product/PriceTypeDefinition.php:17:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 19-19: UndefinedClass
src/product/PriceTypeDefinition.php:19:80: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Type does not exist (see https://psalm.dev/019)


[failure] 21-21: UndefinedClass
src/product/PriceTypeDefinition.php:21:44: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:26: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Unit does not exist (see https://psalm.dev/019)


[failure] 58-58: UndefinedClass
src/product/PriceTypeDefinition.php:58:47: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 63-63: UndefinedClass
src/product/PriceTypeDefinition.php:63:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)


[failure] 68-68: UndefinedClass
src/product/PriceTypeDefinition.php:68:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Type does not exist (see https://psalm.dev/019)

src/product/PriceTypesCollection.php

[failure] 15-15: UndefinedClass
src/product/PriceTypesCollection.php:15:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Type does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/PriceTypesCollection.php:23:48: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Type does not exist (see https://psalm.dev/019)

🔇 Additional comments (3)
src/product/PriceTypeDefinition.php (3)

5-7: ⚠️ Potential issue

Add missing dependency for billing registry classes.

The static analysis indicates missing classes from the hiqdev\billing\registry namespace.

Add the required dependency to your composer.json:

{
    "require": {
+        "hiqdev/billing-registry": "^1.0"
    }
}

Likely invalid or redundant comment.


52-56: 🛠️ Refactor suggestion

Add validation in end() method.

The end() method should validate that all required properties are set before returning.

     public function end(): PriceTypesCollection
     {
-        // Validate the PriceType and lock its state
+        if ($this->unit === null) {
+            throw new \RuntimeException('Unit must be set');
+        }
+        if (empty($this->description)) {
+            throw new \RuntimeException('Description must be set');
+        }
+        if (empty($this->quantityFormatter)) {
+            throw new \RuntimeException('Quantity formatter must be set');
+        }
         return $this->parent;
     }

Likely invalid or redundant comment.


63-66: Add implementation for measuredWith method.

The method currently returns without any implementation.

Would you like me to help implement this method based on the usage patterns in the codebase?

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 63-63: UndefinedClass
src/product/PriceTypeDefinition.php:63:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

Comment on lines 15 to 21
public function monthly(Type $type): PriceTypeDefinition
{
$priceType = $this->createPriceTypeDefinition($type);
$this->prices[$type->toTypeName()] = $priceType;

return $priceType;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor duplicate code in monthly() and overuse() methods.

The methods contain identical implementation which violates the DRY principle.

Consider extracting the common logic:

+    private function addPriceType(Type $type): PriceTypeDefinition 
+    {
+        if (isset($this->prices[$type->toTypeName()])) {
+            throw new \InvalidArgumentException("Price type '{$type->toTypeName()}' already exists");
+        }
+        $priceType = $this->createPriceTypeDefinition($type);
+        $this->prices[$type->toTypeName()] = $priceType;
+        return $priceType;
+    }
+
     public function monthly(Type $type): PriceTypeDefinition
     {
-        $priceType = $this->createPriceTypeDefinition($type);
-        $this->prices[$type->toTypeName()] = $priceType;
-        return $priceType;
+        return $this->addPriceType($type);
     }

     public function overuse(Type $type): PriceTypeDefinition
     {
-        $priceType = $this->createPriceTypeDefinition($type);
-        $this->prices[$type->toTypeName()] = $priceType;
-        return $priceType;
+        return $this->addPriceType($type);
     }

Also applies to: 28-34

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 15-15: UndefinedClass
src/product/PriceTypesCollection.php:15:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Type does not exist (see https://psalm.dev/019)

Comment on lines 36 to 39
public function end(): TariffType
{
return $this->parent;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation in end() method.

The end() method should validate that at least one price type has been added before returning to the parent context.

     public function end(): TariffType
     {
+        if (empty($this->prices)) {
+            throw new \RuntimeException('At least one price type must be defined');
+        }
         return $this->parent;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function end(): TariffType
{
return $this->parent;
}
public function end(): TariffType
{
if (empty($this->prices)) {
throw new \RuntimeException('At least one price type must be defined');
}
return $this->parent;
}

Copy link
Contributor

@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 and nitpick comments (1)
src/product/PriceTypeDefinition.php (1)

45-50: Remove commented-out code.

The commented-out invoiceRepresentation method should be either implemented or removed. Commented-out code can lead to confusion and maintenance issues.

-//    public function invoiceRepresentation(string $representationClass): self
-//    {
-//        $this->invoiceRepresentation = $representationClass;
-//
-//        return $this;
-//    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1e3d171 and d678b94.

📒 Files selected for processing (3)
  • src/product/BillingRegistryInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/BillingRegistryInterface.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/PriceTypeDefinition.php

[failure] 17-17: UndefinedClass
src/product/PriceTypeDefinition.php:17:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 19-19: UndefinedClass
src/product/PriceTypeDefinition.php:19:80: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


[failure] 21-21: UndefinedClass
src/product/PriceTypeDefinition.php:21:44: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 58-58: UndefinedClass
src/product/PriceTypeDefinition.php:58:47: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 63-63: UndefinedClass
src/product/PriceTypeDefinition.php:63:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)


[failure] 68-68: UndefinedClass
src/product/PriceTypeDefinition.php:68:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/PriceTypesCollection.php

[failure] 15-15: UndefinedClass
src/product/PriceTypesCollection.php:15:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/PriceTypesCollection.php:23:48: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


[failure] 28-28: UndefinedClass
src/product/PriceTypesCollection.php:28:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

🔇 Additional comments (6)
src/product/PriceTypesCollection.php (4)

1-13: LGTM! Well-structured class initialization.

The class follows good practices with strict typing, proper namespace organization, and constructor property promotion.


23-26: LGTM! Well-designed helper method.

The helper method follows good practices with proper scoping and single responsibility.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 23-23: UndefinedClass
src/product/PriceTypesCollection.php:23:48: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


15-21: 🛠️ Refactor suggestion

Refactor duplicate code and add validation.

The implementation contains duplicate code with the overuse method and lacks validation for duplicate price types.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 15-15: UndefinedClass
src/product/PriceTypesCollection.php:15:29: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


36-39: 🛠️ Refactor suggestion

Add validation in end() method.

The method should validate the collection state before returning to the parent context.

src/product/PriceTypeDefinition.php (2)

24-43: 🛠️ Refactor suggestion

Add input validation and method documentation.

The builder methods need input validation and proper documentation.


5-18: ⚠️ Potential issue

Add missing dependencies to composer.json.

The following classes are referenced but appear to be missing from dependencies:

  • hiqdev\billing\registry\invoice\InvoiceRepresentationCollection
  • hiqdev\billing\registry\product\PriceType
  • hiqdev\billing\registry\measure\RcpTrafCollector

Add the required package that provides these classes to your composer.json:

{
    "require": {
+        "hiqdev/billing-registry": "^1.0"
    }
}

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 17-17: UndefinedClass
src/product/PriceTypeDefinition.php:17:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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: 4

🧹 Outside diff range and nitpick comments (2)
src/product/PriceTypesCollection.php (2)

24-34: Optimize getAllPrices method.

The nested loops can be replaced with array_merge and array_values for better performance.

 private function getAllPrices(): array
 {
-    $allPrices = [];
-    foreach ($this->pricesGroupedByPriceType as $prices) {
-        foreach ($prices as $price) {
-            $allPrices[] = $price;
-        }
-    }
-
-    return $allPrices;
+    return array_merge(...array_values($this->pricesGroupedByPriceType));
 }

36-43: Refactor duplicate code in price type methods.

The monthly, overuse, feature, domain, certificate, and discount methods share identical implementation patterns.

+    private function createAndAddPriceType(GType $gType, PriceType $type): PriceTypeDefinition
+    {
+        $priceType = $this->createPriceTypeDefinition($gType, $type);
+        $this->addPriceTypeDefinition($type, $priceType);
+        return $priceType;
+    }
+
     public function monthly(PriceType $type): PriceTypeDefinition
     {
-        $priceType = $this->createPriceTypeDefinition(GType::monthly, $type);
-        $this->addPriceTypeDefinition($type, $priceType);
-        return $priceType;
+        return $this->createAndAddPriceType(GType::monthly, $type);
     }

Apply similar changes to other price type methods.

Also applies to: 55-62, 69-76, 78-85, 87-94, 96-103

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d678b94 and f7e6961.

📒 Files selected for processing (4)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/BillingRegistryInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/BillingRegistryInterface.php
🔇 Additional comments (2)
src/product/BillingRegistry.php (1)

27-34: 🛠️ Refactor suggestion

Add error handling for tariff type iteration.

The priceTypes method should handle potential errors from withPrices() calls.

 public function priceTypes(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        foreach ($tariffType->withPrices() as $priceTypeDefinition) {
-            yield $priceTypeDefinition;
+        try {
+            foreach ($tariffType->withPrices() as $priceTypeDefinition) {
+                yield $priceTypeDefinition;
+            }
+        } catch (\Throwable $e) {
+            throw new \RuntimeException(
+                "Failed to retrieve prices for tariff type",
+                0,
+                $e
+            );
         }
     }
 }

Likely invalid or redundant comment.

src/product/PriceTypeDefinition.php (1)

49-53: 🛠️ Refactor suggestion

Add validation in end() method.

The end() method should validate that all required properties are set before returning.

 public function end(): PriceTypesCollection
 {
-    // Validate the PriceType and lock its state
+    $requiredProps = ['unit', 'description', 'quantityFormatter'];
+    foreach ($requiredProps as $prop) {
+        if (!isset($this->$prop)) {
+            throw new \RuntimeException("$prop is required but not set");
+        }
+    }
     return $this->parent;
 }

Likely invalid or redundant comment.

Comment on lines 45 to 48
private function addPriceTypeDefinition(PriceType $type, PriceTypeDefinition $priceTypeDefinition): void
{
$this->pricesGroupedByPriceType[$type->name][] = $priceTypeDefinition;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation in addPriceTypeDefinition method.

The method should validate the price type name and prevent duplicate entries.

 private function addPriceTypeDefinition(PriceType $type, PriceTypeDefinition $priceTypeDefinition): void
 {
+    if (empty($type->name)) {
+        throw new \InvalidArgumentException('Price type name cannot be empty');
+    }
     $this->pricesGroupedByPriceType[$type->name][] = $priceTypeDefinition;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private function addPriceTypeDefinition(PriceType $type, PriceTypeDefinition $priceTypeDefinition): void
{
$this->pricesGroupedByPriceType[$type->name][] = $priceTypeDefinition;
}
private function addPriceTypeDefinition(PriceType $type, PriceTypeDefinition $priceTypeDefinition): void
{
if (empty($type->name)) {
throw new \InvalidArgumentException('Price type name cannot be empty');
}
$this->pricesGroupedByPriceType[$type->name][] = $priceTypeDefinition;
}

Copy link
Contributor

@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: 3

🧹 Nitpick comments (4)
src/product/PriceTypeDefinition.php (3)

18-19: Potential uninitialized property usage.
Although PHP enforces type hints, consider initializing the private property $unit with a default or in the constructor to avoid the risk of referencing it before the setter is called.


48-53: Validate description input if needed.
If an empty description is meaningless in your context, consider throwing an exception or logging a warning.


60-66: Check if $formatterClass exists.
The TODO comment suggests verifying if $formatterClass exists. Implement it to prevent runtime errors from invalid classes.

src/product/BillingRegistry.php (1)

31-38: Consider adding error handling or fallback logic.
If a tariff type's withPrices() call throws or returns non-iterable data, it may cause unexpected behavior. You could wrap the iteration in a try-catch to safeguard.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7ab9d97 and 3f8e530.

📒 Files selected for processing (4)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypesCollection.php (1 hunks)
  • src/quantity/QuantityFormatterInterface.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/PriceTypesCollection.php
🔇 Additional comments (3)
src/product/PriceTypeDefinition.php (2)

26-34: Consider adding validation for GType, PriceType, or parent correctness.
In situations where the constructor receives invalid objects (e.g., null or inconsistent references), you might want to enforce stronger checks or gracefully handle those errors, preventing runtime exceptions later.


89-92: Implementation is incomplete.
The measuredWith method simply returns $this. Ensure that hooking RcpTrafCollector or other measure logic is actually performed if intended.

src/quantity/QuantityFormatterInterface.php (1)

1-28: All methods appear well-defined.
The interface methods are concise and logical, covering textual formatting, DB storage format, and client-friendly quantity values. Great job!

Comment on lines 41 to 52
public function unit(string $unit): self
{
$this->unit = Unit::create($unit);

return $this;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation for the unit.
If $unit can be empty or invalid, a runtime error might occur in Unit::create(). Add input validation to ensure $unit is not empty and is recognized by Unit::create().

Comment on lines 78 to 93
public function end(): PriceTypesCollection
{
// Validate the PriceType and lock its state
return $this->parent;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add final validation to the end() method.
It might be beneficial to validate that required properties (e.g., $unit, $description, and $quantityFormatterDefinition) are all set and valid before returning to the parent.

Copy link
Contributor

@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: 2

🧹 Nitpick comments (3)
src/product/PriceTypeDefinition.php (2)

16-17: Add class-level documentation for clarity.
This class defines a core entity for building price types but lacks a PHPDoc block describing its purpose, usage, and examples.


63-69: Address the TODO for the formatter class check.
Currently, there is a TODO comment but no actual validation. Ensure the class exists and implements the required interface.

Would you like me to generate a quick fix for this check?

src/product/BillingRegistry.php (1)

16-23: Validate duplicates in addTariffType.
If TariffType is identified by something like getId(), consider preventing duplicates.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3f8e530 and 6548d55.

📒 Files selected for processing (2)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/PriceTypeDefinition.php

[failure] 22-22: UndefinedClass
src/product/PriceTypeDefinition.php:22:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 26-26: UndefinedClass
src/product/PriceTypeDefinition.php:26:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\BehaviorCollection does not exist (see https://psalm.dev/019)


[failure] 31-31: UndefinedClass
src/product/PriceTypeDefinition.php:31:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterFactory does not exist (see https://psalm.dev/019)


[failure] 33-33: UndefinedClass
src/product/PriceTypeDefinition.php:33:40: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 34-34: UndefinedClass
src/product/PriceTypeDefinition.php:34:41: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\BehaviorCollection does not exist (see https://psalm.dev/019)


[failure] 60-60: UndefinedDocblockClass
src/product/PriceTypeDefinition.php:60:15: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\unit\FractionUnit does not exist (see https://psalm.dev/200)


[failure] 72-72: UndefinedClass
src/product/PriceTypeDefinition.php:72:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

src/product/BillingRegistry.php

[failure] 41-41: UndefinedDocblockClass
src/product/BillingRegistry.php:41:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 59-59: UndefinedClass
src/product/BillingRegistry.php:59:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (3)
src/product/PriceTypeDefinition.php (3)

18-24: Initialize properties to avoid undefined usage.
Properties such as $unit, $description, etc., can be initialized with default values (e.g., empty strings) to prevent undefined usage.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 22-22: UndefinedClass
src/product/PriceTypeDefinition.php:22:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


44-49: Add input validation for unit.
If $unit can be empty or invalid, a runtime error might occur in Unit::create($unit). Validate that $unit is not empty and recognized by Unit::create.


81-85: Add final validation to end() method.
Before returning $this->parent, verify that you've set required properties (unit, description, and quantity formatter) to valid values.

Copy link
Contributor

@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: 2

♻️ Duplicate comments (1)
src/product/TariffType.php (1)

5-5: ⚠️ Potential issue

Incorrect namespace import for Product
It appears that the class "Product" doesn't exist in the "hiqdev\billing\registry\product" namespace, leading to a potential autoloading or class-not-found error. Consider referencing the correct Product class or interface in "hiqdev\php\billing\product" (e.g., "ProductInterface" or "ServerProduct").

Apply this diff for a potential fix (switching to ProductInterface as an example):

-use hiqdev\billing\registry\product\Product;
+use hiqdev\php\billing\product\ProductInterface as Product;
🧹 Nitpick comments (4)
src/product/TariffType.php (1)

33-37: Add implementation or remove placeholder
The "setPricesSuggester" method currently contains only a comment, leaving the purpose unfulfilled. You may want to either implement the logic (e.g., store or validate the suggester class and tie it into pricing logic) or remove it if it's not required.

src/product/ConsumptionDefinition.php (1)

19-22: Empty groups() method
The "groups()" method is currently a placeholder. If you plan to implement grouping logic for consumption columns, consider adding relevant parameters, logic, or documentation. Otherwise, remove the method to avoid confusion.

src/product/ConsumptionColumn.php (2)

11-16: Encapsulate logic behind meaningful method naming
The method "convertible()" sets "$isConvertible" to true. Though this is functional, you might consider using clearer naming (e.g., "markAsConvertible()") or adding inline documentation to clarify the context and ramifications.


23-28: Extend consistent naming
Similarly, "overMax()" sets "$isOverMax" to true. In combination with "convertible()", both methods could benefit from more descriptive names or docblocks to govern usage.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6548d55 and a1c0436.

📒 Files selected for processing (3)
  • src/product/ConsumptionColumn.php (1 hunks)
  • src/product/ConsumptionDefinition.php (1 hunks)
  • src/product/TariffType.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/ConsumptionDefinition.php

[failure] 7-7: UndefinedClass
src/product/ConsumptionDefinition.php:7:13: UndefinedClass: Class, interface or enum named hiqdev\php\billing\product\ConsumptionColumnCollection does not exist (see https://psalm.dev/019)


[failure] 11-11: UndefinedClass
src/product/ConsumptionDefinition.php:11:39: UndefinedClass: Class, interface or enum named hiqdev\php\billing\product\ConsumptionColumnCollection does not exist (see https://psalm.dev/019)


[failure] 14-14: UndefinedClass
src/product/ConsumptionDefinition.php:14:32: UndefinedClass: Class, interface or enum named hiqdev\php\billing\product\ConsumptionColumnCollection does not exist (see https://psalm.dev/019)

🔇 Additional comments (1)
src/product/TariffType.php (1)

26-31: 🛠️ Refactor suggestion

Validate the product class
When a product is assigned (via the "ofProduct" method), consider validating that the given class or object is correct. At minimum, ensure that it indeed conforms to your business logic or an expected interface to prevent accidental misuse.

 public function ofProduct(Product $product): self
 {
+    // e.g., validate or check interface
+    // if (!$product instanceof ProductInterface) {
+    //     throw new \InvalidArgumentException('Provided product must implement ProductInterface');
+    // }
     $this->product = $product;

     return $this;
 }

Likely invalid or redundant comment.

Comment on lines 56 to 59
public function end(): self
{
// Validate the TariffType and lock its state
return $this;
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Finalize or lock TariffType upon "end"
The "end()" method includes a comment about validating the TariffType and locking its state, but lacks any actual implementation. Consider adding the necessary steps to prevent further modification after this point, or rename it to better describe its purpose (e.g., "finalize" or "build").


class ConsumptionDefinition
{
private ConsumptionColumnCollection $columnCollection;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Undefined ConsumptionColumnCollection class
Static analysis reports that "ConsumptionColumnCollection" is not found in the "hiqdev\php\billing\product" namespace. Make sure you have created and properly namespaced this class or import it from the correct location to avoid fatal errors.

Would you like assistance in defining or locating "ConsumptionColumnCollection"?

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 7-7: UndefinedClass
src/product/ConsumptionDefinition.php:7:13: UndefinedClass: Class, interface or enum named hiqdev\php\billing\product\ConsumptionColumnCollection does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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: 6

♻️ Duplicate comments (1)
src/product/ConsumptionDefinition.php (1)

7-7: ⚠️ Potential issue

Undefined ConsumptionColumnCollection class

The ConsumptionColumnCollection class is not found in the current namespace.

Please ensure the class is properly imported:

+use hiqdev\php\billing\product\ConsumptionColumnCollection;
🧹 Nitpick comments (5)
src/product/ConsumptionDefinition.php (2)

5-9: Add class documentation

Please add PHPDoc to describe the class purpose and its role in the billing system. This will help other developers understand how this class fits into the overall architecture.

+/**
+ * ConsumptionDefinition manages the configuration of consumption-based billing
+ * through collections of columns and groups.
+ *
+ * @see ConsumptionColumnCollection
+ * @see ConsumptionGroupCollection
+ */
 class ConsumptionDefinition

10-14: Consider validating parent TariffType in constructor

The constructor accepts a TariffType without any validation. Consider adding validation to ensure the parent is in a valid state.

 public function __construct(private readonly TariffType $parent)
 {
+    if (!$parent->isValid()) {
+        throw new \InvalidArgumentException('Invalid TariffType provided');
+    }
     $this->columnCollection = new ConsumptionColumnCollection($this);
     $this->groupCollection = new ConsumptionGroupCollection($this);
 }
src/product/ConsumptionGroupCollection.php (1)

22-29: Consider adding validation in group() method

The group() method creates and stores groups without any validation or limits.

 public function group(): ConsumptionGroup
 {
+    if (count($this->groups) >= 100) {
+        throw new \RuntimeException('Maximum number of groups exceeded');
+    }
     $group = new ConsumptionGroup($this);
     $this->groups[] = $group;
     return $group;
 }
src/product/ConsumptionGroup.php (1)

25-30: Add validation in add() method

The add() method accepts PriceType objects without any validation.

 public function add(PriceType $priceType): self
 {
+    if ($priceType->isExpired()) {
+        throw new \InvalidArgumentException('Cannot add expired price type');
+    }
     $this->group[] = $priceType;
     return $this;
 }
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 25-25: UndefinedClass
src/product/ConsumptionGroup.php:25:25: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/ConsumptionColumn.php (1)

9-18: Add property documentation

Consider adding PHPDoc comments to explain the purpose and impact of $isConvertible and $isOverMax flags.

+    /**
+     * Indicates if the consumption can be converted to different units
+     */
     private bool $isConvertible = false;

+    /**
+     * Indicates if the consumption exceeds maximum allowed value
+     */
     private bool $isOverMax = false;
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 15-15: UndefinedClass
src/product/ConsumptionColumn.php:15:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a1c0436 and be007f7.

📒 Files selected for processing (7)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/ConsumptionColumn.php (1 hunks)
  • src/product/ConsumptionColumnCollection.php (1 hunks)
  • src/product/ConsumptionDefinition.php (1 hunks)
  • src/product/ConsumptionGroup.php (1 hunks)
  • src/product/ConsumptionGroupCollection.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/PriceTypeDefinition.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/ConsumptionGroup.php

[failure] 7-7: MissingTemplateParam
src/product/ConsumptionGroup.php:7:35: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionGroup has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


[failure] 18-18: UndefinedDocblockClass
src/product/ConsumptionGroup.php:18:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/200)


[failure] 25-25: UndefinedClass
src/product/ConsumptionGroup.php:25:25: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/ConsumptionColumn.php

[failure] 15-15: UndefinedClass
src/product/ConsumptionColumn.php:15:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)


[failure] 44-44: UndefinedClass
src/product/ConsumptionColumn.php:44:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/ConsumptionColumnCollection.php

[failure] 7-7: MissingTemplateParam
src/product/ConsumptionColumnCollection.php:7:46: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionColumnCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


[failure] 24-24: UndefinedClass
src/product/ConsumptionColumnCollection.php:24:28: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/ConsumptionGroupCollection.php

[failure] 5-5: MissingTemplateParam
src/product/ConsumptionGroupCollection.php:5:45: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionGroupCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/BillingRegistry.php

[failure] 41-41: UndefinedDocblockClass
src/product/BillingRegistry.php:41:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 59-59: UndefinedClass
src/product/BillingRegistry.php:59:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (10)
src/product/BillingRegistry.php (5)

16-23: Add validation for duplicate tariff types.

The current implementation allows duplicate tariff types to be added, which could lead to data integrity issues.


25-28: LGTM! The lock mechanism is simple and effective.

The one-way locking mechanism prevents modifications after initialization.


30-37: Add error handling and improve return type specificity.

The method should handle potential errors from withPrices() and provide a more specific return type hint.


43-55: Add validation for representation class existence.

The method should validate that the provided class exists and implements RepresentationInterface.


57-70: Enhance error context in createQuantityFormatter.

When quantity formatters are not found, you throw a generic exception. Consider adding details about which $type was not found to aid troubleshooting.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 59-59: UndefinedClass
src/product/BillingRegistry.php:59:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

src/product/ConsumptionColumnCollection.php (3)

9-14: LGTM!

Good use of constructor property promotion and proper type declarations.


16-22: LGTM!

Proper implementation of the IteratorAggregate interface with clear return type documentation.


24-36: Verify PriceType class dependency

The code references PriceType from hiqdev\billing\registry\product namespace, but static analysis indicates this class might not exist.

If the class doesn't exist, consider creating it or updating the import path.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 24-24: UndefinedClass
src/product/ConsumptionColumnCollection.php:24:28: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

src/product/ConsumptionColumn.php (2)

20-42: LGTM!

Good implementation of fluent interface pattern with clear state management methods.


44-52: LGTM!

Clean implementation of accessor methods. Note that the PriceType dependency verification was requested in the previous file's review.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 44-44: UndefinedClass
src/product/ConsumptionColumn.php:44:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\PriceType does not exist (see https://psalm.dev/019)

Comment on lines 72 to 132
public function getConsumptionColumns(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
yield $tariffType->consumption()->columns();
}
}

public function getConsumptionGroups(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
yield $tariffType->consumption()->groups();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for consumption methods.

Both methods assume that consumption() will always return a valid object. Consider adding null checks and error handling.

 public function getConsumptionColumns(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        yield $tariffType->consumption()->columns();
+        $consumption = $tariffType->consumption();
+        if ($consumption === null) {
+            throw new \RuntimeException(
+                sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
+            );
+        }
+        yield $consumption->columns();
     }
 }

 public function getConsumptionGroups(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        yield $tariffType->consumption()->groups();
+        $consumption = $tariffType->consumption();
+        if ($consumption === null) {
+            throw new \RuntimeException(
+                sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
+            );
+        }
+        yield $consumption->groups();
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function getConsumptionColumns(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
yield $tariffType->consumption()->columns();
}
}
public function getConsumptionGroups(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
yield $tariffType->consumption()->groups();
}
}
public function getConsumptionColumns(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
$consumption = $tariffType->consumption();
if ($consumption === null) {
throw new \RuntimeException(
sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
);
}
yield $consumption->columns();
}
}
public function getConsumptionGroups(): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
$consumption = $tariffType->consumption();
if ($consumption === null) {
throw new \RuntimeException(
sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
);
}
yield $consumption->groups();
}
}

Comment on lines 5 to 6
class ConsumptionGroupCollection implements \IteratorAggregate
{
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix IteratorAggregate implementation and docblock return type

There are two issues with the iterator implementation:

  1. Missing type parameters for IteratorAggregate
  2. Incorrect return type in docblock (ConsumptionColumn[] vs ConsumptionGroup[])
-class ConsumptionGroupCollection implements \IteratorAggregate
+class ConsumptionGroupCollection implements \IteratorAggregate<int, ConsumptionGroup>
 {
     /** @var ConsumptionGroup[] */
     private array $groups = [];

     /**
-     * @return ConsumptionColumn[]
+     * @return \Traversable<int, ConsumptionGroup>
      */
     public function getIterator(): \Traversable
     {
         return new \ArrayIterator($this->groups);
     }

Also applies to: 14-20

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 5-5: MissingTemplateParam
src/product/ConsumptionGroupCollection.php:5:45: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionGroupCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

Comment on lines 7 to 9
class ConsumptionGroup implements \IteratorAggregate
{
private array $group;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix IteratorAggregate implementation and initialize group array

The class is missing type parameters for IteratorAggregate and the group array is not initialized.

-class ConsumptionGroup implements \IteratorAggregate
+class ConsumptionGroup implements \IteratorAggregate<int, PriceType>
 {
-    private array $group;
+    /** @var PriceType[] */
+    private array $group = [];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class ConsumptionGroup implements \IteratorAggregate
{
private array $group;
class ConsumptionGroup implements \IteratorAggregate<int, PriceType>
{
/** @var PriceType[] */
private array $group = [];
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 7-7: MissingTemplateParam
src/product/ConsumptionGroup.php:7:35: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionGroup has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

Comment on lines 5 to 6
use hiqdev\billing\registry\product\PriceType;

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix import and use statement for PriceType

The PriceType class is not found in the current namespace.

 use hiqdev\billing\registry\product\PriceType;
+use hiqdev\php\billing\product\PriceType;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use hiqdev\billing\registry\product\PriceType;
use hiqdev\php\billing\product\PriceType;

Comment on lines 7 to 8
class ConsumptionColumnCollection implements \IteratorAggregate
{
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add type parameter to IteratorAggregate interface

The class implements IteratorAggregate without specifying the type parameter. In PHP 8.3, it's recommended to explicitly declare the type:

-class ConsumptionColumnCollection implements \IteratorAggregate
+class ConsumptionColumnCollection implements \IteratorAggregate<ConsumptionColumn>

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 7-7: MissingTemplateParam
src/product/ConsumptionColumnCollection.php:7:46: MissingTemplateParam: hiqdev\php\billing\product\ConsumptionColumnCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

Copy link
Contributor

@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: 1

🧹 Nitpick comments (3)
src/product/BehaviorInterface.php (1)

5-8: Consider renaming the method to be more descriptive

The method name class() is ambiguous and could potentially conflict with PHP's class keyword. Consider using a more descriptive name that clearly indicates the method's purpose, such as getClassName() or getBehaviorClass().

 interface BehaviorInterface
 {
-    public function class(): string;
+    public function getBehaviorClass(): string;
 }
src/product/BillingRegistry.php (2)

57-70: Enhance error context in createQuantityFormatter

The error message should include details about which type was not found.

-        throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+        throw new QuantityFormatterNotFoundException(
+            sprintf("Quantity formatter not found for type '%s'", $type)
+        );
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 59-59: UndefinedClass
src/product/BillingRegistry.php:59:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


86-95: Add class validation to behavior method

Similar to getRepresentationsByType, validate the behavior class before processing.

 public function behavior(string $behaviorClassWrapper): \Generator
 {
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new \InvalidArgumentException("Behavior class '$behaviorClassWrapper' does not exist");
+    }
+    if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
+        throw new \InvalidArgumentException(
+            "Class '$behaviorClassWrapper' must implement BehaviorInterface"
+        );
+    }
     foreach ($this->priceTypes() as $priceTypeDefinition) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between be007f7 and ab1f2f5.

📒 Files selected for processing (2)
  • src/product/BehaviorInterface.php (1 hunks)
  • src/product/BillingRegistry.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 41-41: UndefinedDocblockClass
src/product/BillingRegistry.php:41:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 59-59: UndefinedClass
src/product/BillingRegistry.php:59:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (4)
src/product/BillingRegistry.php (4)

16-23: Add validation for duplicate tariff types

The method should prevent duplicate tariff types to maintain data integrity.


30-37: 🛠️ Refactor suggestion

Add error handling to priceTypes method

The method should handle potential errors from withPrices() calls.

 public function priceTypes(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        foreach ($tariffType->withPrices() as $priceTypeDefinition) {
-            yield $priceTypeDefinition;
+        try {
+            foreach ($tariffType->withPrices() as $priceTypeDefinition) {
+                yield $priceTypeDefinition;
+            }
+        } catch (\Throwable $e) {
+            throw new \RuntimeException(
+                "Failed to retrieve prices for tariff type: " . $tariffType->getId(),
+                0,
+                $e
+            );
         }
     }
 }

Likely invalid or redundant comment.


72-84: 🛠️ Refactor suggestion

Add error handling for consumption methods

Both methods need null checks and error handling for the consumption calls.

 public function getConsumptionColumns(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        yield $tariffType->consumption()->columns();
+        $consumption = $tariffType->consumption();
+        if ($consumption === null) {
+            throw new \RuntimeException(
+                sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
+            );
+        }
+        yield $consumption->columns();
     }
 }

Likely invalid or redundant comment.


43-55: 🛠️ Refactor suggestion

Add class validation to getRepresentationsByType method

The method should validate that the provided class exists and implements the correct interface.

 public function getRepresentationsByType(string $representationClass): array
 {
+    if (!class_exists($representationClass)) {
+        throw new \InvalidArgumentException("Class '$representationClass' does not exist");
+    }
+    if (!is_subclass_of($representationClass, DocumentRepresentationInterface::class)) {
+        throw new \InvalidArgumentException(
+            "Class '$representationClass' must implement DocumentRepresentationInterface"
+        );
+    }
     $representations = [];

Likely invalid or redundant comment.

Comment on lines 5 to 9
use hiqdev\billing\registry\invoice\RepresentationInterface;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
use hiqdev\billing\registry\quantity\FractionQuantityData;
use hiqdev\php\billing\type\Type;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update import paths to match the codebase structure

The import paths for several classes don't match the codebase structure:

-use hiqdev\billing\registry\invoice\RepresentationInterface;
-use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
-use hiqdev\billing\registry\quantity\FractionQuantityData;
+use hiqdev\php\billing\product\DocumentRepresentationInterface;
+use hiqdev\php\billing\product\formatter\QuantityFormatterNotFoundException;
+use hiqdev\php\billing\product\quantity\FractionQuantityData;
 use hiqdev\php\billing\type\Type;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use hiqdev\billing\registry\invoice\RepresentationInterface;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
use hiqdev\billing\registry\quantity\FractionQuantityData;
use hiqdev\php\billing\type\Type;
use hiqdev\php\billing\product\DocumentRepresentationInterface;
use hiqdev\php\billing\product\formatter\QuantityFormatterNotFoundException;
use hiqdev\php\billing\product\quantity\FractionQuantityData;
use hiqdev\php\billing\type\Type;

Copy link
Contributor

@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: 1

♻️ Duplicate comments (2)
src/product/PriceTypeDefinition.php (2)

88-92: ⚠️ Potential issue

Add validation in end() method

The end() method should validate that all required properties are set before returning.

     public function end(): PriceTypesCollection
     {
-        // Validate the PriceType and lock its state
+        if ($this->unit === null) {
+            throw new \RuntimeException('Unit is required but not set');
+        }
+        if (empty($this->description)) {
+            throw new \RuntimeException('Description is required but not set');
+        }
+        if ($this->quantityFormatterDefinition === null) {
+            throw new \RuntimeException('Quantity formatter is required but not set');
+        }
         return $this->parent;
     }

99-102: ⚠️ Potential issue

Remove or implement measuredWith method

The method appears to be a no-op, returning $this without any implementation.

Either implement the method's logic or remove it if it's not needed. If it's intended for future use, document it as such:

+    /**
+     * @todo Implement measurement logic
+     * This method is intended to configure measurement collection using RcpTrafCollector
+     * but is currently not implemented.
+     */
     public function measuredWith(\hiqdev\billing\registry\measure\RcpTrafCollector $param): self
     {
+        throw new \RuntimeException('Method not implemented');
-        return $this;
     }
🧹 Nitpick comments (3)
src/product/BehaviorInterface.php (1)

5-10: Enhance interface documentation

While marker interfaces are valid design patterns, the documentation could be more descriptive about:

  • The purpose and benefits of marking a class as a product behavior
  • Examples of concrete behaviors that might implement this interface
  • How the interface integrates with the billing system
 /**
- * Empty interface for mark product behavior
+ * Marker interface for product behaviors in the billing system.
+ *
+ * Classes implementing this interface represent specific billing behaviors
+ * that can be attached to products. Examples might include:
+ * - Recurring billing behavior
+ * - Usage-based billing behavior
+ * - Tiered pricing behavior
+ *
+ * These behaviors can be registered with products through the BehaviorCollection.
  */
src/product/PriceTypeDefinition.php (2)

41-44: Document or remove empty hook method

The init() method is marked as a hook but lacks documentation explaining its purpose and when it should be overridden.

+    /**
+     * Hook method called after constructor initialization.
+     * Override this method in child classes to perform additional initialization.
+     */
     protected function init(): void
     {
-        // Hook
     }

124-130: Fix documentation language and typos

The PHPDoc contains typos and is written in a non-English language.

     /**
-     * це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
-     * місячне споживання за яке потрібно пробілити клієнта
+     * This parameter defines the aggregate function that is applied to daily recorded resources
+     * to determine the monthly consumption for which the client needs to be billed.
      *
      * @param Aggregate $aggregate
      * @return self
      */
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ab1f2f5 and dd5e730.

📒 Files selected for processing (2)
  • src/product/BehaviorInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
🔇 Additional comments (2)
src/product/PriceTypeDefinition.php (2)

5-16: ⚠️ Potential issue

Add missing dependency in composer.json

The class has multiple dependencies from the hiqdev\billing\registry namespace, but there's no corresponding package dependency declared.

Add the required dependency to composer.json:

 {
     "require": {
+        "hiqdev/billing-registry": "^1.0"
     }
 }

Likely invalid or redundant comment.


19-29: 🛠️ Refactor suggestion

Initialize properties to prevent undefined property access

Properties should be initialized with default values or marked as nullable to prevent undefined property access.

-    private UnitInterface $unit;
+    private ?UnitInterface $unit = null;
 
-    private string $description;
+    private string $description = '';
 
-    private QuantityFormatterDefinition $quantityFormatterDefinition;
+    private ?QuantityFormatterDefinition $quantityFormatterDefinition = null;
 
     private InvoiceRepresentationCollection $invoiceCollection;
 
     private BehaviorCollection $behaviorCollection;
 
-    private Aggregate $aggregate;
+    private ?Aggregate $aggregate = null;

Likely invalid or redundant comment.

Copy link
Contributor

@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: 2

♻️ Duplicate comments (5)
src/product/BillingRegistry.php (5)

5-8: ⚠️ Potential issue

Update import paths to match the codebase structure

The import paths for several classes don't match the project's namespace structure. They should be updated to use the hiqdev\php\billing\product namespace.

-use hiqdev\billing\registry\behavior\BehaviorNotFoundException;
-use hiqdev\billing\registry\invoice\RepresentationInterface;
-use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
-use hiqdev\billing\registry\quantity\FractionQuantityData;
+use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
+use hiqdev\php\billing\product\invoice\RepresentationInterface;
+use hiqdev\php\billing\product\quantity\formatter\QuantityFormatterNotFoundException;
+use hiqdev\php\billing\product\quantity\FractionQuantityData;

18-25: 🛠️ Refactor suggestion

Enhance validation in addTariffType method

While the basic locked state validation is good, consider these improvements:

  1. Add validation for duplicate tariff types (as previously suggested)
  2. Add type validation to ensure TariffType class exists
 public function addTariffType(TariffType $tariffType): void
 {
     if ($this->locked) {
         throw new \RuntimeException("BillingRegistry is locked and cannot be modified.");
     }
 
+    if (!class_exists(TariffType::class)) {
+        throw new \RuntimeException("TariffType class is not defined.");
+    }
+
+    foreach ($this->tariffTypes as $existingTariff) {
+        if ($existingTariff->getId() === $tariffType->getId()) {
+            throw new \RuntimeException("Tariff type with ID '{$tariffType->getId()}' already exists.");
+        }
+    }
+
     $this->tariffTypes[] = $tariffType;
 }

32-39: 🛠️ Refactor suggestion

Add error handling and improve documentation

The method should handle potential errors from withPrices() and include proper return type documentation.

+/**
+ * @return \Generator<PriceTypeDefinition>
+ * @throws \RuntimeException When price retrieval fails
+ */
 public function priceTypes(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
+        try {
             foreach ($tariffType->withPrices() as $priceTypeDefinition) {
                 yield $priceTypeDefinition;
             }
+        } catch (\Throwable $e) {
+            throw new \RuntimeException(
+                "Failed to retrieve prices for tariff type: " . $tariffType->getId(),
+                0,
+                $e
+            );
+        }
     }
 }

45-57: 🛠️ Refactor suggestion

Add validation for representation class

The method should validate the existence and interface implementation of the provided class.

 public function getRepresentationsByType(string $representationClass): array
 {
+    if (!class_exists($representationClass)) {
+        throw new \InvalidArgumentException("Class '$representationClass' does not exist");
+    }
+    if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
+        throw new \InvalidArgumentException("Class '$representationClass' must implement RepresentationInterface");
+    }
+
     $representations = [];
     foreach ($this->priceTypes() as $priceTypeDefinition) {
         foreach ($priceTypeDefinition->documentRepresentation() as $representation) {

79-91: 🛠️ Refactor suggestion

Add error handling for consumption methods

Both methods need null checks and proper error handling.

+/**
+ * @return \Generator<ConsumptionColumn>
+ * @throws \RuntimeException When consumption data is not available
+ */
 public function getConsumptionColumns(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        yield $tariffType->consumption()->columns();
+        $consumption = $tariffType->consumption();
+        if ($consumption === null) {
+            throw new \RuntimeException(
+                sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
+            );
+        }
+        yield $consumption->columns();
     }
 }

+/**
+ * @return \Generator<ConsumptionGroup>
+ * @throws \RuntimeException When consumption data is not available
+ */
 public function getConsumptionGroups(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        yield $tariffType->consumption()->groups();
+        $consumption = $tariffType->consumption();
+        if ($consumption === null) {
+            throw new \RuntimeException(
+                sprintf("No consumption data available for tariff type '%s'", $tariffType->getId())
+            );
+        }
+        yield $consumption->groups();
     }
 }
🧹 Nitpick comments (1)
src/product/BillingRegistry.php (1)

12-13: Add class-level documentation and consider extracting constants

The class would benefit from comprehensive documentation and constant extraction for error messages.

+/**
+ * Registry for managing billing-related entities such as tariff types, price types,
+ * and their associated behaviors.
+ *
+ * This class implements the Registry pattern to maintain a collection of tariff types
+ * and provide access to their various aspects such as price types, consumption data,
+ * and behaviors.
+ *
+ * @implements BillingRegistryInterface
+ */
 class BillingRegistry implements BillingRegistryInterface
 {
+    private const ERROR_REGISTRY_LOCKED = "BillingRegistry is locked and cannot be modified.";
+    private const ERROR_DUPLICATE_TARIFF = "Tariff type with ID '%s' already exists.";
+    private const ERROR_NO_CONSUMPTION = "No consumption data available for tariff type '%s'";
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dd5e730 and 73bf433.

📒 Files selected for processing (1)
  • src/product/BillingRegistry.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 43-43: UndefinedDocblockClass
src/product/BillingRegistry.php:43:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 61-61: UndefinedClass
src/product/BillingRegistry.php:61:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


[failure] 107-107: UndefinedClass
src/product/BillingRegistry.php:107:19: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\BehaviorNotFoundException does not exist (see https://psalm.dev/019)

Comment on lines 93 to 132
public function behavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
$type = $this->convertStringTypeToType($type);

foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
return $behavior;
}
}
}
}

throw new BehaviorNotFoundException('Behavior class not found');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve behavior method documentation and error handling

The method needs better documentation, input validation, and more informative error messages.

+/**
+ * Retrieves a behavior of specified type and class.
+ *
+ * @param string $type The type identifier
+ * @param string $behaviorClassWrapper The behavior class to match
+ * @return BehaviorInterface The matched behavior
+ * @throws BehaviorNotFoundException When no matching behavior is found
+ * @throws \InvalidArgumentException When input parameters are invalid
+ */
 public function behavior(string $type, string $behaviorClassWrapper): BehaviorInterface
 {
+    if (empty($type)) {
+        throw new \InvalidArgumentException('Type cannot be empty');
+    }
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new \InvalidArgumentException("Behavior class '$behaviorClassWrapper' does not exist");
+    }
+    if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
+        throw new \InvalidArgumentException("Class '$behaviorClassWrapper' must implement BehaviorInterface");
+    }
+
     $type = $this->convertStringTypeToType($type);
 
     foreach ($this->priceTypes() as $priceTypeDefinition) {
         if ($priceTypeDefinition->hasType($type)) {
             foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
                 if ($behavior instanceof $behaviorClassWrapper) {
                     return $behavior;
                 }
             }
         }
     }
 
-    throw new BehaviorNotFoundException('Behavior class not found');
+    throw new BehaviorNotFoundException(
+        sprintf("Behavior of class '%s' not found for type '%s'", $behaviorClassWrapper, $type)
+    );
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function behavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
$type = $this->convertStringTypeToType($type);
foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
return $behavior;
}
}
}
}
throw new BehaviorNotFoundException('Behavior class not found');
}
/**
* Retrieves a behavior of specified type and class.
*
* @param string $type The type identifier
* @param string $behaviorClassWrapper The behavior class to match
* @return BehaviorInterface The matched behavior
* @throws BehaviorNotFoundException When no matching behavior is found
* @throws \InvalidArgumentException When input parameters are invalid
*/
public function behavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
if (empty($type)) {
throw new \InvalidArgumentException('Type cannot be empty');
}
if (!class_exists($behaviorClassWrapper)) {
throw new \InvalidArgumentException("Behavior class '$behaviorClassWrapper' does not exist");
}
if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
throw new \InvalidArgumentException("Class '$behaviorClassWrapper' must implement BehaviorInterface");
}
$type = $this->convertStringTypeToType($type);
foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
return $behavior;
}
}
}
}
throw new BehaviorNotFoundException(
sprintf("Behavior of class '%s' not found for type '%s'", $behaviorClassWrapper, $type)
);
}
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 107-107: UndefinedClass
src/product/BillingRegistry.php:107:19: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\BehaviorNotFoundException does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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: 1

🧹 Nitpick comments (2)
src/product/BillingRegistry.php (2)

13-15: Add validation for tariff types array

Consider adding runtime validation to ensure tariff type uniqueness and validity.

Add a method to validate tariff types:

+    /**
+     * @param TariffType $tariffType
+     * @throws \InvalidArgumentException If tariff type is invalid
+     */
+    private function validateTariffType(TariffType $tariffType): void
+    {
+        foreach ($this->tariffTypes as $existingTariff) {
+            if ($existingTariff->getId() === $tariffType->getId()) {
+                throw new \InvalidArgumentException(
+                    sprintf('Tariff type with ID "%s" already exists', $tariffType->getId())
+                );
+            }
+        }
+    }

73-76: Add input validation to convertStringTypeToType method

The method should validate the input string before conversion.

Apply this diff to add validation:

 private function convertStringTypeToType(string $type): TypeInterface
 {
+    if (empty($type)) {
+        throw new \InvalidArgumentException('Type string cannot be empty');
+    }
     return Type::anyId($type);
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 73bf433 and 4c4d0a5.

📒 Files selected for processing (2)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/TariffType.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/TariffType.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (5)
src/product/BillingRegistry.php (5)

58-71: 🛠️ Refactor suggestion

Improve createQuantityFormatter method validation and error handling

The method needs input validation and more descriptive error messages.

Apply this diff to improve the method:

+    /**
+     * @throws QuantityFormatterNotFoundException
+     * @throws \InvalidArgumentException
+     */
     public function createQuantityFormatter(
         string $type,
         FractionQuantityData $data,
     ): array {
+        if (empty($type)) {
+            throw new \InvalidArgumentException('Type cannot be empty');
+        }
+
         $type = $this->convertStringTypeToType($type);
 
         foreach ($this->priceTypes() as $priceTypeDefinition) {
             if ($priceTypeDefinition->hasType($type)) {
                 return $priceTypeDefinition->createQuantityFormatter($data);
             }
         }
 
-        throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+        throw new QuantityFormatterNotFoundException(
+            sprintf('Quantity formatter not found for type "%s"', $type)
+        );
     }

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


5-8: ⚠️ Potential issue

Fix incorrect import paths

The import paths for several classes don't match the project structure. They should be updated to use the correct namespace.

Apply this diff to fix the import paths:

-use hiqdev\billing\registry\invoice\RepresentationInterface;
-use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
-use hiqdev\billing\registry\quantity\FractionQuantityData;
+use hiqdev\php\billing\invoice\RepresentationInterface;
+use hiqdev\php\billing\quantity\formatter\QuantityFormatterNotFoundException;
+use hiqdev\php\billing\quantity\FractionQuantityData;

Likely invalid or redundant comment.


31-38: 🛠️ Refactor suggestion

Add error handling for withPrices calls

The method should handle potential errors when retrieving prices from tariff types.

Apply this diff to add error handling:

 public function priceTypes(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        foreach ($tariffType->withPrices() as $priceTypeDefinition) {
-            yield $priceTypeDefinition;
+        try {
+            foreach ($tariffType->withPrices() as $priceTypeDefinition) {
+                yield $priceTypeDefinition;
+            }
+        } catch (\Throwable $e) {
+            throw new \RuntimeException(
+                sprintf('Failed to retrieve prices from tariff type "%s": %s',
+                    $tariffType->getId(),
+                    $e->getMessage()
+                ),
+                0,
+                $e
+            );
         }
     }
 }

Likely invalid or redundant comment.


44-56: 🛠️ Refactor suggestion

Validate representation class parameter

The method should validate that the provided class exists and implements RepresentationInterface.

Apply this diff to add validation:

 public function getRepresentationsByType(string $representationClass): array
 {
+    if (!class_exists($representationClass)) {
+        throw new \InvalidArgumentException(
+            sprintf('Representation class "%s" does not exist', $representationClass)
+        );
+    }
+    if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
+        throw new \InvalidArgumentException(
+            sprintf('Class "%s" must implement RepresentationInterface', $representationClass)
+        );
+    }
+
     $representations = [];
     foreach ($this->priceTypes() as $priceTypeDefinition) {

Likely invalid or redundant comment.


17-24: 🛠️ Refactor suggestion

Enhance tariff type validation in addTariffType method

The method should validate the tariff type before adding it to the registry.

Apply this diff to improve validation:

 public function addTariffType(TariffType $tariffType): void
 {
     if ($this->locked) {
         throw new \RuntimeException("BillingRegistry is locked and cannot be modified.");
     }
 
+    $this->validateTariffType($tariffType);
     $this->tariffTypes[] = $tariffType;
 }

Likely invalid or redundant comment.

Copy link
Contributor

@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

♻️ Duplicate comments (5)
src/product/PriceTypeDefinition.php (5)

5-12: ⚠️ Potential issue

Add missing dependency for registry namespace

The class depends on multiple classes from the hiqdev\billing\registry namespace, but there's no corresponding dependency in composer.json.

Add the required dependency to composer.json that provides the hiqdev\billing\registry namespace.


48-53: ⚠️ Potential issue

Add input validation for the unit

The unit value should be validated before passing to Unit::create().

If $unit can be empty or invalid, a runtime error might occur in Unit::create(). Add input validation to ensure $unit is not empty and is recognized by Unit::create().


72-78: ⚠️ Potential issue

Implement class existence validation

The TODO comment indicates missing validation for the formatter class.

Add validation to ensure the formatter class exists and implements the correct interface.


90-94: ⚠️ Potential issue

Add validation in end() method

The end() method should validate that all required properties are set before returning.

Add validation to ensure required properties (unit, description, quantityFormatterDefinition) are set before returning to the parent.


101-104: ⚠️ Potential issue

Implement the measuredWith logic

The method currently returns $this without implementing any logic.

Either implement the measurement logic or remove the method if it's not needed.

🧹 Nitpick comments (12)
src/product/PriceTypeDefinitionCollection.php (3)

10-16: Use explicit array typing for clarity
The private array property $pricesGroupedByPriceType could be more self-documenting if you annotate it with @var array<string, PriceTypeDefinition[]>, helping IDEs and static analyzers understand its structure.

 /**
  * @var array<string, PriceTypeDefinition[]>
  */
 private array $pricesGroupedByPriceType = [];

18-36: Consider implementing Countable
Your class implements \IteratorAggregate to allow iteration over the list of prices. If you also implement \Countable, you may conveniently provide the number of price definitions.


38-108: Reduce code duplication for similar methods
All methods (e.g., monthly, overuse, domain, etc.) differ only by GType. Consider creating a single private helper that accepts a GType and PriceType and returns a PriceTypeDefinition, to streamline the code.

-public function monthly(PriceType $type): PriceTypeDefinition
-{
-    $priceType = $this->createPriceTypeDefinition(GType::monthly, $type, $this->parent->tariffType());
-    $this->addPriceTypeDefinition($type, $priceType);
-    return $priceType;
-}
...
+private function definePrice(GType $gType, PriceType $type): PriceTypeDefinition
+{
+    $priceType = $this->createPriceTypeDefinition($gType, $type, $this->parent->tariffType());
+    $this->addPriceTypeDefinition($type, $priceType);
+    return $priceType;
+}
+
+public function monthly(PriceType $type): PriceTypeDefinition
+{
+    return $this->definePrice(GType::monthly, $type);
+}
src/product/BehaviorCollection.php (2)

7-7: Annotate IteratorAggregate with a generic
Psalm or PhpStan may complain about missing generic params. Consider adding template annotations, e.g. IteratorAggregate<BehaviorInterface>, to satisfy stricter static analyzers.

-class BehaviorCollection implements \IteratorAggregate
+/**
+ * @implements \IteratorAggregate<int, BehaviorInterface>
+ */
+class BehaviorCollection implements \IteratorAggregate
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 7-7: MissingTemplateParam
src/product/BehaviorCollection.php:7:37: MissingTemplateParam: hiqdev\php\billing\product\BehaviorCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


24-31: Validate BehaviorInterface implementations
Before attaching a behavior, consider checking if it indeed matches your expected interface shape (e.g., ensuring mandatory methods exist). This can help catch misconfigurations earlier.

src/product/TariffTypeDefinition.php (2)

1-21: Document how $product is utilized
Although $product is stored, you may want to include brief doc comments about its role within the definition (e.g., whether it’s used for referencing metadata or external logic).


23-56: Extend method-level documentation
Some public methods like end() and setPricesSuggester() lack details on their purpose and expected usage. Adding doc comments can clarify behavior and make the API more discoverable.

src/product/BillingRegistry.php (3)

17-24: Consider preventing duplicate tariff types
When adding a new tariff type, you may want to ensure uniqueness. If one with the same identity exists, throwing an exception guards against accidental overwrites.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 17-17: MissingDependency
src/product/BillingRegistry.php:17:35: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


40-56: Short-circuit by returning early
In getRepresentationsByType, consider short-circuiting once you find enough representations or if you anticipate large loops. This can improve performance in big registries.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


58-71: Provide clearer error context
When throwing QuantityFormatterNotFoundException, consider including the requested $type, making debugging easier.

-throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+throw new QuantityFormatterNotFoundException(
+    sprintf("Quantity formatter not found for type '%s'", (string) $type)
+);
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

src/product/PriceTypeDefinition.php (2)

43-46: Document or implement the init hook

The empty init() method suggests it's a hook for initialization, but its purpose is not documented.

Add PHPDoc explaining the purpose of this hook method and when it should be overridden:

+    /**
+     * Hook method for additional initialization in derived classes.
+     * Override this method to perform custom initialization logic.
+     */
     protected function init(): void
     {
         // Hook
     }

127-129: Translate comments to English

The PHPDoc comments are written in Ukrainian and should be translated to English for consistency.

Replace the Ukrainian comments with English translation:

-     * це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
-     * місячне споживання за яке потрібно пробілити клієнта
+     * This parameter defines the aggregate function that is applied to daily recorded resources
+     * to determine the monthly consumption for which the client needs to be billed
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4c4d0a5 and b414de3.

📒 Files selected for processing (6)
  • src/product/BehaviorCollection.php (1 hunks)
  • src/product/BehaviorInterface.php (1 hunks)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypeDefinitionCollection.php (1 hunks)
  • src/product/TariffTypeDefinition.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BehaviorInterface.php

[failure] 12-12: UndefinedClass
src/product/BehaviorInterface.php:12:35: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\domain\TariffType does not exist (see https://psalm.dev/019)


[failure] 14-14: UndefinedClass
src/product/BehaviorInterface.php:14:38: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\domain\TariffType does not exist (see https://psalm.dev/019)

src/product/BehaviorCollection.php

[failure] 7-7: MissingTemplateParam
src/product/BehaviorCollection.php:7:37: MissingTemplateParam: hiqdev\php\billing\product\BehaviorCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


[failure] 12-12: UndefinedClass
src/product/BehaviorCollection.php:12:33: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\domain\TariffType does not exist (see https://psalm.dev/019)

src/product/BillingRegistry.php

[failure] 14-14: MissingDependency
src/product/BillingRegistry.php:14:5: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 14-14: MissingDependency
src/product/BillingRegistry.php:14:13: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 17-17: MissingDependency
src/product/BillingRegistry.php:17:35: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 34-34: MissingDependency
src/product/BillingRegistry.php:34:22: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (3)
src/product/BehaviorInterface.php (1)

7-15: Ensure correct namespace usage
Static analysis indicates that hiqdev\billing\registry\domain\TariffType might be missing or incorrectly referenced. Verify that the namespace path to TariffType is valid or adjust your use statement.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 12-12: UndefinedClass
src/product/BehaviorInterface.php:12:35: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\domain\TariffType does not exist (see https://psalm.dev/019)


[failure] 14-14: UndefinedClass
src/product/BehaviorInterface.php:14:38: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\domain\TariffType does not exist (see https://psalm.dev/019)

src/product/BillingRegistry.php (1)

78-93: Clarify fallback behavior
If getBehavior returns null when no match is found, it may be beneficial to log or throw an exception, depending on usage. This ensures calling code is aware if the behavior truly doesn't exist.

src/product/PriceTypeDefinition.php (1)

20-31: 🛠️ Refactor suggestion

Initialize properties to prevent undefined access

Properties should be initialized with default values to prevent undefined property access.

Apply this diff:

-    private UnitInterface $unit;
+    private UnitInterface $unit;
 
-    private string $description;
+    private string $description = '';
 
-    private QuantityFormatterDefinition $quantityFormatterDefinition;
+    private ?QuantityFormatterDefinition $quantityFormatterDefinition = null;
 
-    private InvoiceRepresentationCollection $invoiceCollection;
+    private InvoiceRepresentationCollection $invoiceCollection;
 
-    private PriceTypeDefinitionBehaviourCollection $behaviorCollection;
+    private PriceTypeDefinitionBehaviourCollection $behaviorCollection;
 
-    private Aggregate $aggregate;
+    private ?Aggregate $aggregate = null;

Likely invalid or redundant comment.

Copy link
Contributor

@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: 1

🧹 Nitpick comments (3)
src/product/BehaviorNotFoundException.php (1)

7-9: Add a docblock to clarify usage context

A short docblock explaining when or why this exception is thrown would make the code more self-documenting. For instance, note that it is thrown when a requested behavior class is not registered or cannot be located.

src/product/BillingRegistry.php (2)

78-93: Align naming conventions for “Behavior” vs. “Behaviour”
The exception message uses British spelling, while the class is named in American spelling. For consistency, update the message or rename the class. Also, since you always throw an exception on failure, returning a nullable ?BehaviorInterface might be misleading.

- throw new BehaviorNotFoundException('Behaviour was not found');
+ throw new BehaviorNotFoundException('Behavior was not found');

95-112: Add docblock and consider deduplication
Given that getBehaviors iterates over both tariff types and price types, it may yield the same behavior multiple times if it is declared at multiple levels. If that’s undesirable, consider filtering or deduplicating. Also, add a docblock for clarity:

 /**
+ * Retrieves a generator of all behaviors matching the specified class,
+ * searching in both tariff types and price types.
+ *
+ * @param string $behaviorClassWrapper
+ * @return \Generator<BehaviorInterface>
+ */
 public function getBehaviors(string $behaviorClassWrapper): \Generator
 {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b414de3 and b6893c5.

📒 Files selected for processing (2)
  • src/product/BehaviorNotFoundException.php (1 hunks)
  • src/product/BillingRegistry.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 14-14: MissingDependency
src/product/BillingRegistry.php:14:5: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 14-14: MissingDependency
src/product/BillingRegistry.php:14:13: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 17-17: MissingDependency
src/product/BillingRegistry.php:17:35: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 34-34: MissingDependency
src/product/BillingRegistry.php:34:22: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

🔇 Additional comments (6)
src/product/BillingRegistry.php (6)

17-24: Prevent duplicate tariff-type entries
This comment is identical to a past suggestion about adding validation for duplicates. Consider checking for an existing tariff type with the same ID before adding a new one.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 17-17: MissingDependency
src/product/BillingRegistry.php:17:35: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


26-30: Locking mechanism confirmed
The locking mechanism is straightforward and effectively prevents further modifications.


31-38: Error handling for price retrieval
The method inherits any errors arising from each tariffType->withPrices(). A previous review recommended adding error handling and clarifying the return type. Consider reintroducing or adopting that approach if applicable.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 34-34: MissingDependency
src/product/BillingRegistry.php:34:22: MissingDependency: hiqdev\php\billing\product\TariffTypeDefinition depends on class or interface hiqdev\php\billing\product\parentnodedefinitioninterface that does not exist (see https://psalm.dev/157)


40-56: Docblock references an unknown interface?
Static analysis flags RepresentationInterface in the docblock as undefined. Confirm that this interface is available and loaded in the codebase.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


73-76: Conversion function usage
This private method simply delegates to Type::anyId. That’s acceptable, but if your domain logic requires advanced string-to-type conversion later, consider adding validation or caching for performance.


58-71: 🛠️ Refactor suggestion

Contextualize the exception message
When throwing a QuantityFormatterNotFoundException, include the given $type to aid debugging:

-throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+throw new QuantityFormatterNotFoundException(
+    sprintf("Quantity formatter not found for type '%s'", $type->getId())
+);

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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/product/ParentNodeDefinitionInterface.php (1)

5-8: Consider adding a docblock and clarifying the method purpose.
It would be beneficial to document the contract of withBehaviors() for improved maintainability and clarity, especially regarding what type of BehaviorCollection gets returned and any side effects. If BehaviorCollection resides in a different namespace, ensure that it’s properly imported.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b6893c5 and f66e578.

📒 Files selected for processing (1)
  • src/product/ParentNodeDefinitionInterface.php (1 hunks)
🔇 Additional comments (1)
src/product/ParentNodeDefinitionInterface.php (1)

1-1: Good use of strict typing.
This ensures type safety across the file, preventing unexpected type coercion issues.

Copy link
Contributor

@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: 2

♻️ Duplicate comments (3)
src/product/PriceTypeDefinition.php (3)

5-16: ⚠️ Potential issue

Add missing billing-registry dependency

Multiple classes from the hiqdev\billing\registry namespace are not found. This is a dependency issue.

Add the required dependency to your composer.json as mentioned in the previous comment.


74-75: ⚠️ Potential issue

Implement class existence validation

The TODO comment indicates missing validation for the formatter class.

Implement the class existence check as suggested in the previous review comments.


101-104: ⚠️ Potential issue

Implement measuredWith method logic

The measuredWith method is currently a no-op, returning $this without any implementation.

Either implement the measurement logic or remove the method if it's not needed, as mentioned in previous review comments.

🧹 Nitpick comments (2)
src/product/BehaviorInterface.php (1)

7-15: Add method-level documentation

The interface methods lack PHPDoc documentation explaining their purpose and parameters.

Apply this diff:

 interface BehaviorInterface
 {
+    /**
+     * Sets the tariff type for this behavior.
+     *
+     * @param TariffType $tariffTypeName The tariff type to set
+     */
     public function setTariffType(TariffType $tariffTypeName): void;

+    /**
+     * Gets the current tariff type.
+     *
+     * @return TariffType The current tariff type
+     */
     public function getTariffType(): TariffType;
 }
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 12-12: UndefinedClass
src/product/BehaviorInterface.php:12:35: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\TariffType does not exist (see https://psalm.dev/019)


[failure] 14-14: UndefinedClass
src/product/BehaviorInterface.php:14:38: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\TariffType does not exist (see https://psalm.dev/019)

src/product/BehaviorCollection.php (1)

7-7: Add generic type hints for IteratorAggregate

The class implements IteratorAggregate but lacks the generic type parameter.

Apply this diff:

-class BehaviorCollection implements \IteratorAggregate
+class BehaviorCollection implements \IteratorAggregate<int, BehaviorInterface>
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 7-7: MissingTemplateParam
src/product/BehaviorCollection.php:7:37: MissingTemplateParam: hiqdev\php\billing\product\BehaviorCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f66e578 and 63f5327.

📒 Files selected for processing (5)
  • src/product/BehaviorCollection.php (1 hunks)
  • src/product/BehaviorInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
  • src/product/PriceTypeDefinitionCollection.php (1 hunks)
  • src/product/TariffTypeDefinition.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/product/TariffTypeDefinition.php
  • src/product/PriceTypeDefinitionCollection.php
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/PriceTypeDefinition.php

[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


[failure] 26-26: UndefinedClass
src/product/PriceTypeDefinition.php:26:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 28-28: UndefinedClass
src/product/PriceTypeDefinition.php:28:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\PriceTypeDefinitionBehaviourCollection does not exist (see https://psalm.dev/019)

src/product/BehaviorCollection.php

[failure] 7-7: MissingTemplateParam
src/product/BehaviorCollection.php:7:37: MissingTemplateParam: hiqdev\php\billing\product\BehaviorCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


[failure] 12-12: UndefinedClass
src/product/BehaviorCollection.php:12:33: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\TariffType does not exist (see https://psalm.dev/019)

src/product/BehaviorInterface.php

[failure] 12-12: UndefinedClass
src/product/BehaviorInterface.php:12:35: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\TariffType does not exist (see https://psalm.dev/019)


[failure] 14-14: UndefinedClass
src/product/BehaviorInterface.php:14:38: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\TariffType does not exist (see https://psalm.dev/019)

🔇 Additional comments (1)
src/product/BehaviorCollection.php (1)

24-31: Method implementation looks good

The attach method correctly sets the tariff type and maintains method chaining.


namespace hiqdev\php\billing\product;

use hiqdev\billing\registry\Domain\Model\TariffType;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add missing dependency for TariffType class

The TariffType class from hiqdev\billing\registry\Domain\Model namespace is not found. This appears to be a missing dependency issue.

Add the required dependency to your composer.json:

{
    "require": {
        "hiqdev/billing-registry": "^1.0"
    }
}

Comment on lines 127 to 139
* це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
* місячне споживання за яке потрібно пробілити клієнта
*
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Translate documentation to English

The PHPDoc comment is written in a non-English language. For better maintainability and collaboration, documentation should be in English.

Apply this diff:

-     * це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
-     * місячне споживання за яке потрібно пробілити клієнта
+     * This parameter defines the aggregate function that is applied to daily recorded resources
+     * to determine the monthly consumption for which the client needs to be billed
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
* це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
* місячне споживання за яке потрібно пробілити клієнта
*
* This parameter defines the aggregate function that is applied to daily recorded resources
* to determine the monthly consumption for which the client needs to be billed
*

Copy link
Contributor

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 63f5327 and 49989bf.

📒 Files selected for processing (2)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


[failure] 98-98: UndefinedClass
src/product/BillingRegistry.php:98:22: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\TariffTypeBehaviorCollection does not exist (see https://psalm.dev/019)

src/product/PriceTypeDefinition.php

[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


[failure] 26-26: UndefinedClass
src/product/PriceTypeDefinition.php:26:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)


[failure] 28-28: UndefinedClass
src/product/PriceTypeDefinition.php:28:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\PriceTypeDefinitionBehaviourCollection does not exist (see https://psalm.dev/019)

🔇 Additional comments (11)
src/product/BillingRegistry.php (8)

11-15: LGTM! Properties are well-defined.

The class declaration and properties are properly typed and documented.


26-29: LGTM! Simple and effective implementation.

The lock method correctly prevents further modifications to the registry.


31-38: 🛠️ Refactor suggestion

Add error handling and improve return type specificity.

The method should handle potential errors from withPrices() and provide a more specific return type hint.

-public function priceTypes(): \Generator
+/**
+ * @return \Generator<PriceTypeDefinition>
+ * @throws \RuntimeException When price retrieval fails
+ */
+public function priceTypes(): \Generator
 {
     foreach ($this->tariffTypes as $tariffType) {
-        foreach ($tariffType->withPrices() as $priceTypeDefinition) {
-            yield $priceTypeDefinition;
+        try {
+            foreach ($tariffType->withPrices() as $priceTypeDefinition) {
+                yield $priceTypeDefinition;
+            }
+        } catch (\Throwable $e) {
+            throw new \RuntimeException(
+                sprintf("Failed to retrieve prices for tariff type '%s'", $tariffType->getId()),
+                0,
+                $e
+            );
         }
     }
 }

Likely invalid or redundant comment.


17-24: 🛠️ Refactor suggestion

Add validation for duplicate tariff types.

While the lock check is good, the method should also prevent duplicate tariff types to maintain data integrity.

 public function addTariffType(TariffTypeDefinition $tariffType): void
 {
     if ($this->locked) {
         throw new \RuntimeException("BillingRegistry is locked and cannot be modified.");
     }
 
+    foreach ($this->tariffTypes as $existingTariff) {
+        if ($existingTariff->getId() === $tariffType->getId()) {
+            throw new \RuntimeException(
+                sprintf("Tariff type with ID '%s' already exists.", $tariffType->getId())
+            );
+        }
+    }
+
     $this->tariffTypes[] = $tariffType;
 }

Likely invalid or redundant comment.


58-71: 🛠️ Refactor suggestion

Improve error handling and input validation.

The method needs better error messages and input validation.

 public function createQuantityFormatter(
     string $type,
     FractionQuantityData $data,
 ): array {
+    if (empty($type)) {
+        throw new \InvalidArgumentException('Type cannot be empty');
+    }
+
     $type = $this->convertStringTypeToType($type);
 
     foreach ($this->priceTypes() as $priceTypeDefinition) {
         if ($priceTypeDefinition->hasType($type)) {
             return $priceTypeDefinition->createQuantityFormatter($data);
         }
     }
 
-    throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+    throw new QuantityFormatterNotFoundException(
+        sprintf("Quantity formatter not found for type '%s'", $type)
+    );
 }

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


40-56: 🛠️ Refactor suggestion

Add validation for representation class existence.

The method should validate that the provided class exists and implements RepresentationInterface.

 public function getRepresentationsByType(string $representationClass): array
 {
+    if (!class_exists($representationClass)) {
+        throw new \InvalidArgumentException(
+            sprintf("Class '%s' does not exist", $representationClass)
+        );
+    }
+    if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
+        throw new \InvalidArgumentException(
+            sprintf("Class '%s' must implement RepresentationInterface", $representationClass)
+        );
+    }
+
     $representations = [];
     foreach ($this->priceTypes() as $priceTypeDefinition) {
         foreach ($priceTypeDefinition->documentRepresentation() as $representation) {

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


5-8: ⚠️ Potential issue

Add missing dependency for registry classes.

The code imports several classes from the hiqdev\billing\registry namespace, but this dependency appears to be missing from the project. This will cause compilation failures.

Add the required dependency to your composer.json:

 {
     "require": {
+        "hiqdev/billing-registry": "^1.0"
     }
 }

Likely invalid or redundant comment.


78-112: 🛠️ Refactor suggestion

Add behavior class validation and improve error messages.

The methods should validate the behavior class wrapper and provide more informative error messages.

 public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
 {
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new \InvalidArgumentException(
+            sprintf("Behavior class '%s' does not exist", $behaviorClassWrapper)
+        );
+    }
+    if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
+        throw new \InvalidArgumentException(
+            sprintf("Class '%s' must implement BehaviorInterface", $behaviorClassWrapper)
+        );
+    }
+
     $type = $this->convertStringTypeToType($type);
 
     foreach ($this->priceTypes() as $priceTypeDefinition) {
         if ($priceTypeDefinition->hasType($type)) {
             foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
                 if ($behavior instanceof $behaviorClassWrapper) {
                     return $behavior;
                 }
             }
         }
     }
 
-    throw new BehaviorNotFoundException('Behaviour was not found');
+    throw new BehaviorNotFoundException(
+        sprintf("Behavior of class '%s' not found for type '%s'", $behaviorClassWrapper, $type)
+    );
 }

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 98-98: UndefinedClass
src/product/BillingRegistry.php:98:22: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\TariffTypeBehaviorCollection does not exist (see https://psalm.dev/019)

src/product/PriceTypeDefinition.php (3)

127-129: 🛠️ Refactor suggestion

Translate documentation to English.

Documentation should be in English for better maintainability and collaboration.

-    /**
-     * це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
-     * місячне споживання за яке потрібно пробілити клієнта
+    /**
+     * This parameter defines the aggregate function that is applied to daily recorded resources
+     * to determine the monthly consumption for which the client needs to be billed

Likely invalid or redundant comment.


73-78: 🛠️ Refactor suggestion

Implement TODO: Add class existence validation.

The TODO comment indicates missing validation for the formatter class existence.

     public function quantityFormatter(string $formatterClass, $fractionUnit = null): self
     {
-        // TODO: check if formatterClass exists
+        if (!class_exists($formatterClass)) {
+            throw new \InvalidArgumentException(
+                sprintf('Formatter class "%s" does not exist', $formatterClass)
+            );
+        }
+        if (!is_subclass_of($formatterClass, QuantityFormatterInterface::class)) {
+            throw new \InvalidArgumentException(
+                sprintf('Class "%s" must implement QuantityFormatterInterface', $formatterClass)
+            );
+        }
         $this->quantityFormatterDefinition = new QuantityFormatterDefinition($formatterClass, $fractionUnit);
 
         return $this;
     }

Likely invalid or redundant comment.


90-94: 🛠️ Refactor suggestion

Add validation in end() method.

The end() method should validate that all required properties are set before returning.

     public function end(): PriceTypeDefinitionCollection
     {
-        // Validate the PriceType and lock its state
+        $requiredProps = ['unit', 'description', 'quantityFormatterDefinition'];
+        foreach ($requiredProps as $prop) {
+            if (!isset($this->$prop)) {
+                throw new \RuntimeException(sprintf("Required property '%s' is not set", $prop));
+            }
+        }
         return $this->parent;
     }

Likely invalid or redundant comment.

Comment on lines 101 to 103
public function measuredWith(\hiqdev\billing\registry\measure\RcpTrafCollector $param): self
{
return $this;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

🛠️ Refactor suggestion

Remove unused measuredWith method

The measuredWith method in PriceTypeDefinition.php is not used anywhere in the codebase, and its implementation is a no-op that just returns $this. Since both the method and its parameter type RcpTrafCollector are only referenced in this single location, it's safe to remove this unused method.

🔗 Analysis chain

Implement measuredWith method or remove it.

The measuredWith method is currently a no-op. Either implement the measurement logic or remove the method if it's not needed.

Let me check if this method is used elsewhere:


Let me try to find any usage of this method or the collector class it depends on.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for uses of measuredWith method
rg "->measuredWith\(" --type php

Length of output: 439


Script:

#!/bin/bash
# Search for uses of measuredWith method with proper escaping
rg "measuredWith" --type php

# Also check for any references to RcpTrafCollector class
rg "RcpTrafCollector" --type php

Length of output: 327

Copy link
Contributor

@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: 2

🧹 Nitpick comments (2)
src/product/BillingRegistry.php (2)

13-24: Enhance type safety and data validation

The tariff types storage implementation could be improved:

  1. The property docblock could be more specific
  2. Consider validating for duplicate tariff types before adding

Apply these improvements:

-    /** @var TariffTypeDefinition[] */
+    /** @var array<int|string, TariffTypeDefinition> */
     private array $tariffTypes = [];

     public function addTariffType(TariffTypeDefinition $tariffType): void
     {
         if ($this->locked) {
             throw new \RuntimeException("BillingRegistry is locked and cannot be modified.");
         }
+
+        foreach ($this->tariffTypes as $existingTariff) {
+            if ($existingTariff->getId() === $tariffType->getId()) {
+                throw new \RuntimeException(
+                    sprintf('Tariff type with ID "%s" already exists', $tariffType->getId())
+                );
+            }
+        }

         $this->tariffTypes[] = $tariffType;
     }

73-76: Add input validation and documentation

The type conversion helper needs input validation and better documentation.

Apply these improvements:

+    /**
+     * Converts a string type identifier to a TypeInterface instance.
+     *
+     * @param string $type The type identifier
+     * @return TypeInterface
+     * @throws \InvalidArgumentException When type is empty
+     */
     private function convertStringTypeToType(string $type): TypeInterface
     {
+        if (empty($type)) {
+            throw new \InvalidArgumentException('Type cannot be empty');
+        }
+
         return Type::anyId($type);
     }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 49989bf and 1280f4c.

📒 Files selected for processing (1)
  • src/product/BillingRegistry.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


[failure] 104-104: UndefinedClass
src/product/BillingRegistry.php:104:22: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\TariffTypeBehaviorCollection does not exist (see https://psalm.dev/019)

🔇 Additional comments (3)
src/product/BillingRegistry.php (3)

5-8: ⚠️ Potential issue

Fix incorrect import paths

The imports from hiqdev\billing\registry namespace are incorrect. These classes should be imported from the hiqdev\php\billing namespace according to the project structure.

Apply this diff to fix the imports:

-use hiqdev\billing\registry\invoice\RepresentationInterface;
-use hiqdev\billing\registry\quantity\formatter\QuantityFormatterNotFoundException;
-use hiqdev\billing\registry\quantity\FractionQuantityData;
+use hiqdev\php\billing\product\DocumentRepresentationInterface;
+use hiqdev\php\billing\product\formatter\QuantityFormatterNotFoundException;
+use hiqdev\php\billing\product\quantity\FractionQuantityData;

Likely invalid or redundant comment.


31-38: 🛠️ Refactor suggestion

Add error handling and improve return type documentation

The price types generator needs better error handling and documentation.

Apply these improvements:

+    /**
+     * @return \Generator<int, PriceTypeDefinition>
+     * @throws \RuntimeException When price retrieval fails
+     */
     public function priceTypes(): \Generator
     {
         foreach ($this->tariffTypes as $tariffType) {
+            try {
                 foreach ($tariffType->withPrices() as $priceTypeDefinition) {
                     yield $priceTypeDefinition;
                 }
+            } catch (\Throwable $e) {
+                throw new \RuntimeException(
+                    sprintf('Failed to retrieve prices for tariff type: %s', $tariffType->getId()),
+                    0,
+                    $e
+                );
+            }
         }
     }

Likely invalid or redundant comment.


58-71: 🛠️ Refactor suggestion

Improve error handling and documentation

The quantity formatter creation needs better input validation, error messages, and documentation.

Apply these improvements:

+    /**
+     * @param string $type The type identifier
+     * @param FractionQuantityData $data The quantity data
+     * @return array{formatter: callable, params: array} The formatter configuration
+     * @throws QuantityFormatterNotFoundException When no formatter is found
+     * @throws \InvalidArgumentException When type is empty
+     */
     public function createQuantityFormatter(
         string $type,
         FractionQuantityData $data,
     ): array {
+        if (empty($type)) {
+            throw new \InvalidArgumentException('Type cannot be empty');
+        }
+
         $type = $this->convertStringTypeToType($type);

         foreach ($this->priceTypes() as $priceTypeDefinition) {
             if ($priceTypeDefinition->hasType($type)) {
                 return $priceTypeDefinition->createQuantityFormatter($data);
             }
         }

-        throw new QuantityFormatterNotFoundException('Quantity formatter not found');
+        throw new QuantityFormatterNotFoundException(
+            sprintf('Quantity formatter not found for type "%s"', $type)
+        );
     }

Likely invalid or redundant comment.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 60-60: UndefinedClass
src/product/BillingRegistry.php:60:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)

Comment on lines 40 to 57
/**
* @param string $representationClass
* @return RepresentationInterface[]
*/
public function getRepresentationsByType(string $representationClass): array
{
$representations = [];
foreach ($this->priceTypes() as $priceTypeDefinition) {
foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
if ($representation instanceof $representationClass) {
$representations[] = $representation;
}
}
}

return $representations;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation and fix return type hint

The method needs validation for the representation class and updated return type documentation.

Apply these improvements:

     /**
      * @param string $representationClass
-     * @return RepresentationInterface[]
+     * @return DocumentRepresentationInterface[]
+     * @throws \InvalidArgumentException When representation class is invalid
      */
     public function getRepresentationsByType(string $representationClass): array
     {
+        if (!class_exists($representationClass)) {
+            throw new \InvalidArgumentException(
+                sprintf('Class "%s" does not exist', $representationClass)
+            );
+        }
+        if (!is_subclass_of($representationClass, DocumentRepresentationInterface::class)) {
+            throw new \InvalidArgumentException(
+                sprintf('Class "%s" must implement DocumentRepresentationInterface', $representationClass)
+            );
+        }
+
         $representations = [];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @param string $representationClass
* @return RepresentationInterface[]
*/
public function getRepresentationsByType(string $representationClass): array
{
$representations = [];
foreach ($this->priceTypes() as $priceTypeDefinition) {
foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
if ($representation instanceof $representationClass) {
$representations[] = $representation;
}
}
}
return $representations;
}
/**
* @param string $representationClass
* @return DocumentRepresentationInterface[]
* @throws \InvalidArgumentException When representation class is invalid
*/
public function getRepresentationsByType(string $representationClass): array
{
if (!class_exists($representationClass)) {
throw new \InvalidArgumentException(
sprintf('Class "%s" does not exist', $representationClass)
);
}
if (!is_subclass_of($representationClass, DocumentRepresentationInterface::class)) {
throw new \InvalidArgumentException(
sprintf('Class "%s" must implement DocumentRepresentationInterface', $representationClass)
);
}
$representations = [];
foreach ($this->priceTypes() as $priceTypeDefinition) {
foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
if ($representation instanceof $representationClass) {
$representations[] = $representation;
}
}
}
return $representations;
}
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 42-42: UndefinedDocblockClass
src/product/BillingRegistry.php:42:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)

Comment on lines 78 to 119
/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
$type = $this->convertStringTypeToType($type);

foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
return $behavior;
}
}
}
}

throw new BehaviorNotFoundException('Behaviour was not found');
}

public function getBehaviors(string $behaviorClassWrapper): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
foreach ($tariffType->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
yield $behavior;
}
}
}

foreach ($this->priceTypes() as $priceTypeDefinition) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
yield $behavior;
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor behavior management for better maintainability

The behavior management implementation needs several improvements:

  1. Input validation
  2. Better error messages
  3. Code deduplication
  4. Better documentation

Apply these improvements:

+    /**
+     * @param string $behaviorClassWrapper The behavior class to match
+     * @return \Generator<int, BehaviorInterface>
+     * @throws \InvalidArgumentException When behavior class is invalid
+     */
+    private function findBehaviorsInCollection(iterable $collection, string $behaviorClassWrapper): \Generator
+    {
+        foreach ($collection as $behavior) {
+            if ($behavior instanceof $behaviorClassWrapper) {
+                yield $behavior;
+            }
+        }
+    }

     /**
      * @param string $type - full type like 'overuse,lb_capacity_unit'
      * @param string $behaviorClassWrapper
      * @return BehaviorInterface
      * @throws BehaviorNotFoundException
+     * @throws \InvalidArgumentException When parameters are invalid
      */
     public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
     {
+        if (empty($type)) {
+            throw new \InvalidArgumentException('Type cannot be empty');
+        }
+        if (!class_exists($behaviorClassWrapper)) {
+            throw new \InvalidArgumentException(
+                sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
+            );
+        }
+
         $type = $this->convertStringTypeToType($type);

         foreach ($this->priceTypes() as $priceTypeDefinition) {
             if ($priceTypeDefinition->hasType($type)) {
-                foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
-                    if ($behavior instanceof $behaviorClassWrapper) {
-                        return $behavior;
-                    }
+                foreach ($this->findBehaviorsInCollection($priceTypeDefinition->withBehaviors(), $behaviorClassWrapper) as $behavior) {
+                    return $behavior;
                 }
             }
         }

-        throw new BehaviorNotFoundException('Behaviour was not found');
+        throw new BehaviorNotFoundException(
+            sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type)
+        );
     }

+    /**
+     * @param string $behaviorClassWrapper The behavior class to match
+     * @return \Generator<int, BehaviorInterface>
+     * @throws \InvalidArgumentException When behavior class is invalid
+     */
     public function getBehaviors(string $behaviorClassWrapper): \Generator
     {
+        if (!class_exists($behaviorClassWrapper)) {
+            throw new \InvalidArgumentException(
+                sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
+            );
+        }
+
         foreach ($this->tariffTypes as $tariffType) {
-            foreach ($tariffType->withBehaviors() as $behavior) {
-                if ($behavior instanceof $behaviorClassWrapper) {
-                    yield $behavior;
-                }
-            }
+            yield from $this->findBehaviorsInCollection($tariffType->withBehaviors(), $behaviorClassWrapper);
         }

         foreach ($this->priceTypes() as $priceTypeDefinition) {
-            foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
-                if ($behavior instanceof $behaviorClassWrapper) {
-                    yield $behavior;
-                }
-            }
+            yield from $this->findBehaviorsInCollection($priceTypeDefinition->withBehaviors(), $behaviorClassWrapper);
         }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
$type = $this->convertStringTypeToType($type);
foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
return $behavior;
}
}
}
}
throw new BehaviorNotFoundException('Behaviour was not found');
}
public function getBehaviors(string $behaviorClassWrapper): \Generator
{
foreach ($this->tariffTypes as $tariffType) {
foreach ($tariffType->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
yield $behavior;
}
}
}
foreach ($this->priceTypes() as $priceTypeDefinition) {
foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
yield $behavior;
}
}
}
}
/**
* @param string $behaviorClassWrapper The behavior class to match
* @return \Generator<int, BehaviorInterface>
* @throws \InvalidArgumentException When behavior class is invalid
*/
private function findBehaviorsInCollection(iterable $collection, string $behaviorClassWrapper): \Generator
{
foreach ($collection as $behavior) {
if ($behavior instanceof $behaviorClassWrapper) {
yield $behavior;
}
}
}
/**
* @param string $type - full type like 'overuse,lb_capacity_unit'
* @param string $behaviorClassWrapper
* @return BehaviorInterface
* @throws BehaviorNotFoundException
* @throws \InvalidArgumentException When parameters are invalid
*/
public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
{
if (empty($type)) {
throw new \InvalidArgumentException('Type cannot be empty');
}
if (!class_exists($behaviorClassWrapper)) {
throw new \InvalidArgumentException(
sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
);
}
$type = $this->convertStringTypeToType($type);
foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
foreach ($this->findBehaviorsInCollection($priceTypeDefinition->withBehaviors(), $behaviorClassWrapper) as $behavior) {
return $behavior;
}
}
}
throw new BehaviorNotFoundException(
sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type)
);
}
/**
* @param string $behaviorClassWrapper The behavior class to match
* @return \Generator<int, BehaviorInterface>
* @throws \InvalidArgumentException When behavior class is invalid
*/
public function getBehaviors(string $behaviorClassWrapper): \Generator
{
if (!class_exists($behaviorClassWrapper)) {
throw new \InvalidArgumentException(
sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
);
}
foreach ($this->tariffTypes as $tariffType) {
yield from $this->findBehaviorsInCollection($tariffType->withBehaviors(), $behaviorClassWrapper);
}
foreach ($this->priceTypes() as $priceTypeDefinition) {
yield from $this->findBehaviorsInCollection($priceTypeDefinition->withBehaviors(), $behaviorClassWrapper);
}
}
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 104-104: UndefinedClass
src/product/BillingRegistry.php:104:22: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\TariffTypeBehaviorCollection does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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: 3

🧹 Nitpick comments (6)
src/product/AggregateNotFoundException.php (1)

7-9: Consider adding a default error message.

While extending RuntimeException is valid, including a default message or a context parameter could aid debugging. For instance:

-class AggregateNotFoundException extends RuntimeException
+class AggregateNotFoundException extends RuntimeException 
+{
+    public function __construct(string $type = '')
+    {
+        $message = $type !== '' 
+            ? "Aggregate not found for type '$type'"
+            : "Aggregate not found";
+        parent::__construct($message);
+    }
+}
src/product/ParentNodeDefinitionInterface.php (1)

5-10: Ensure comprehensive documentation for interface methods.

Documenting intent and usage in withBehaviors() and hasBehavior() allows future maintainers to implement or mock them correctly. Consider adding PHPDoc tags describing parameters and return types in more detail.

src/product/BillingRegistry.php (1)

41-44: Adjust docblock for more specific or correct interface references.

Static analysis indicates that @return RepresentationInterface[] may not match an actual, existing interface name. Update docblock references to reflect the correct interface or class name used in production code.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 43-43: UndefinedDocblockClass
src/product/BillingRegistry.php:43:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)

src/product/PriceTypeDefinition.php (3)

48-50: Consider adding input validation for $unit.

$this->unit = Unit::create($unit); will fail if $unit is invalid or empty. Add checks to ensure $unit is valid, and throw an exception if not.


72-75: Resolve TODO on formatter class checks.

Line 74 references a TODO to check the formatter’s validity. Add class existence checks and interface checks to ensure the formatter class implements QuantityFormatterInterface.


137-140: Translate non-English comments to English.

Lines 138-140 are written in another language. For better maintainability, convert them to English so that all developers can understand the code’s purpose.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1280f4c and 2a0421b.

📒 Files selected for processing (4)
  • src/product/AggregateNotFoundException.php (1 hunks)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/ParentNodeDefinitionInterface.php (1 hunks)
  • src/product/PriceTypeDefinition.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/BillingRegistry.php

[failure] 43-43: UndefinedDocblockClass
src/product/BillingRegistry.php:43:16: UndefinedDocblockClass: Docblock-defined class, interface or enum named hiqdev\billing\registry\invoice\RepresentationInterface does not exist (see https://psalm.dev/200)


[failure] 61-61: UndefinedClass
src/product/BillingRegistry.php:61:9: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\FractionQuantityData does not exist (see https://psalm.dev/019)


[failure] 105-105: UndefinedClass
src/product/BillingRegistry.php:105:22: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\behavior\TariffTypeBehaviorCollection does not exist (see https://psalm.dev/019)


[failure] 121-121: UndefinedClass
src/product/BillingRegistry.php:121:49: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Aggregate does not exist (see https://psalm.dev/019)

src/product/PriceTypeDefinition.php

[failure] 24-24: UndefinedClass
src/product/PriceTypeDefinition.php:24:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


[failure] 26-26: UndefinedClass
src/product/PriceTypeDefinition.php:26:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\invoice\InvoiceRepresentationCollection does not exist (see https://psalm.dev/019)

🔇 Additional comments (2)
src/product/BillingRegistry.php (2)

7-8: Confirm presence of classes from external package.

QuantityFormatterNotFoundException and FractionQuantityData point to hiqdev\billing\registry\quantity. If these classes live in an external package, ensure composer dependencies are in place. Otherwise, update the namespaces or adapt the code to existing classes.


85-100: 🛠️ Refactor suggestion

Validate behavior class.

getBehavior doesn't validate $behaviorClassWrapper before using instanceof. Consider adding existence checks and ensuring it implements the expected interface. Avoid runtime failures from invalid classes.

 public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
 {
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new \InvalidArgumentException(
+            "Behavior class '$behaviorClassWrapper' does not exist"
+        );
+    }
+
     $type = $this->convertStringTypeToType($type);
     ...
 }

Likely invalid or redundant comment.

Comment on lines 121 to 132
public function getAggregate(string $type): Aggregate
{
$type = $this->convertStringTypeToType($type);

foreach ($this->priceTypes() as $priceTypeDefinition) {
if ($priceTypeDefinition->hasType($type)) {
return $priceTypeDefinition->getAggregate();
}
}

throw new AggregateNotFoundException('Aggregate was not found');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

The Aggregate class appears to be missing from the codebase

The class Aggregate is imported from hiqdev\billing\registry\product\Aggregate namespace in both BillingRegistry.php and PriceTypeDefinition.php, but:

  • The class definition is not found in the codebase
  • It's not available in the vendor directory (which is not present in the sandbox)
  • The namespace suggests it should be in a package named hiqdev/billing-registry, but this package is not listed in composer dependencies

This could lead to runtime errors. You should either:

  • Add the missing package that contains this class to composer dependencies
  • Create the Aggregate class in the correct namespace if it's meant to be part of this package
  • Update the code to use a different class if the dependency was renamed or moved
🔗 Analysis chain

Validate existence of Aggregate class prior to usage.

Aggregate is imported from hiqdev\billing\registry\product\Aggregate, but static analysis flags it as undefined. Ensure the class is correctly included in composer or located in the repository. Otherwise, reference the correct namespace or rename as needed.


Let me search for more information about the Aggregate class.


Let me check if this is a dependency from composer by looking at composer.json.


Let me try one more search with proper escaping to find any references to this class.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify the Aggregate class existence 
rg "class Aggregate" -t php

Length of output: 130


Script:

#!/bin/bash
# Check for any file named Aggregate.php
fd Aggregate.php

# Check for any usage or import of Aggregate class
rg "use.*Aggregate" -t php

Length of output: 209


Script:

#!/bin/bash
# Check composer.json for the package
cat composer.json

# Also check if there are any other references to the Aggregate class
rg "hiqdev\\billing\\registry\\product\\Aggregate" -t php

Length of output: 3132


Script:

#!/bin/bash
# Search for the Aggregate class definition in vendor directory
fd -t f "Aggregate.php" vendor

# Search for references with proper escaping
rg -l "hiqdev\\\\billing\\\\registry\\\\product\\\\Aggregate"

Length of output: 248

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 121-121: UndefinedClass
src/product/BillingRegistry.php:121:49: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\product\Aggregate does not exist (see https://psalm.dev/019)

Comment on lines 5 to 16
use hiqdev\billing\registry\behavior\PriceTypeDefinitionBehaviourCollection;
use hiqdev\billing\registry\Domain\Model\TariffType;
use hiqdev\billing\registry\invoice\InvoiceRepresentationCollection;
use hiqdev\billing\registry\product\Aggregate;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterFactory;
use hiqdev\billing\registry\quantity\FractionQuantityData;
use hiqdev\billing\registry\Domain\Model\Unit\FractionUnit;
use hiqdev\php\billing\quantity\QuantityFormatterInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\Unit;
use hiqdev\php\units\UnitInterface;
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Missing required dependency hiqdev/billing-registry

The codebase heavily uses classes from the hiqdev\billing\registry namespace, but this dependency is not listed in the composer.json requirements. This could lead to runtime errors.

  • Add "hiqdev/billing-registry": "^version" to the require section in composer.json
  • Multiple files depend on this package:
    • src/product/PriceTypeDefinition.php
    • src/product/TariffTypeDefinition.php
    • src/product/BillingRegistry.php
    • And others
🔗 Analysis chain

Confirm external dependency availability.

PriceTypeDefinitionBehaviourCollection, InvoiceRepresentationCollection, and similar references to hiqdev\billing\registry\... classes imply an external or separate package. Verify that the correct dependency is installed and configured in composer.json.


Let me try a different approach to verify the dependency.


Let me check if the registry package exists in the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check composer.json for references to hiqdev\billing\registry
rg "hiqdev\\\\billing\\\\registry" composer.json

Length of output: 46


Script:

#!/bin/bash
# Check composer.json content to see all dependencies
cat composer.json

Length of output: 2946


Script:

#!/bin/bash
# Check if the registry package exists in the codebase
fd "registry" --type d

# Also check if there are any references to the registry namespace in the codebase
rg "hiqdev\\\\billing\\\\registry" -g "*.php"

Length of output: 2579

Copy link
Contributor

@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

♻️ Duplicate comments (4)
src/product/PriceTypeDefinition.php (4)

5-14: ⚠️ Potential issue

Add missing dependency hiqdev/billing-registry

Multiple classes are imported from the hiqdev\billing\registry namespace, but this dependency appears to be missing from composer.json. This will cause runtime errors.

Add the following to your composer.json:

{
    "require": {
+        "hiqdev/billing-registry": "^version"
    }
}

19-29: 🛠️ Refactor suggestion

Initialize class properties

Properties should be initialized to prevent undefined property access.

Apply this diff:

-    private Unit $unit;
+    private Unit $unit;

-    private string $description;
+    private string $description = '';

-    private QuantityFormatterDefinition $quantityFormatterDefinition;
+    private QuantityFormatterDefinition $quantityFormatterDefinition;

-    private Aggregate $aggregate;
+    private ?Aggregate $aggregate = null;
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 19-19: UndefinedClass
src/product/PriceTypeDefinition.php:19:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\Unit\Unit does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/PriceTypeDefinition.php:23:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)


71-77: ⚠️ Potential issue

Implement class existence validation

The TODO comment indicates missing validation for the formatter class.

Add validation to ensure the formatter class exists and implements the correct interface.


89-93: ⚠️ Potential issue

Add validation in end() method

The method should validate that all required properties are set before returning.

Add validation for required properties:

     public function end(): PriceTypeDefinitionCollection
     {
-        // Validate the PriceType and lock its state
+        if (!isset($this->unit)) {
+            throw new \RuntimeException('Unit is required but not set');
+        }
+        if (empty($this->description)) {
+            throw new \RuntimeException('Description is required but not set');
+        }
+        if (!isset($this->quantityFormatterDefinition)) {
+            throw new \RuntimeException('Quantity formatter is required but not set');
+        }
         return $this->parent;
     }
🧹 Nitpick comments (1)
src/product/PriceTypeDefinition.php (1)

31-40: Add constructor documentation and clarify initialization

The constructor lacks PHPDoc documentation, and the purpose of the init() method is unclear.

Add constructor documentation and clarify the hook's purpose:

+    /**
+     * @param PriceTypeDefinitionCollection $parent Parent collection managing this definition
+     * @param TypeInterface $type Price type being defined
+     * @param TariffType $tariffType Associated tariff type
+     */
     public function __construct(
         private readonly PriceTypeDefinitionCollection $parent,
         private readonly TypeInterface $type,
         TariffType $tariffType,
     ) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2a0421b and 78380f0.

📒 Files selected for processing (1)
  • src/product/PriceTypeDefinition.php (1 hunks)
🧰 Additional context used
🪛 GitHub Check: PHP 8.3
src/product/PriceTypeDefinition.php

[failure] 19-19: UndefinedClass
src/product/PriceTypeDefinition.php:19:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\Domain\Model\Unit\Unit does not exist (see https://psalm.dev/019)


[failure] 23-23: UndefinedClass
src/product/PriceTypeDefinition.php:23:13: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition does not exist (see https://psalm.dev/019)

🔇 Additional comments (2)
src/product/PriceTypeDefinition.php (2)

100-103: Remove unused method

The measuredWith method is not used anywhere in the codebase and its implementation is a no-op.

Remove this unused method.


137-139: Translate documentation to English

Documentation should be in English for better maintainability and collaboration.

Replace the documentation with:

-     * це параметер визначає агрегатну функцію яка застосовується для щоденно записаних ресурсів щоб визнизначти
-     * місячне споживання за яке потрібно пробілити клієнта
+     * This parameter defines the aggregate function that is applied to daily recorded resources
+     * to determine the monthly consumption for which the client needs to be billed

Copy link
Contributor

@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 (4)
src/product/TariffTypeDefinition.php (4)

17-21: Consider input validation in the constructor.
Currently, there is no validation of the $tariffType object. If the constructor is a critical entry point for the system, you may wish to sanity-check or enforce constraints (e.g., ensuring $tariffType is fully formed).


35-39: Add validation or usage for $suggesterClass.
Currently, the setPricesSuggester() method simply comments about validation but doesn’t apply it. Consider implementing validation or storing $suggesterClass if it’s intended for later usage. Otherwise, you may remove the method if it serves no purpose.


51-60: Improve readability & reduce complexity in hasBehavior().
The loop checks each $behavior against instanceof $behaviorClassName. You may refactor using array helpers or short-circuit logic to make it more concise. For instance:

return array_reduce(
    iterator_to_array($this->behaviorCollection),
    fn($carry, $behavior) => $carry || $behavior instanceof $behaviorClassName,
    false
);

62-66: Clarify the end() method’s concerns.
If the goal is to "lock its state," consider throwing exceptions or performing final validations that ensure TariffType is correct before returning $this. Listing which validations occur may also help future maintenance.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 78380f0 and da2c506.

📒 Files selected for processing (1)
  • src/product/TariffTypeDefinition.php (1 hunks)
🔇 Additional comments (2)
src/product/TariffTypeDefinition.php (2)

28-33: Potential null property usage.
The property $product is assigned in ofProduct() but is never checked or validated for null outside this method. If other methods rely on $product, they might operate on an uninitialized property.

Consider verifying if $product is always set before usage.


9-10: Class implements an unshown interface.
The file mentions ParentNodeDefinitionInterface, but it is not shown. Please ensure that the interface is defined and accessible.

Use the following script to confirm if the interface exists and is properly declared:

✅ Verification successful

Interface verification successful
The ParentNodeDefinitionInterface is properly defined in src/product/ParentNodeDefinitionInterface.php and is accessible within the same namespace.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for the declaration of ParentNodeDefinitionInterface
ast-grep --pattern $'interface ParentNodeDefinitionInterface {\n  $$$\n}'

Length of output: 72


Script:

#!/bin/bash
# Search for any mentions of ParentNodeDefinitionInterface
rg "ParentNodeDefinitionInterface" -A 2

# Get the namespace context and use statements from the implementing class
cat src/product/TariffTypeDefinition.php | head -n 10

Length of output: 1057

Comment on lines 5 to 13
use hiqdev\billing\registry\behavior\PriceTypeDefinitionBehaviourCollection;
use hiqdev\billing\registry\Domain\Model\TariffType;
use hiqdev\billing\registry\Domain\Model\Unit\Unit;
use hiqdev\billing\registry\invoice\InvoiceRepresentationCollection;
use hiqdev\billing\registry\product\Aggregate;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterDefinition;
use hiqdev\billing\registry\quantity\formatter\QuantityFormatterFactory;
use hiqdev\billing\registry\quantity\FractionQuantityData;
use hiqdev\billing\registry\Domain\Model\Unit\FractionUnit;
Copy link
Member

Choose a reason for hiding this comment

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

The same thing here.
We'll neet a call to figure it out.

Copy link
Contributor

@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

♻️ Duplicate comments (2)
src/product/price/PriceTypeDefinition.php (1)

132-132: ⚠️ Potential issue

Undefined class reference.

huiqdev\billing\registry\measure\RcpTrafCollector does not exist according to static analysis. This will cause an error at runtime if called.

Consider either removing this parameter or referencing a valid class/interface:

-public function measuredWith(\hiqdev\billing\registry\measure\RcpTrafCollector $param): self
+// Example fix: replace with an interface or existing class
+public function measuredWith(SomeValidMeasureInterface $param): self
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 132-132: UndefinedClass
src/product/price/PriceTypeDefinition.php:132:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

src/product/BillingRegistry.php (1)

171-181: 🛠️ Refactor suggestion

Include the requested type in AggregateNotFoundException
Providing the missing type in the error message simplifies debugging when an aggregate is not found.

-throw new AggregateNotFoundException('Aggregate was not found');
+throw new AggregateNotFoundException(
+    sprintf('Aggregate not found for type "%s"', $type)
+);
🧹 Nitpick comments (3)
src/product/price/PriceTypeDefinition.php (2)

20-39: Typed properties without defaults.

Properties like $unit, $description, and $quantityFormatterDefinition are typed but never initialized with defaults. Ensure these properties are set in practice to avoid "uninitialized property" errors.


81-109: Consider enforcing a stricter interface check for quantity formatters.

Currently, the quantityFormatter() method checks only for class existence, but not whether the class implements a required interface. If a class is found but does not implement QuantityFormatterInterface, runtime errors could occur.

You could add a check like:

 if (!\class_exists($formatterClass)) {
     throw new InvalidQuantityFormatterException("Formatter class $formatterClass does not exist");
 }
+if (!\is_subclass_of($formatterClass, QuantityFormatterInterface::class)) {
+    throw new InvalidQuantityFormatterException("Formatter class $formatterClass must implement QuantityFormatterInterface");
+}
src/product/invoice/InvoiceRepresentationCollection.php (1)

11-11: Missing template parameters in IteratorAggregate usage.

Psalm indicates that two template params are expected (TKey and TValue). Updating the class declaration can address this:

- class InvoiceRepresentationCollection implements \IteratorAggregate
+ class InvoiceRepresentationCollection implements \IteratorAggregate<int, RepresentationInterface>
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 11-11: MissingTemplateParam
src/product/invoice/InvoiceRepresentationCollection.php:11:50: MissingTemplateParam: hiqdev\php\billing\product\invoice\InvoiceRepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0e2bcb0 and d107199.

📒 Files selected for processing (4)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/invoice/InvoiceRepresentationCollection.php (1 hunks)
  • src/product/price/PriceTypeDefinition.php (1 hunks)
  • src/product/price/PriceTypeDefinitionCollection.php (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
src/product/BillingRegistry.php (1)
Learnt from: SilverFire
PR: hiqdev/php-billing#93
File: src/product/BillingRegistry.php:5-5
Timestamp: 2025-03-28T16:44:06.585Z
Learning: The `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. Use internal interfaces and implementations instead.
🧬 Code Definitions (4)
src/product/price/PriceTypeDefinitionCollection.php (7)
src/product/price/PriceTypeDefinition.php (3)
  • type (139-142)
  • __construct (40-52)
  • end (114-120)
src/product/price/PriceTypeStorage.php (4)
  • PriceTypeStorage (7-39)
  • getAll (23-33)
  • add (13-18)
  • count (35-38)
src/product/TariffTypeDefinition.php (3)
  • __construct (23-27)
  • tariffType (29-32)
  • end (83-95)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (2)
  • __construct (10-13)
  • end (15-20)
src/product/price/PriceTypeDefinitionCollectionInterface.php (3)
  • getIterator (13-13)
  • priceType (15-15)
  • end (17-17)
src/product/price/PriceTypeDefinitionFactoryInterface.php (1)
  • create (10-14)
src/product/TariffTypeDefinitionInterface.php (2)
  • tariffType (14-14)
  • end (28-28)
src/product/BillingRegistry.php (10)
src/product/behavior/InvalidBehaviorException.php (1)
  • InvalidBehaviorException (7-9)
src/product/Exception/AggregateNotFoundException.php (1)
  • AggregateNotFoundException (7-9)
src/product/Exception/BillingRegistryLockedException.php (1)
  • BillingRegistryLockedException (5-8)
src/product/invoice/InvalidRepresentationException.php (1)
  • InvalidRepresentationException (7-10)
src/product/price/PriceTypeDefinition.php (4)
  • PriceTypeDefinition (24-196)
  • createQuantityFormatter (100-109)
  • withBehaviors (154-159)
  • getAggregate (188-195)
src/product/quantity/QuantityFormatterNotFoundException.php (1)
  • QuantityFormatterNotFoundException (7-9)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/behavior/BehaviorNotFoundException.php (1)
  • BehaviorNotFoundException (7-9)
src/product/BillingRegistryInterface.php (1)
  • priceTypes (13-13)
src/product/TariffTypeDefinition.php (2)
  • withPrices (64-69)
  • withBehaviors (71-76)
src/product/invoice/InvoiceRepresentationCollection.php (4)
src/product/price/PriceTypeDefinition.php (4)
  • PriceTypeDefinition (24-196)
  • __construct (40-52)
  • type (139-142)
  • end (114-120)
src/product/invoice/RepresentationInterface.php (1)
  • setType (13-13)
src/product/TariffTypeDefinitionInterface.php (1)
  • end (28-28)
src/product/BillingRegistry.php (1)
  • lock (35-38)
src/product/price/PriceTypeDefinition.php (6)
src/product/Exception/AggregateNotDefinedException.php (1)
  • AggregateNotDefinedException (7-10)
src/product/quantity/InvalidQuantityFormatterException.php (1)
  • InvalidQuantityFormatterException (7-10)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/price/PriceTypeDefinitionFactoryInterface.php (1)
  • create (10-14)
src/product/price/PriceTypeDefinitionFactory.php (1)
  • create (10-16)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (17-17)
🪛 GitHub Check: PHP 8.3
src/product/invoice/InvoiceRepresentationCollection.php

[failure] 11-11: MissingTemplateParam
src/product/invoice/InvoiceRepresentationCollection.php:11:50: MissingTemplateParam: hiqdev\php\billing\product\invoice\InvoiceRepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinition.php

[failure] 132-132: UndefinedClass
src/product/price/PriceTypeDefinition.php:132:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

🔇 Additional comments (19)
src/product/price/PriceTypeDefinition.php (5)

1-19: Overall class definition looks good.

The namespace, imports, and class-level documentation are clear and follow a consistent structure.


40-52: Constructor usage is clear and well-structured.

The constructor properly initializes invoice and behavior collections, and calls init() for future extensibility.


54-79: Setters and getters are consistent.

The unit() and description() setters correctly guard against locked state. The getDescription() method returns the assigned description as expected.


133-170: Behavior methods are properly structured.

Methods measuredWith(), type(), hasType(), getUnit(), withBehaviors(), and hasBehavior() consistently check for locked state where needed. The design is cohesive for storing and retrieving behaviors.


172-195: Aggregation logic is clear and consistent.

The aggregation() setter and getAggregate() retrieval handle locked state and null checks appropriately. This ensures well-defined usage patterns.

src/product/invoice/InvoiceRepresentationCollection.php (5)

1-19: Namespace and class-level documentation are clear.

The use of HasLock trait and typed properties are consistent with the project's style.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 11-11: MissingTemplateParam
src/product/invoice/InvoiceRepresentationCollection.php:11:50: MissingTemplateParam: hiqdev\php\billing\product\invoice\InvoiceRepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


21-27: Iterator implementation is straightforward.

Returning an ArrayIterator for $representations is standard and efficient.


29-38: attach() usage looks correct.

It applies a lock check, sets the representation type, and appends to $representations. The method chaining approach is also convenient.


40-48: end() method correctly finalizes and returns the PriceType.

Locking the collection and returning $this->priceTypeDefinition aligns with the design seen in other classes.


50-53: filterByType() usage is concise and effective.

Filtering by $className with instanceof is straightforward and easy to understand.

src/product/price/PriceTypeDefinitionCollection.php (5)

1-29: Overall class definition is cohesive.

The constructor properly sets up $storage, $factory, and optionally $collectionInstance. The use of HasLock for concurrency control is consistent.


31-37: Iterator implementation is clean.

Returning an ArrayIterator of all definitions via $this->storage->getAll() is a fitting approach.


39-47: priceType() creation and storage appear correct.

Factory-based instantiation of new PriceTypeDefinition objects ensures a consistent pattern for new price types, and properly stores them.


49-58: end() method finalizes the definitions correctly.

Locking and returning $this->parent is a logical approach to indicate that the price definitions are no longer modifiable.


60-63: count() method effectively delegates to the underlying storage.

This aligns with the rest of the design and ensures consistent counting of tracked definitions.

src/product/BillingRegistry.php (4)

1-25: Overall design looks solid
The initial definitions, properties, and constructor logic appear to be clean and straightforward, with type declarations in place. No issues found in these lines.


26-33: Enforce uniqueness when adding Tariff Types
As previously suggested, consider checking for duplicates to safeguard against registering the same tariff type more than once.


63-67: Use an InvalidRepresentationException instead of InvalidBehaviorException
When verifying a representation class, it’s more accurate to throw an InvalidRepresentationException for non-implementing classes.


152-169: Replicate input validation for getBehaviors
Similar to getBehavior(), ensure the behavior class is valid (e.g., class_exists and implements BehaviorInterface) before iterating to avoid runtime errors.

Copy link
Contributor

@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 (4)
tests/unit/product/BillingRegistryTest.php (3)

59-63: Consider testing a valid but incompatible class scenario.

Currently, you only test with an obviously invalid class name. It might be beneficial to add a test where the class exists but doesn't implement RepresentationInterface, ensuring the code raises an exception as expected.


71-88: Expand negative coverage for getBehavior().

This test only validates the happy path. Consider also testing edge cases, such as when $behaviorClassWrapper exists but doesn't implement BehaviorInterface, to confirm the InvalidBehaviorException is thrown.


171-176: Avoid leaving commented-out tests in production code.

These lines are commented out, which might lead to confusion about whether this test is needed or outdated. Either remove them if they're no longer relevant or re-enable them as a skipped test (@depends or @group wip) to clarify the reason for deferral.

src/product/BillingRegistry.php (1)

91-94: Ensure non-empty string logic for convertStringTypeToType().

Currently, calling convertStringTypeToType('') creates a Type with an empty ID, which might be unintended. Consider validating $type is non-empty to avoid confusion and potential errors.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d107199 and b3bb340.

📒 Files selected for processing (3)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/trait/HasLock.php (1 hunks)
  • tests/unit/product/BillingRegistryTest.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/trait/HasLock.php
🧰 Additional context used
🧠 Learnings (1)
src/product/BillingRegistry.php (1)
Learnt from: SilverFire
PR: hiqdev/php-billing#93
File: src/product/BillingRegistry.php:5-5
Timestamp: 2025-04-03T08:52:34.233Z
Learning: The `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. Use internal interfaces and implementations instead.
🧬 Code Definitions (1)
tests/unit/product/BillingRegistryTest.php (5)
src/product/BillingRegistry.php (5)
  • BillingRegistry (20-189)
  • addTariffType (28-33)
  • priceTypes (39-46)
  • getAggregate (166-177)
  • getBehavior (103-132)
tests/unit/product/behavior/TestBehavior.php (2)
  • TestBehavior (7-20)
  • getContext (16-19)
tests/unit/product/Domain/Model/DummyTariffType.php (1)
  • DummyTariffType (7-18)
tests/unit/product/Domain/Model/FakeTariffType.php (1)
  • FakeTariffType (7-18)
src/product/trait/HasLock.php (1)
  • lock (11-15)
🔇 Additional comments (2)
src/product/BillingRegistry.php (2)

58-62: Use InvalidRepresentationException consistently instead of InvalidBehaviorException.

Throwing InvalidBehaviorException for a failure to implement RepresentationInterface is inconsistent. The test references using InvalidRepresentationException for representational class errors, which matches the method's semantics.


147-164: Add input validation for class existence and interface implementation in getBehaviors().

Unlike getBehavior(), getBehaviors() omits checks for whether $behaviorClassWrapper is valid and implements BehaviorInterface. Align this method with getBehavior() to improve consistency and error handling.

Copy link
Contributor

@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: 2

♻️ Duplicate comments (3)
src/product/price/PriceTypeDefinition.php (1)

131-131: ⚠️ Potential issue

Undefined class reference.

The class \hiqdev\billing\registry\measure\RcpTrafCollector does not exist in the codebase, which will cause errors. Replace or remove the parameter type to avoid a runtime error.

-public function measuredWith(\hiqdev\billing\registry\measure\RcpTrafCollector $param): self
+public function measuredWith(/* Replace with a valid class or type */ $param): self
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 131-131: UndefinedClass
src/product/price/PriceTypeDefinition.php:131:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

src/product/TariffTypeDefinition.php (1)

13-13: ⚠️ Potential issue

Resolve Psalm's missing template parameter error.

The interface expects 1 template parameter. Add the necessary annotation to fix the pipeline failure:

+/**
+ * @template T
+ * @implements TariffTypeDefinitionInterface<T>
+ */
 class TariffTypeDefinition implements TariffTypeDefinitionInterface
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 13-13: MissingTemplateParam
src/product/TariffTypeDefinition.php:13:39: MissingTemplateParam: hiqdev\php\billing\product\TariffTypeDefinition has missing template params when extending hiqdev\php\billing\product\TariffTypeDefinitionInterface, expecting 1 (see https://psalm.dev/182)

🪛 GitHub Actions: Psalm Static Analysis

[error] 13-13: MissingTemplateParam: hiqdev\php\billing\product\TariffTypeDefinition has missing template params when extending hiqdev\php\billing\product\TariffTypeDefinitionInterface, expecting 1 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinitionInterface.php (1)

31-31: ⚠️ Potential issue

Replace or remove reference to \hiqdev\billing\registry\measure\RcpTrafCollector.
This class cannot be resolved and triggers a static analysis failure. If it's truly required, ensure the correct dependency is installed and namespace is updated. Otherwise, remove or comment out this method until the correct type is confirmed.

 // TODO: Not sure if it will be needed at all
-public function measuredWith(\hiqdev\billing\registry\measure\RcpTrafCollector $param): self;
+// public function measuredWith(/* Confirm correct class or remove */ $param): self;
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 31-31: UndefinedClass
src/product/price/PriceTypeDefinitionInterface.php:31:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

🧹 Nitpick comments (10)
src/product/behavior/BehaviorCollectionInterface.php (2)

11-14: Improve PHPDoc for getIterator() method

The current PHPDoc doesn't fully align with the return type declaration. To better reflect the expected return type and improve type safety, consider updating the PHPDoc annotation.

/**
- * @return BehaviorInterface[]
+ * @return \Traversable<int|string, BehaviorInterface>
 */

18-21: Consider adding more context to the end() method's PHPDoc

The method returns either a tariff or price type definition, but the PHPDoc doesn't explain why or under what conditions each type is returned. Additional context would improve maintainability.

/**
 * @return TariffTypeDefinitionInterface|PriceTypeDefinitionInterface
+ * @description Returns the parent definition object that this collection belongs to,
+ * allowing for a fluent interface pattern to continue from the parent context.
 */
src/product/behavior/TariffTypeBehaviorRegistry.php (1)

31-50: Possible optimization for hasBehavior
The current linear search is straightforward. If performance or scale becomes an issue, consider storing behaviors in a keyed structure (e.g., a dictionary keyed by FQCN) for O(1) lookups.

src/product/trait/HasLock.php (1)

36-45: Type checking for lockable items
lockItems() assumes all items implement HasLockInterface. Consider verifying the type in a loop to prevent potential TypeError if non-lockable items slip through.

src/product/invoice/InvoiceRepresentationCollection.php (1)

49-52: Consider returning a typed collection instead of a raw array.

Refactoring filterByType to return a new InvoiceRepresentationCollection will enable chaining and consistent handling of filtered items:

-public function filterByType(string $className): array
+public function filterByType(string $className): self
 {
     $filtered = array_filter($this->representations, fn($r) => $r instanceof $className);
+    $collection = new self($this->priceTypeDefinition);
+    foreach ($filtered as $representation) {
+        $collection->attach($representation);
+    }
+    return $collection;
-    return $filtered;
 }
src/product/price/PriceTypeDefinitionCollection.php (1)

32-32: Correct the doc block spelling.

Change @inerhitDoc to @inheritDoc to match standard PHPDoc usage.

- * @inerhitDoc
+ * @inheritDoc
src/product/TariffTypeDefinition.php (1)

60-60: Implement or remove the mention of suggester validation.

Currently, there's a placeholder comment indicating that you plan to validate/store the suggester class. Would you like help implementing this logic or opening a new issue?

src/product/BillingRegistry.php (3)

28-33: Consider preventing duplicate tariff types.
Currently, addTariffType appends tariff type definitions unconditionally. Kindly consider validating for duplicates (e.g., comparing IDs or similar attributes) to avoid inconsistent registry states.

 public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void
 {
     $this->ensureNotLocked();

+    foreach ($this->tariffTypeDefinitions as $existing) {
+        if ($existing->getId() === $tariffTypeDefinition->getId()) {
+            throw new \RuntimeException(
+                "Tariff type with ID '{$tariffTypeDefinition->getId()}' already exists in the registry."
+            );
+        }
+    }

     $this->tariffTypeDefinitions[] = $tariffTypeDefinition;
 }

48-74: Unify exception usage for invalid representation classes.
Currently, a missing class is handled by throwing InvalidRepresentationException, but a non-implementing class triggers InvalidBehaviorException at line 59. Using a single exception type (e.g., InvalidRepresentationException) for both could make error handling more consistent.

 if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
-    throw new InvalidBehaviorException(
+    throw new InvalidRepresentationException(
         sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
     );
 }

147-164: Consider validating the behavior class in getBehaviors.
Unlike getBehavior, this method does not confirm that the $behaviorClassWrapper is a valid or existing class. For consistency and error prevention, consider duplicating those checks here.

 public function getBehaviors(string $behaviorClassWrapper): \Generator
 {
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new InvalidBehaviorException(
+            sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
+        );
+    }
+    if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
+        throw new InvalidBehaviorException(
+            sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
+        );
+    }

     foreach ($this->tariffTypeDefinitions as $tariffTypeDefinition) {
         ...
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4492d45 and 0b49dfb.

📒 Files selected for processing (15)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/BillingRegistryInterface.php (1 hunks)
  • src/product/TariffTypeDefinition.php (1 hunks)
  • src/product/TariffTypeDefinitionInterface.php (1 hunks)
  • src/product/behavior/BehaviorCollectionInterface.php (1 hunks)
  • src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (1 hunks)
  • src/product/behavior/BehaviorTariffTypeCollection.php (1 hunks)
  • src/product/behavior/TariffTypeBehaviorRegistry.php (1 hunks)
  • src/product/invoice/InvoiceRepresentationCollection.php (1 hunks)
  • src/product/price/PriceTypeDefinition.php (1 hunks)
  • src/product/price/PriceTypeDefinitionCollection.php (1 hunks)
  • src/product/price/PriceTypeDefinitionCollectionInterface.php (1 hunks)
  • src/product/price/PriceTypeDefinitionInterface.php (1 hunks)
  • src/product/trait/HasLock.php (1 hunks)
  • src/product/trait/HasLockInterface.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/product/BillingRegistryInterface.php
  • src/product/behavior/BehaviorPriceTypeDefinitionCollection.php
  • src/product/behavior/BehaviorTariffTypeCollection.php
🧰 Additional context used
🧠 Learnings (2)
src/product/BillingRegistry.php (1)
Learnt from: SilverFire
PR: hiqdev/php-billing#93
File: src/product/BillingRegistry.php:5-5
Timestamp: 2025-04-03T08:52:34.233Z
Learning: The `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. Use internal interfaces and implementations instead.
src/product/TariffTypeDefinition.php (1)
Learnt from: SilverFire
PR: hiqdev/php-billing#93
File: src/product/BillingRegistry.php:5-5
Timestamp: 2025-04-03T08:52:34.233Z
Learning: The `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. Use internal interfaces and implementations instead.
🧬 Code Definitions (10)
src/product/trait/HasLockInterface.php (2)
src/product/behavior/TariffTypeBehaviorRegistry.php (1)
  • lock (52-55)
src/product/trait/HasLock.php (1)
  • lock (11-17)
src/product/trait/HasLock.php (6)
src/product/Exception/LockedException.php (1)
  • LockedException (7-10)
src/product/behavior/TariffTypeBehaviorRegistry.php (1)
  • lock (52-55)
src/product/trait/HasLockInterface.php (1)
  • lock (7-7)
src/product/TariffTypeDefinition.php (1)
  • afterLock (95-99)
src/product/BillingRegistry.php (1)
  • afterLock (190-193)
src/product/price/PriceTypeDefinition.php (1)
  • afterLock (196-200)
src/product/price/PriceTypeDefinitionCollectionInterface.php (7)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/price/PriceTypeDefinitionCollection.php (3)
  • getIterator (34-37)
  • priceType (39-47)
  • end (53-56)
src/product/behavior/BehaviorCollectionInterface.php (2)
  • getIterator (14-14)
  • end (21-21)
src/product/behavior/BehaviorCollection.php (1)
  • getIterator (19-22)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (1)
  • end (15-18)
src/product/behavior/BehaviorTariffTypeCollection.php (1)
  • end (15-18)
src/product/TariffTypeDefinitionInterface.php (1)
  • end (29-29)
src/product/TariffTypeDefinitionInterface.php (7)
src/product/TariffTypeDefinition.php (6)
  • tariffType (29-32)
  • ofProduct (34-40)
  • getProduct (42-47)
  • setPricesSuggester (56-62)
  • withPrices (64-69)
  • end (83-93)
src/product/price/PriceTypeDefinitionCollection.php (1)
  • end (53-56)
src/product/behavior/BehaviorCollectionInterface.php (1)
  • end (21-21)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (1)
  • end (15-18)
src/product/behavior/BehaviorTariffTypeCollection.php (1)
  • end (15-18)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
src/product/price/PriceTypeDefinitionInterface.php (1)
  • end (26-26)
src/product/invoice/InvoiceRepresentationCollection.php (4)
src/product/trait/HasLock.php (1)
  • ensureNotLocked (19-24)
src/product/invoice/RepresentationInterface.php (1)
  • setType (13-13)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/TariffTypeDefinitionInterface.php (1)
  • end (29-29)
src/product/price/PriceTypeDefinitionCollection.php (4)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/price/PriceTypeStorage.php (4)
  • PriceTypeStorage (7-39)
  • getAll (23-33)
  • add (13-18)
  • count (35-38)
src/product/price/PriceTypeDefinitionFactoryInterface.php (1)
  • create (10-14)
src/product/TariffTypeDefinitionInterface.php (2)
  • tariffType (15-15)
  • end (29-29)
src/product/BillingRegistry.php (11)
src/product/behavior/InvalidBehaviorException.php (1)
  • InvalidBehaviorException (7-9)
src/product/Exception/AggregateNotFoundException.php (1)
  • AggregateNotFoundException (7-9)
src/product/price/PriceTypeDefinition.php (7)
  • PriceTypeDefinition (24-201)
  • type (138-141)
  • documentRepresentation (124-129)
  • createQuantityFormatter (101-110)
  • hasType (143-146)
  • withBehaviors (153-158)
  • getAggregate (187-194)
src/product/quantity/QuantityFormatterNotFoundException.php (1)
  • QuantityFormatterNotFoundException (7-9)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/behavior/BehaviorNotFoundException.php (1)
  • BehaviorNotFoundException (7-9)
src/product/price/PriceTypeDefinitionInterface.php (5)
  • type (33-33)
  • documentRepresentation (28-28)
  • createQuantityFormatter (24-24)
  • hasType (35-35)
  • getAggregate (48-48)
src/product/trait/HasLock.php (1)
  • ensureNotLocked (19-24)
src/product/BillingRegistryInterface.php (1)
  • priceTypes (14-14)
src/product/TariffTypeDefinition.php (2)
  • withPrices (64-69)
  • withBehaviors (71-76)
src/product/TariffTypeDefinitionInterface.php (1)
  • withPrices (27-27)
src/product/price/PriceTypeDefinitionInterface.php (10)
src/product/invoice/InvoiceRepresentationCollection.php (2)
  • InvoiceRepresentationCollection (12-53)
  • end (44-47)
src/product/price/PriceTypeDefinition.php (13)
  • type (138-141)
  • unit (59-66)
  • description (68-75)
  • getDescription (77-80)
  • quantityFormatter (88-99)
  • createQuantityFormatter (101-110)
  • end (115-119)
  • documentRepresentation (124-129)
  • measuredWith (131-136)
  • hasType (143-146)
  • getUnit (148-151)
  • aggregation (174-181)
  • getAggregate (187-194)
src/product/BillingRegistry.php (2)
  • createQuantityFormatter (76-89)
  • getAggregate (166-177)
src/product/behavior/BehaviorCollectionInterface.php (1)
  • end (21-21)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (1)
  • end (15-18)
src/product/behavior/BehaviorTariffTypeCollection.php (1)
  • end (15-18)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
src/product/price/PriceTypeDefinitionCollection.php (1)
  • end (53-56)
src/product/TariffTypeDefinitionInterface.php (1)
  • end (29-29)
src/product/TariffTypeDefinition.php (1)
  • end (83-93)
src/product/TariffTypeDefinition.php (6)
src/product/behavior/BehaviorTariffTypeCollection.php (3)
  • BehaviorTariffTypeCollection (8-19)
  • __construct (10-13)
  • end (15-18)
src/product/behavior/TariffTypeBehaviorRegistry.php (5)
  • TariffTypeBehaviorRegistry (27-56)
  • __construct (31-34)
  • getBehaviors (36-39)
  • hasBehavior (41-50)
  • lock (52-55)
src/product/Exception/ProductNotDefinedException.php (1)
  • ProductNotDefinedException (7-10)
src/product/price/PriceTypeDefinitionCollection.php (4)
  • PriceTypeDefinitionCollection (14-62)
  • __construct (22-29)
  • end (53-56)
  • count (58-61)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
src/product/trait/HasLockInterface.php (1)
  • lock (7-7)
src/product/price/PriceTypeDefinition.php (5)
src/product/Exception/AggregateNotDefinedException.php (1)
  • AggregateNotDefinedException (7-10)
src/product/quantity/InvalidQuantityFormatterException.php (1)
  • InvalidQuantityFormatterException (7-10)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/trait/HasLock.php (2)
  • ensureNotLocked (19-24)
  • lock (11-17)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
🪛 GitHub Check: PHP 8.3
src/product/behavior/BehaviorCollectionInterface.php

[failure] 9-9: MissingTemplateParam
src/product/behavior/BehaviorCollectionInterface.php:9:47: MissingTemplateParam: hiqdev\php\billing\product\behavior\BehaviorCollectionInterface has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinitionCollectionInterface.php

[failure] 9-9: MissingTemplateParam
src/product/price/PriceTypeDefinitionCollectionInterface.php:9:58: MissingTemplateParam: hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/invoice/InvoiceRepresentationCollection.php

[failure] 12-12: MissingTemplateParam
src/product/invoice/InvoiceRepresentationCollection.php:12:50: MissingTemplateParam: hiqdev\php\billing\product\invoice\InvoiceRepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinitionInterface.php

[failure] 31-31: UndefinedClass
src/product/price/PriceTypeDefinitionInterface.php:31:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

src/product/TariffTypeDefinition.php

[failure] 13-13: MissingTemplateParam
src/product/TariffTypeDefinition.php:13:39: MissingTemplateParam: hiqdev\php\billing\product\TariffTypeDefinition has missing template params when extending hiqdev\php\billing\product\TariffTypeDefinitionInterface, expecting 1 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinition.php

[failure] 131-131: UndefinedClass
src/product/price/PriceTypeDefinition.php:131:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

🪛 GitHub Actions: Psalm Static Analysis
src/product/TariffTypeDefinition.php

[error] 13-13: MissingTemplateParam: hiqdev\php\billing\product\TariffTypeDefinition has missing template params when extending hiqdev\php\billing\product\TariffTypeDefinitionInterface, expecting 1 (see https://psalm.dev/182)

🔇 Additional comments (22)
src/product/behavior/BehaviorCollectionInterface.php (1)

16-16: Method signature looks good

The attach() method has a clear purpose and follows the fluent interface pattern by returning self.

src/product/behavior/TariffTypeBehaviorRegistry.php (3)

1-26: Well-documented class purpose
This doc block provides a clear overview of the class’s responsibilities and rationale, aiding maintainability. Keep it updated as features evolve.


27-30: Final class and strict property typing
Declaring the class as final and using typed properties help enforce intended usage patterns and maintain type safety. This is a good practice.


52-55: Lock delegation
Delegating the lock to the underlying behavior collection is consistent with the HasLockInterface contract and keeps the registry’s locking logic simple.

src/product/trait/HasLockInterface.php (1)

1-8: Interface definition
Defining a minimal interface for locking explicitly clarifies the contract, fostering consistency across different classes.

src/product/trait/HasLock.php (3)

7-9: Private boolean lock state
Storing the locked state in a private property is a clean approach. It prevents external tampering and encourages use of the provided methods.


11-17: Repeated lock calls
Currently, multiple calls to lock() do not throw an exception if already locked. Verify that this behavior is intentional or consider guarding repeated calls with an early return or exception.


19-24: Enforcing immutability
ensureNotLocked() effectively prevents changes after locking, aligning with the concept of immutable objects post-lock.

src/product/price/PriceTypeDefinitionCollectionInterface.php (1)

9-9: Add the missing template parameters for \IteratorAggregate.

This interface extends \IteratorAggregate, which requires two template parameters, but they are missing. Consider adding them to ensure better type safety and to resolve the static analysis warning.

+/**
+ * @extends \IteratorAggregate<int, PriceTypeDefinitionInterface>
+ */
 interface PriceTypeDefinitionCollectionInterface extends \IteratorAggregate, \Countable, HasLockInterface
 {
     // ...
 }
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 9-9: MissingTemplateParam
src/product/price/PriceTypeDefinitionCollectionInterface.php:9:58: MissingTemplateParam: hiqdev\php\billing\product\price\PriceTypeDefinitionCollectionInterface has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinition.php (2)

77-80: Add a null check for getDescription().

Accessing $this->description before it's defined could cause runtime errors. The following snippet shows how to guard against it.

 public function getDescription(): string
 {
+    if (!isset($this->description)) {
+        throw new \LogicException('Description has not been defined. Call description() method first.');
+    }
     return $this->description;
 }

148-151: Add a null check for getUnit().

Accessing $this->unit before it's defined could cause runtime errors. Add a guard clause to avoid undefined property access.

 public function getUnit(): UnitInterface
 {
+    if (!isset($this->unit)) {
+        throw new \LogicException('Unit has not been defined. Call unit() method first.');
+    }
     return $this->unit;
 }
src/product/TariffTypeDefinitionInterface.php (1)

13-29: All definitions look good.

The interface methods, return types, and doc-blocks appear consistent with your codebase. No additional issues found.

src/product/BillingRegistry.php (10)

5-19: Imports look consistent and fulfill the necessary dependencies.
No issues spotted with the namespaces or classes referenced, as they all appear to be defined within hiqdev\php\billing\product or its sub-namespaces.


20-23: Class declaration is coherent and ensures the registry can be locked.
Implementing BillingRegistryInterface and using HasLock is well-structured, allowing external code to treat this as a standard billing registry.


35-46: Verify behavior with no tariff types or no prices.
While the generator-based approach is correct and concise, ensure other parts of the system can gracefully handle the situation when there are no tariff types or the tariff types have no prices.

Do you want me to generate a script to check that no consumer code breaks if priceTypes() yields zero results?


76-89: Quantity formatter creation appears correct.
The search for matching price types and the fallback to throw QuantityFormatterNotFoundException are aligned with the intended design.


91-94: String-to-type conversion is succinct and valid.
Invoking Type::anyId($type) cleanly abstracts additional complexities of type creation.


96-132: Thorough validation for behavior class in getBehavior.
Well done validating class_exists and is_subclass_of prior to searching. This approach prevents subtle runtime errors.


134-145: Helper method for finding behavior is helpful.
findBehaviorInPriceType method is a clean, reusable abstraction.


166-177: Aggregate retrieval logic is consistent with the rest of the design.
The loop effectively searches for a matching price type and throws AggregateNotFoundException if none is found.


179-188: Tariff type definitions retrieval meets standard expectations.
Providing a generator for TariffTypeDefinitionInterface allows flexible iteration without forcing array allocation.


190-193: Final locking stage is properly delegated.
Automatically locking child items ($this->lockItems) ensures consistent immutability once the registry is finalized.

use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
use hiqdev\php\billing\product\trait\HasLockInterface;

interface BehaviorCollectionInterface extends \IteratorAggregate, HasLockInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add generic type parameters to IteratorAggregate interface extension

The static analysis tool flagged that IteratorAggregate requires template parameters in PHP 8.3. To fix this and maintain type safety, specify the key and value types for the iterator.

-interface BehaviorCollectionInterface extends \IteratorAggregate, HasLockInterface
+interface BehaviorCollectionInterface extends \IteratorAggregate<int|string, BehaviorInterface>, HasLockInterface
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
interface BehaviorCollectionInterface extends \IteratorAggregate, HasLockInterface
interface BehaviorCollectionInterface extends \IteratorAggregate<int|string, BehaviorInterface>, HasLockInterface
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 9-9: MissingTemplateParam
src/product/behavior/BehaviorCollectionInterface.php:9:47: MissingTemplateParam: hiqdev\php\billing\product\behavior\BehaviorCollectionInterface has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

/**
* @template T of PriceTypeDefinition
*/
class InvoiceRepresentationCollection implements \IteratorAggregate, HasLockInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Address missing template parameters in class declaration.

Psalm reports a missing template parameter for IteratorAggregate. You can fix this by specifying the appropriate template parameters in the doc block, for example:

-/**
- * @template T of PriceTypeDefinition
- */
+/**
+ * @template K as array-key
+ * @template V as RepresentationInterface
+ * @extends \IteratorAggregate<K, V>
+ * @template T of PriceTypeDefinition
+ */
 class InvoiceRepresentationCollection implements \IteratorAggregate, HasLockInterface
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class InvoiceRepresentationCollection implements \IteratorAggregate, HasLockInterface
/**
* @template K as array-key
* @template V as RepresentationInterface
* @extends \IteratorAggregate<K, V>
* @template T of PriceTypeDefinition
*/
class InvoiceRepresentationCollection implements \IteratorAggregate, HasLockInterface
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 12-12: MissingTemplateParam
src/product/invoice/InvoiceRepresentationCollection.php:12:50: MissingTemplateParam: hiqdev\php\billing\product\invoice\InvoiceRepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

Copy link
Contributor

@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: 1

🔭 Outside diff range comments (1)
tests/unit/product/TariffTypeDefinitionTest.php (1)

1-86: 🛠️ Refactor suggestion

Additional test coverage needed for end() method.

The existing tests don't fully validate the end() method's behavior. Based on the TariffTypeDefinition implementation, end() should throw a LogicException if no price types are defined.

Add a dedicated test for the end() method's validation behavior:

public function testEndThrowsExceptionWithoutPriceTypes(): void
{
    $tariffType = $this->createMock(TariffTypeInterface::class);
    $product = $this->createMock(ProductInterface::class);
    $definition = new TariffTypeDefinition($tariffType);
    $definition->ofProduct($product);
    
    $this->expectException(\LogicException::class);
    $definition->end();
}
🧹 Nitpick comments (2)
tests/unit/product/TariffTypeDefinitionTest.php (2)

26-33: LGTM: TestOfProduct verifies return type.

This test appropriately checks that the ofProduct method returns an instance of TariffTypeDefinitionInterface. The test could be enhanced by also verifying that the product was actually set.

 public function testOfProduct(): void
 {
     $tariffType = $this->createMock(TariffTypeInterface::class);
     $product = $this->createMock(ProductInterface::class);
     $definition = new TariffTypeDefinition($tariffType);

     $this->assertInstanceOf(TariffTypeDefinitionInterface::class, $definition->ofProduct($product));
+    $this->assertSame($product, $definition->getProduct(), 'Product should be correctly set by ofProduct method');
 }

54-60: LGTM: TestWithPrices verifies return type.

The test confirms that withPrices() returns an instance of PriceTypeDefinitionCollection. Consider adding a test to verify that the returned collection is associated with the definition.

Consider adding a test that verifies the returned PriceTypeDefinitionCollection is properly associated with the definition (parent relationship). This would ensure the fluent interface works correctly when chaining methods.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0b49dfb and 0d8bd65.

📒 Files selected for processing (1)
  • tests/unit/product/TariffTypeDefinitionTest.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
tests/unit/product/TariffTypeDefinitionTest.php (3)
src/product/price/PriceTypeDefinition.php (3)
  • type (138-141)
  • withBehaviors (153-158)
  • end (115-119)
src/product/Exception/ProductNotDefinedException.php (1)
  • ProductNotDefinedException (7-10)
src/product/TariffTypeDefinition.php (7)
  • TariffTypeDefinition (13-100)
  • tariffType (29-32)
  • ofProduct (34-40)
  • getProduct (42-47)
  • withPrices (64-69)
  • withBehaviors (71-76)
  • end (83-93)
🔇 Additional comments (5)
tests/unit/product/TariffTypeDefinitionTest.php (5)

1-15: File structure and imports look good.

The test file has the proper namespace, imports all required classes, and follows PSR standards with strict typing. The organization is clean and the TestCase extension is correct.


16-24: LGTM: TestTariffType verifies core functionality.

The test correctly verifies that the tariffType() method returns the same tariff type that was passed to the constructor. The mocking strategy is appropriate for a unit test.


35-43: LGTM: TestGetProduct verifies correct product retrieval.

This test properly verifies that getProduct() returns the same product that was set via ofProduct().


45-52: LGTM: Exception test handles negative case properly.

The test correctly verifies that ProductNotDefinedException is thrown when getProduct() is called without setting a product first.


62-68: LGTM: TestWithBehaviors verifies return type.

Similar to the withPrices test, this correctly verifies that withBehaviors() returns a BehaviorTariffTypeCollection instance.

Comment on lines +70 to +84
public function testLockPreventsModification(): void
{
$tariffType = $this->createMock(TariffTypeInterface::class);
$product = $this->createMock(ProductInterface::class);
$definition = new TariffTypeDefinition($tariffType);
$definition->ofProduct($product);
$definition->withPrices()
->priceType(Type::anyId('dummy'));
$definition->end();

$definition->lock();

$this->expectException(LockedException::class);
$definition->ofProduct($this->createMock(ProductInterface::class));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Test for locking mechanism needs validation of price type.

The test verifies that the lock prevents modification, which is good. However, the test is missing validation that the price type added on line 77 is actually required for the end() method to succeed, as seen in the implementation's end() method which throws an exception if no price types are defined.

 public function testLockPreventsModification(): void
 {
     $tariffType = $this->createMock(TariffTypeInterface::class);
     $product = $this->createMock(ProductInterface::class);
     $definition = new TariffTypeDefinition($tariffType);
     $definition->ofProduct($product);
     $definition->withPrices()
         ->priceType(Type::anyId('dummy'));
     $definition->end();

+    // Verify that end() would throw exception without price types
+    $anotherDefinition = new TariffTypeDefinition($tariffType);
+    $anotherDefinition->ofProduct($product);
+    $this->expectException(\LogicException::class);
+    $anotherDefinition->end();
+}
+
+public function testLockPreventsModificationAfterLock(): void
+{
+    $tariffType = $this->createMock(TariffTypeInterface::class);
+    $product = $this->createMock(ProductInterface::class);
+    $definition = new TariffTypeDefinition($tariffType);
+    $definition->ofProduct($product);
+    $definition->withPrices()
+        ->priceType(Type::anyId('dummy'));
+    $definition->end();
     $definition->lock();

     $this->expectException(LockedException::class);
     $definition->ofProduct($this->createMock(ProductInterface::class));
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function testLockPreventsModification(): void
{
$tariffType = $this->createMock(TariffTypeInterface::class);
$product = $this->createMock(ProductInterface::class);
$definition = new TariffTypeDefinition($tariffType);
$definition->ofProduct($product);
$definition->withPrices()
->priceType(Type::anyId('dummy'));
$definition->end();
$definition->lock();
$this->expectException(LockedException::class);
$definition->ofProduct($this->createMock(ProductInterface::class));
}
public function testLockPreventsModification(): void
{
$tariffType = $this->createMock(TariffTypeInterface::class);
$product = $this->createMock(ProductInterface::class);
$definition = new TariffTypeDefinition($tariffType);
$definition->ofProduct($product);
$definition->withPrices()
->priceType(Type::anyId('dummy'));
$definition->end();
// Verify that end() would throw exception without price types
$anotherDefinition = new TariffTypeDefinition($tariffType);
$anotherDefinition->ofProduct($product);
$this->expectException(\LogicException::class);
$anotherDefinition->end();
}
public function testLockPreventsModificationAfterLock(): void
{
$tariffType = $this->createMock(TariffTypeInterface::class);
$product = $this->createMock(ProductInterface::class);
$definition = new TariffTypeDefinition($tariffType);
$definition->ofProduct($product);
$definition->withPrices()
->priceType(Type::anyId('dummy'));
$definition->end();
$definition->lock();
$this->expectException(LockedException::class);
$definition->ofProduct($this->createMock(ProductInterface::class));
}

Copy link
Contributor

@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 (6)
src/product/BillingRegistry.php (4)

27-32: Consider preventing duplicate tariff types.

Including a duplication check here would help maintain data integrity. A quick equality check or ID comparison against existing tariff types can avoid inconsistent registry states.

 public function addTariffType(TariffTypeDefinitionInterface $tariffTypeDefinition): void
 {
     $this->ensureNotLocked();

+    foreach ($this->tariffTypeDefinitions as $existingTariff) {
+        if ($existingTariff->getId() === $tariffTypeDefinition->getId()) {
+            throw new \RuntimeException(
+                "Tariff type with ID '{$tariffTypeDefinition->getId()}' already exists."
+            );
+        }
+    }

     $this->tariffTypeDefinitions[] = $tariffTypeDefinition;
 }

49-53: Unify exception usage for representation validation.

Using InvalidBehaviorException to report a non-implementing representation class might be confusing. You could employ InvalidRepresentationException for consistency and clarity, as this check focuses on representation alignment rather than behavior.

 if (!is_subclass_of($representationClass, RepresentationInterface::class)) {
-    throw new InvalidBehaviorException(
+    throw new InvalidRepresentationException(
         sprintf(
-            'Representation class "%s" does not implement RepresentationInterface',
+            'Representation class "%s" does not implement the required interface',
             $representationClass
         )
     );
 }

131-148: Align validity checks with getBehavior.

Unlike getBehavior(), getBehaviors() does not verify that the requested class exists or implements BehaviorInterface. Not applying these checks could lead to silent failures when using an invalid class name.

 public function getBehaviors(string $behaviorClassWrapper): \Generator
 {
+    if (!class_exists($behaviorClassWrapper)) {
+        throw new InvalidBehaviorException(
+            sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
+        );
+    }
+    if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
+        throw new InvalidBehaviorException(
+            sprintf('Class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
+        );
+    }

     foreach ($this->tariffTypeDefinitions as $tariffTypeDefinition) {
         ...

160-160: Include the requested type in the exception message.

Explicitly mentioning which type was attempted helps users debug missing aggregates more efficiently.

-    throw new AggregateNotFoundException('Aggregate was not found');
+    throw new AggregateNotFoundException(
+        sprintf("Aggregate not found for type '%s'", $type->getName())
+    );
src/product/BillingRegistryInterface.php (2)

47-47: Consider adding explicit validation expectations for getBehaviors.

The docblock for getBehaviors() does not mention the possibility—or impossibility—of invalid behavior classes. Aligning it with getBehavior() by stating the expected behavior or adding an explicit mention that no exceptions are thrown could improve consistency.


49-49: Document the exception for getAggregate.

Unlike other methods, getAggregate() can throw AggregateNotFoundException but lacks a corresponding docblock annotation. Documenting this can help users anticipate error scenarios.

 /**
+ * @throws AggregateNotFoundException when the requested aggregate cannot be found
 */
 public function getAggregate(string $type): AggregateInterface;
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0d8bd65 and 9d367cd.

📒 Files selected for processing (2)
  • src/product/BillingRegistry.php (1 hunks)
  • src/product/BillingRegistryInterface.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
src/product/BillingRegistry.php (11)
src/product/behavior/InvalidBehaviorException.php (1)
  • InvalidBehaviorException (7-9)
src/product/Exception/AggregateNotFoundException.php (1)
  • AggregateNotFoundException (7-9)
src/product/invoice/InvalidRepresentationException.php (1)
  • InvalidRepresentationException (7-10)
src/product/price/PriceTypeDefinition.php (7)
  • PriceTypeDefinition (24-201)
  • type (138-141)
  • documentRepresentation (124-129)
  • createQuantityFormatter (101-110)
  • hasType (143-146)
  • getAggregate (187-194)
  • afterLock (196-200)
src/product/quantity/QuantityFormatterNotFoundException.php (1)
  • QuantityFormatterNotFoundException (7-9)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/behavior/BehaviorNotFoundException.php (1)
  • BehaviorNotFoundException (7-9)
src/product/price/PriceTypeDefinitionInterface.php (5)
  • type (33-33)
  • documentRepresentation (28-28)
  • createQuantityFormatter (24-24)
  • hasType (35-35)
  • getAggregate (48-48)
src/type/Type.php (2)
  • Type (18-103)
  • anyId (99-102)
src/product/trait/HasLock.php (3)
  • ensureNotLocked (19-24)
  • afterLock (31-34)
  • lockItems (40-45)
src/product/TariffTypeDefinitionInterface.php (1)
  • withPrices (27-27)
src/product/BillingRegistryInterface.php (5)
src/product/behavior/BehaviorNotFoundException.php (1)
  • BehaviorNotFoundException (7-9)
src/product/behavior/InvalidBehaviorException.php (1)
  • InvalidBehaviorException (7-9)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/BillingRegistry.php (8)
  • priceTypes (34-41)
  • addTariffType (27-32)
  • getRepresentationsByType (43-65)
  • createQuantityFormatter (67-80)
  • getBehavior (87-116)
  • getBehaviors (131-148)
  • getAggregate (150-161)
  • getTariffTypeDefinitions (163-168)
src/product/price/PriceTypeDefinitionInterface.php (3)
  • createQuantityFormatter (24-24)
  • type (33-33)
  • getAggregate (48-48)
🔇 Additional comments (1)
src/product/BillingRegistry.php (1)

67-80: Confirm handling of multiple matching PriceTypeDefinitions.

The current design returns the first match for the specified type. If multiple PriceTypeDefinitions share the same type, only the first is used. Ensure that this approach is correct for your billing logic, or consider refining it to handle all matches or raise an error if more than one match is found.

Copy link
Contributor

@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

♻️ Duplicate comments (5)
src/product/price/PriceTypeDefinition.php (4)

77-80: Missing null check in getDescription()
This method directly returns the $description property but does not guarantee it has been initialized, potentially causing an uninitialized typed property error.


115-119: Implement validation in end() method
As previously suggested, consider verifying that all critical fields (e.g., unit, description, quantityFormatterDefinition) are set before returning. Leaving them unvalidated could lead to inadvertent misconfiguration.


131-131: Undefined class reference in measuredWith()
The class \hiqdev\billing\registry\measure\RcpTrafCollector does not appear to exist in the codebase, which will cause runtime errors if invoked.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 131-131: UndefinedClass
src/product/price/PriceTypeDefinition.php:131:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)


148-151: Missing null check in getUnit()
This method returns $this->unit without ensuring it was initialized, possibly causing uninitialized property errors.

src/product/price/PriceTypeDefinitionInterface.php (1)

31-31: Fix undefined class reference in measuredWith()
The type \hiqdev\billing\registry\measure\RcpTrafCollector is not defined in the project. Either remove or replace it with a valid class/interface.

🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 31-31: UndefinedClass
src/product/price/PriceTypeDefinitionInterface.php:31:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

🧹 Nitpick comments (1)
src/product/invoice/RepresentationCollection.php (1)

12-12: Add missing template parameters to fix static analysis issues
The IteratorAggregate interface is generic, expecting two parameters (e.g., <int, RepresentationInterface>). Consider specifying them to address the “MissingTemplateParam” warning.

Example:

- class RepresentationCollection implements \IteratorAggregate, HasLockInterface
+ /**
+  * @implements \IteratorAggregate<int, RepresentationInterface>
+  */
+ class RepresentationCollection implements \IteratorAggregate, HasLockInterface
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 12-12: MissingTemplateParam
src/product/invoice/RepresentationCollection.php:12:43: MissingTemplateParam: hiqdev\php\billing\product\invoice\RepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 48e882f and dc5d281.

📒 Files selected for processing (3)
  • src/product/invoice/RepresentationCollection.php (1 hunks)
  • src/product/price/PriceTypeDefinition.php (1 hunks)
  • src/product/price/PriceTypeDefinitionInterface.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (3)
src/product/invoice/RepresentationCollection.php (4)
src/product/price/PriceTypeDefinition.php (4)
  • PriceTypeDefinition (24-201)
  • __construct (40-52)
  • type (138-141)
  • end (115-119)
src/product/trait/HasLock.php (1)
  • ensureNotLocked (19-24)
src/product/invoice/RepresentationInterface.php (1)
  • setType (13-13)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/price/PriceTypeDefinitionInterface.php (9)
src/product/invoice/RepresentationCollection.php (2)
  • RepresentationCollection (12-53)
  • end (44-47)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/price/PriceTypeDefinition.php (13)
  • type (138-141)
  • unit (59-66)
  • description (68-75)
  • getDescription (77-80)
  • quantityFormatter (88-99)
  • createQuantityFormatter (101-110)
  • end (115-119)
  • documentRepresentation (124-129)
  • measuredWith (131-136)
  • hasType (143-146)
  • getUnit (148-151)
  • aggregation (174-181)
  • getAggregate (187-194)
src/product/BillingRegistryInterface.php (2)
  • createQuantityFormatter (31-31)
  • getAggregate (49-49)
src/product/TariffTypeDefinitionInterface.php (1)
  • end (29-29)
src/product/behavior/BehaviorCollectionInterface.php (1)
  • end (21-21)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (1)
  • end (15-18)
src/product/price/PriceTypeDefinitionCollection.php (1)
  • end (53-56)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
src/product/price/PriceTypeDefinition.php (9)
src/product/Exception/AggregateNotDefinedException.php (1)
  • AggregateNotDefinedException (7-10)
src/product/behavior/BehaviorPriceTypeDefinitionCollection.php (3)
  • BehaviorPriceTypeDefinitionCollection (8-19)
  • __construct (10-13)
  • end (15-18)
src/product/quantity/InvalidQuantityFormatterException.php (1)
  • InvalidQuantityFormatterException (7-10)
src/product/quantity/QuantityFormatterDefinition.php (2)
  • QuantityFormatterDefinition (7-29)
  • formatterClass (20-23)
src/product/quantity/QuantityFormatterFactory.php (1)
  • QuantityFormatterFactory (7-18)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)
src/product/price/PriceTypeDefinitionFactory.php (1)
  • create (10-16)
src/product/price/PriceTypeDefinitionCollectionInterface.php (1)
  • end (18-18)
src/product/trait/HasLockInterface.php (1)
  • lock (7-7)
🪛 GitHub Check: PHP 8.3
src/product/invoice/RepresentationCollection.php

[failure] 12-12: MissingTemplateParam
src/product/invoice/RepresentationCollection.php:12:43: MissingTemplateParam: hiqdev\php\billing\product\invoice\RepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

src/product/price/PriceTypeDefinitionInterface.php

[failure] 31-31: UndefinedClass
src/product/price/PriceTypeDefinitionInterface.php:31:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

src/product/price/PriceTypeDefinition.php

[failure] 131-131: UndefinedClass
src/product/price/PriceTypeDefinition.php:131:34: UndefinedClass: Class, interface or enum named hiqdev\billing\registry\measure\RcpTrafCollector does not exist (see https://psalm.dev/019)

Copy link
Contributor

@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 (5)
src/product/invoice/DuplicateRepresentationException.php (1)

5-10: Consider using a domain-specific LogicException and adding a docblock.
Right now, this file references the built-in LogicException. If you intend to use your custom domain logic exception (e.g., hiqdev\php\billing\Exception\LogicException), consider switching the use statement accordingly. Additionally, adding a brief docblock describing the purpose of this exception would improve clarity and maintainability.

src/product/invoice/RepresentationUniquenessGuard.php (2)

9-18: Ensure thorough testing of uniqueness checks.
The logic for detecting and throwing DuplicateRepresentationException is straightforward and correct. However, ensure that you have robust test coverage to confirm that duplicates are correctly detected under various scenarios (e.g., same classes, different classes, etc.).

Would you like help creating tests that verify multiple representation objects under different conditions?


20-24: Potential collision in generateKey if classes share the same short name.
Relying on ReflectionClass($representation)->getShortName() could lead to collisions if two classes from different namespaces share identical short names. If uniqueness across namespaces is required, consider incorporating the full namespace or additional properties in the key.

src/product/invoice/RepresentationCollection.php (2)

12-12: Provide the missing template parameters for IteratorAggregate.
Static analysis indicates that you need to specify both template parameters (key type and value type) when implementing \IteratorAggregate. For example:

 /**
  * @template T of PriceTypeDefinition
+ * @template-implements \IteratorAggregate<int, RepresentationInterface>
  */
 class RepresentationCollection implements \IteratorAggregate, HasLockInterface
 {
🧰 Tools
🪛 GitHub Check: PHP 8.3

[failure] 12-12: MissingTemplateParam
src/product/invoice/RepresentationCollection.php:12:43: MissingTemplateParam: hiqdev\php\billing\product\invoice\RepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)


54-58: Filtering by type is correct but consider type-specific docblocks.
filterByType effectively filters array items using instanceof. If you rely on external tooling for static analysis, consider documenting the expected return type (e.g., @return array<YourClass>), especially when $className is known.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dc5d281 and 42e41c7.

📒 Files selected for processing (3)
  • src/product/invoice/DuplicateRepresentationException.php (1 hunks)
  • src/product/invoice/RepresentationCollection.php (1 hunks)
  • src/product/invoice/RepresentationUniquenessGuard.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (3)
src/product/invoice/DuplicateRepresentationException.php (1)
src/Exception/LogicException.php (1)
  • LogicException (5-8)
src/product/invoice/RepresentationCollection.php (5)
src/product/price/PriceTypeDefinition.php (4)
  • PriceTypeDefinition (24-201)
  • __construct (40-52)
  • type (138-141)
  • end (115-119)
src/product/invoice/RepresentationUniquenessGuard.php (2)
  • RepresentationUniquenessGuard (5-25)
  • ensureUnique (9-18)
src/product/trait/HasLock.php (1)
  • ensureNotLocked (19-24)
src/product/invoice/RepresentationInterface.php (1)
  • setType (13-13)
src/product/price/PriceTypeDefinitionInterface.php (2)
  • type (33-33)
  • end (26-26)
src/product/invoice/RepresentationUniquenessGuard.php (1)
src/product/invoice/DuplicateRepresentationException.php (1)
  • DuplicateRepresentationException (7-10)
🪛 GitHub Check: PHP 8.3
src/product/invoice/RepresentationCollection.php

[failure] 12-12: MissingTemplateParam
src/product/invoice/RepresentationCollection.php:12:43: MissingTemplateParam: hiqdev\php\billing\product\invoice\RepresentationCollection has missing template params when extending IteratorAggregate, expecting 2 (see https://psalm.dev/182)

🔇 Additional comments (1)
src/product/invoice/RepresentationCollection.php (1)

33-44: Attachment logic is properly guarded and typed.
The attach flow checks for locks, sets the type, and ensures uniqueness with RepresentationUniquenessGuard. The approach looks clean and maintainable. No concerns here.

Copy link
Contributor

@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: 1

🧹 Nitpick comments (2)
src/product/quantity/QuantityFormatterFactory.php (2)

16-20: Missing PHPDoc for the create method

The method lacks documentation about its purpose, parameters, return value, and potential exceptions.

Add a PHPDoc comment to improve code readability and IDE support:

/**
+ * Creates a quantity formatter based on the provided unit, definition, and data.
+ *
+ * @param UnitInterface $unit The unit to be used by the formatter
+ * @param QuantityFormatterDefinition $definition The definition containing formatter class and fraction unit
+ * @param FractionQuantityData $data The data to be formatted
+ * @return QuantityFormatterInterface The created formatter instance
+ * @throws \InvalidArgumentException If the formatter class does not exist or does not implement QuantityFormatterInterface
+ */
public static function create(
    UnitInterface $unit,
    QuantityFormatterDefinition $definition,
    FractionQuantityData $data,
): QuantityFormatterInterface {

19-19: Assumptions about formatter constructor signature

The code assumes that all formatter classes have a specific constructor signature with three parameters. This creates tight coupling between the factory and concrete formatter implementations.

Consider making the factory more flexible by implementing an interface-based approach where formatters can be created with different parameter sets, or by using a builder pattern:

/**
 * Alternative implementation with interface-based approach
 */
public static function create(
    UnitInterface $unit,
    QuantityFormatterDefinition $definition,
    FractionQuantityData $data,
): QuantityFormatterInterface {
    $formatterClass = $definition->formatterClass();
    
    // Validate class exists and implements interface
    
    if (method_exists($formatterClass, 'createFromComponents')) {
        // Use a static factory method if available
        return $formatterClass::createFromComponents($unit, $definition->getFractionUnit(), $data);
    }
    
    // Fall back to direct instantiation
    return new $formatterClass($unit, $definition->getFractionUnit(), $data);
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 29218ec and d1b60ac.

📒 Files selected for processing (1)
  • src/product/quantity/QuantityFormatterFactory.php (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/product/quantity/QuantityFormatterFactory.php (2)
src/product/quantity/QuantityFormatterDefinition.php (3)
  • QuantityFormatterDefinition (7-29)
  • formatterClass (20-23)
  • getFractionUnit (25-28)
src/product/quantity/FractionQuantityData.php (1)
  • FractionQuantityData (7-14)

Comment on lines +1 to +21
<?php declare(strict_types=1);

namespace hiqdev\php\billing\product\quantity;

use hiqdev\php\units\UnitInterface;

/**
* Was created to replace {@see \hipanel\modules\finance\logic\bill\QuantityFormatterFactory}
*/
class QuantityFormatterFactory
{
public static function create(
UnitInterface $unit,
QuantityFormatterDefinition $definition,
FractionQuantityData $data,
): QuantityFormatterInterface {
$formatterClass = $definition->formatterClass();

return new $formatterClass($unit, $definition->getFractionUnit(), $data);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Good implementation, but needs error handling and type validation

The factory implementation is clean and follows good design principles with a static creation method. However, it's dynamically instantiating classes without any validation or error handling.

Consider adding validation to ensure the formatter class exists and implements the expected interface:

public static function create(
    UnitInterface $unit,
    QuantityFormatterDefinition $definition,
    FractionQuantityData $data,
): QuantityFormatterInterface {
    $formatterClass = $definition->formatterClass();
    
+   if (!class_exists($formatterClass)) {
+       throw new \InvalidArgumentException("Formatter class '{$formatterClass}' does not exist");
+   }
+   
+   if (!is_a($formatterClass, QuantityFormatterInterface::class, true)) {
+       throw new \InvalidArgumentException("Formatter class '{$formatterClass}' must implement " . QuantityFormatterInterface::class);
+   }

    return new $formatterClass($unit, $definition->getFractionUnit(), $data);
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<?php declare(strict_types=1);
namespace hiqdev\php\billing\product\quantity;
use hiqdev\php\units\UnitInterface;
/**
* Was created to replace {@see \hipanel\modules\finance\logic\bill\QuantityFormatterFactory}
*/
class QuantityFormatterFactory
{
public static function create(
UnitInterface $unit,
QuantityFormatterDefinition $definition,
FractionQuantityData $data,
): QuantityFormatterInterface {
$formatterClass = $definition->formatterClass();
return new $formatterClass($unit, $definition->getFractionUnit(), $data);
}
}
<?php declare(strict_types=1);
namespace hiqdev\php\billing\product\quantity;
use hiqdev\php\units\UnitInterface;
/**
* Was created to replace {@see \hipanel\modules\finance\logic\bill\QuantityFormatterFactory}
*/
class QuantityFormatterFactory
{
public static function create(
UnitInterface $unit,
QuantityFormatterDefinition $definition,
FractionQuantityData $data,
): QuantityFormatterInterface {
$formatterClass = $definition->formatterClass();
if (!class_exists($formatterClass)) {
throw new \InvalidArgumentException("Formatter class '{$formatterClass}' does not exist");
}
if (!is_a($formatterClass, QuantityFormatterInterface::class, true)) {
throw new \InvalidArgumentException("Formatter class '{$formatterClass}' must implement " . QuantityFormatterInterface::class);
}
return new $formatterClass($unit, $definition->getFractionUnit(), $data);
}
}

@SilverFire SilverFire merged commit 6ae9d3c into hiqdev:master Apr 22, 2025
3 of 5 checks passed
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.

2 participants