Skip to content

HP-2496/Added methods findBehaviorByClass, findPricesByTypeName and u…#103

Merged
SilverFire merged 3 commits into
hiqdev:masterfrom
ValeriyShnurovoy:HP-2496/Extend_TariffTypeDefinition_in_BillingRegistry_with_hasBehavior_and_getBehavior_methods
Aug 8, 2025
Merged

HP-2496/Added methods findBehaviorByClass, findPricesByTypeName and u…#103
SilverFire merged 3 commits into
hiqdev:masterfrom
ValeriyShnurovoy:HP-2496/Extend_TariffTypeDefinition_in_BillingRegistry_with_hasBehavior_and_getBehavior_methods

Conversation

@ValeriyShnurovoy
Copy link
Copy Markdown
Contributor

@ValeriyShnurovoy ValeriyShnurovoy commented Aug 6, 2025

…nit tests for them

Summary by CodeRabbit

  • New Features
    • Added the ability to retrieve prices filtered by type name.
    • Introduced functionality to find and access specific behaviors by class name.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 6, 2025

Walkthrough

New lookup methods were introduced to the TariffTypeDefinition and PriceTypeDefinition classes, as well as to related interfaces. These allow querying for prices by type name and behaviors by class. The interfaces were updated to declare these new methods, ensuring consistency across implementations.

Changes

Cohort / File(s) Change Summary
TariffTypeDefinition Enhancements
src/product/TariffTypeDefinition.php
Added findPricesByTypeName(string $typeName): ?array and findBehaviorByClass(string $class): ?BehaviorInterface methods, plus a private helper for type matching.
TariffTypeDefinition Interface Update
src/product/TariffTypeDefinitionInterface.php
Declared findPricesByTypeName(string $typeName): ?array in the interface.
HasBehaviors Interface Update
src/product/behavior/HasBehaviorsInterface.php
Added findBehaviorByClass(string $class): ?BehaviorInterface method declaration to the interface.
PriceTypeDefinition Enhancement
src/product/price/PriceTypeDefinition.php
Added findBehaviorByClass(string $class): ?BehaviorInterface method to the class.
TariffTypeBehaviorRegistry Enhancement
src/product/behavior/TariffTypeBehaviorRegistry.php
Added findBehaviorByClass(string $class): ?BehaviorInterface method to the class.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant TariffTypeDefinition
    participant PriceTypeDefinition
    participant TariffTypeBehaviorRegistry
    participant BehaviorInterface

    Client->>TariffTypeDefinition: findPricesByTypeName(typeName)
    TariffTypeDefinition->>TariffTypeDefinition: ensureNotLocked()
    TariffTypeDefinition->>TariffTypeDefinition: matchesPriceType(price, typeName)
    TariffTypeDefinition-->>Client: array|null

    Client->>TariffTypeDefinition: findBehaviorByClass(class)
    TariffTypeDefinition->>TariffTypeDefinition: ensureNotLocked()
    TariffTypeDefinition->>BehaviorInterface: check class
    TariffTypeDefinition-->>Client: BehaviorInterface|null

    Client->>PriceTypeDefinition: findBehaviorByClass(class)
    PriceTypeDefinition->>BehaviorInterface: check class
    PriceTypeDefinition-->>Client: BehaviorInterface|null

    Client->>TariffTypeBehaviorRegistry: findBehaviorByClass(class)
    TariffTypeBehaviorRegistry->>BehaviorInterface: check instanceof class
    TariffTypeBehaviorRegistry-->>Client: BehaviorInterface|null
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • SilverFire

Poem

A hop and a skip through the code I go,
New methods for searching, now quick as a doe!
Behaviors and prices, found by their name,
Interfaces updated to play the same game.
With each little patch, the product grows bright—
A rabbit’s delight in the soft morning light! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

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

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

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

Other keywords and placeholders

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

CodeRabbit Configuration File (.coderabbit.yaml)

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

Documentation and Community

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

Copy link
Copy Markdown
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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 354dcdc and 3c7c5ca.

📒 Files selected for processing (4)
  • src/product/TariffTypeDefinition.php (4 hunks)
  • src/product/TariffTypeDefinitionInterface.php (2 hunks)
  • src/product/behavior/HasBehaviorsInterface.php (1 hunks)
  • src/product/price/PriceTypeDefinition.php (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: the `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. use int...
Learnt from: SilverFire
PR: hiqdev/php-billing#93
File: src/product/BillingRegistry.php:5-5
Timestamp: 2025-01-13T12:20:09.570Z
Learning: The `hiqdev/php-billing` repository should not depend on `advancedhosters/billing-registry`. Use internal interfaces and implementations instead.

Applied to files:

  • src/product/price/PriceTypeDefinition.php
  • src/product/TariffTypeDefinitionInterface.php
  • src/product/TariffTypeDefinition.php
🔇 Additional comments (7)
src/product/behavior/HasBehaviorsInterface.php (1)

22-22: LGTM! Clean interface extension.

The new method declaration follows consistent patterns and provides a clear contract for behavior lookup by class name.

src/product/TariffTypeDefinitionInterface.php (2)

8-8: LGTM! Import added for method signature.

The import is necessary to support the method signature in the interface.


42-42: LGTM! Interface method declaration is well-defined.

The method signature is clear and follows consistent patterns with appropriate nullable return type.

src/product/price/PriceTypeDefinition.php (2)

8-8: LGTM! Import added for return type.

The import is necessary to support the BehaviorInterface return type in the new method.


198-207: LGTM! Clean implementation of behavior lookup.

The implementation correctly iterates through the behavior collection and uses instanceof to find matching behaviors. The logic is consistent with the existing hasBehavior method pattern.

src/product/TariffTypeDefinition.php (2)

130-141: LGTM! Clean behavior lookup implementation.

The implementation correctly iterates through behaviors and uses instanceof for type checking, consistent with similar methods in the codebase.


107-110: Confirm comma-delimited type names for price matching

The matchesPriceType() method in src/product/TariffTypeDefinition.php (lines 107–110) does:

return str_ends_with($price->type()->getName(), ",$typeName");

This assumes that
$price->type()->getName() returns a comma-separated hierarchy (e.g. "tariffType,priceType") so that checking for ",$typeName" correctly identifies the intended leaf type.

• Please verify that your TypeDefinitionInterface::getName() implementation indeed concatenates parent and child names with commas.
• If it only returns the bare price-type name (or uses a different delimiter), update the matching logic (for example, compare equality or split on comma) to avoid false positives/negatives.

Comment thread src/product/TariffTypeDefinition.php Outdated
Comment on lines +94 to +105
public function findPricesByTypeName(string $typeName): ?array
{
$prices = null;
$this->ensureNotLocked();

foreach ($this->prices as $price) {
if ($this->matchesPriceType($price, $typeName)) {
$prices[] = $price;
}
}
return $prices;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix potential null array append issue.

The variable $prices is initialized as null but then array elements are appended to it on line 101, which will cause a PHP error.

 public function findPricesByTypeName(string $typeName): ?array
 {
-    $prices = null;
+    $prices = [];
     $this->ensureNotLocked();

     foreach ($this->prices as $price) {
         if ($this->matchesPriceType($price, $typeName)) {
             $prices[] = $price;
         }
     }
-    return $prices;
+    return empty($prices) ? null : $prices;
 }
📝 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 findPricesByTypeName(string $typeName): ?array
{
$prices = null;
$this->ensureNotLocked();
foreach ($this->prices as $price) {
if ($this->matchesPriceType($price, $typeName)) {
$prices[] = $price;
}
}
return $prices;
}
public function findPricesByTypeName(string $typeName): ?array
{
$prices = [];
$this->ensureNotLocked();
foreach ($this->prices as $price) {
if ($this->matchesPriceType($price, $typeName)) {
$prices[] = $price;
}
}
return empty($prices) ? null : $prices;
}
🤖 Prompt for AI Agents
In src/product/TariffTypeDefinition.php around lines 94 to 105, the variable
$prices is initialized as null but then used as an array when appending
elements, causing a PHP error. Initialize $prices as an empty array instead of
null before the loop to fix this issue.

Copy link
Copy Markdown
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/behavior/TariffTypeBehaviorRegistry.php (1)

63-72: LGTM! Consider refactoring to reduce code duplication.

The implementation correctly implements the HasBehaviorsInterface contract and follows the expected pattern. However, there's significant code duplication with the existing hasBehavior method.

Consider refactoring to eliminate duplication:

 public function hasBehavior(string $behaviorClassName): bool
 {
-    foreach ($this->behaviorCollection as $behavior) {
-        if ($behavior instanceof $behaviorClassName) {
-            return true;
-        }
-    }
-
-    return false;
+    return $this->findBehaviorByClass($behaviorClassName) !== null;
 }

This approach leverages the new method while maintaining the same functionality and reducing code duplication.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5758fea and b5693d1.

📒 Files selected for processing (2)
  • src/product/TariffTypeDefinition.php (4 hunks)
  • src/product/behavior/TariffTypeBehaviorRegistry.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/product/TariffTypeDefinition.php
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/product/behavior/TariffTypeBehaviorRegistry.php (3)
src/product/behavior/HasBehaviorsInterface.php (1)
  • findBehaviorByClass (22-22)
src/product/TariffTypeDefinition.php (1)
  • findBehaviorByClass (128-131)
src/product/price/PriceTypeDefinition.php (1)
  • findBehaviorByClass (198-207)

@SilverFire SilverFire merged commit c650bfe into hiqdev:master Aug 8, 2025
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.

3 participants