-
Notifications
You must be signed in to change notification settings - Fork 12
HP-2496: Extend TariffTypeDefinition with findBehaviorByClass method #104
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
Changes from all commits
0aff5ab
ca6bc90
2f7d605
f253628
8064c4a
1a99cc4
5b5cf26
da60a93
e6fe816
c4cb9df
9f10707
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace hiqdev\php\billing\product\behavior; | ||
|
|
||
| use hiqdev\php\billing\product\trait\HasLockInterface; | ||
|
|
||
| /** | ||
| * @psalm-suppress MissingTemplateParam | ||
| * @psalm-suppress InvalidTemplateParam | ||
| */ | ||
| abstract class BehaviorRegistry implements HasLockInterface, HasBehaviorsInterface | ||
| { | ||
| public function hasBehavior(string $behaviorClassName): bool | ||
| { | ||
| foreach ($this->withBehaviors() as $behavior) { | ||
| if ($behavior instanceof $behaviorClassName) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public function findBehaviorByClass(string $class) | ||
| { | ||
| foreach ($this->withBehaviors() as $behavior) { | ||
| if ($behavior instanceof $class) { | ||
| return $behavior; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public function lock(): void | ||
| { | ||
| $this->withBehaviors()->lock(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace hiqdev\php\billing\product\behavior; | ||
|
|
||
| use hiqdev\php\billing\product\Domain\Model\TariffTypeInterface; | ||
| use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface; | ||
|
|
||
| /** | ||
| * @template-covariant T of PriceTypeDefinitionInterface | ||
| */ | ||
| final class PriceTypeBehaviorRegistry extends BehaviorRegistry | ||
| { | ||
| /** | ||
| * @var BehaviorPriceTypeDefinitionCollection<T> | ||
| */ | ||
| private BehaviorPriceTypeDefinitionCollection $behaviorCollection; | ||
|
|
||
| /** | ||
| * @psalm-param T $priceTypeDefinition | ||
| */ | ||
| public function __construct(PriceTypeDefinitionInterface $priceTypeDefinition, TariffTypeInterface $tariffType) | ||
| { | ||
| $this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($priceTypeDefinition, $tariffType); | ||
| } | ||
|
|
||
| /** | ||
| * @return BehaviorPriceTypeDefinitionCollection<T> | ||
| */ | ||
| public function withBehaviors(): BehaviorPriceTypeDefinitionCollection | ||
| { | ||
| return $this->behaviorCollection; | ||
| } | ||
|
|
||
| protected function getBehaviorCollection(): BehaviorCollectionInterface | ||
| { | ||
| return $this->behaviorCollection; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| * | ||
| * @template-covariant T of TariffTypeDefinitionInterface | ||
| */ | ||
| final class TariffTypeBehaviorRegistry implements HasLockInterface | ||
| final class TariffTypeBehaviorRegistry extends BehaviorRegistry | ||
| { | ||
| /** | ||
| * @var BehaviorTariffTypeCollection<T> | ||
|
|
@@ -44,35 +44,13 @@ public function __construct(TariffTypeDefinitionInterface $tariffTypeDefinition, | |
| /** | ||
| * @return BehaviorTariffTypeCollection<T> | ||
| */ | ||
| public function getBehaviors(): BehaviorTariffTypeCollection | ||
| public function withBehaviors(): BehaviorTariffTypeCollection | ||
| { | ||
| return $this->behaviorCollection; | ||
| } | ||
|
Comment on lines
+47
to
50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainwithBehaviors() rename: verify all call sites migrated from getBehaviors() Rename looks good. Please ensure no usages of getBehaviors() remain. Run this script to find leftovers and confirm new usages: 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "Searching for old API getBehaviors("
rg -n --hidden --no-ignore -S '\bgetBehaviors\(' || true
echo
echo "Searching for new API withBehaviors("
rg -n --hidden --no-ignore -S '\bwithBehaviors\(' || trueLength of output: 2746 withBehaviors() rename verification All call-sites of the old getBehaviors() API have been updated to withBehaviors(). The only remaining reference is in the docblock of the registry class: • src/product/behavior/TariffTypeBehaviorRegistry.php:19 🤖 Prompt for AI Agents |
||
|
|
||
| public function hasBehavior(string $behaviorClassName): bool | ||
| protected function getBehaviorCollection(): BehaviorCollectionInterface | ||
| { | ||
| foreach ($this->behaviorCollection as $behavior) { | ||
| if ($behavior instanceof $behaviorClassName) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public function findBehaviorByClass(string $class): ?BehaviorInterface | ||
| { | ||
| foreach ($this->getBehaviors() as $behavior) { | ||
| if ($behavior instanceof $class) { | ||
| return $behavior; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public function lock(): void | ||
| { | ||
| $this->behaviorCollection->lock(); | ||
| return $this->behaviorCollection; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,8 @@ | |
| namespace hiqdev\php\billing\product\price; | ||
|
|
||
| use hiqdev\php\billing\product\AggregateInterface; | ||
| use hiqdev\php\billing\product\behavior\BehaviorCollectionInterface; | ||
| use hiqdev\php\billing\product\behavior\BehaviorInterface; | ||
| use hiqdev\php\billing\product\behavior\HasBehaviorsInterface; | ||
| use hiqdev\php\billing\product\behavior\PriceTypeBehaviorRegistry; | ||
| use hiqdev\php\billing\product\Exception\AggregateNotDefinedException; | ||
| use hiqdev\php\billing\product\behavior\BehaviorPriceTypeDefinitionCollection; | ||
| use hiqdev\php\billing\product\invoice\RepresentationCollection; | ||
|
|
@@ -22,6 +21,7 @@ | |
| use hiqdev\php\billing\product\TariffTypeDefinitionInterface; | ||
| use hiqdev\php\billing\product\trait\HasLock; | ||
| use hiqdev\php\billing\type\TypeInterface; | ||
| use function class_exists; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate formatter contract, not just existence class_exists prevents fatals, but validating the class implements QuantityFormatterInterface will fail earlier and with a clearer message. - if (!class_exists($formatterClass)) {
+ if (!class_exists($formatterClass)) {
throw new InvalidQuantityFormatterException("Formatter class $formatterClass does not exist");
}
+ if (!is_subclass_of($formatterClass, QuantityFormatterInterface::class)) {
+ throw new InvalidQuantityFormatterException(sprintf(
+ 'Formatter class %s must implement %s',
+ $formatterClass,
+ QuantityFormatterInterface::class
+ ));
+ }Also applies to: 107-112 🤖 Prompt for AI Agents |
||
|
|
||
| /** | ||
| * @template TParentCollection | ||
|
|
@@ -46,16 +46,13 @@ class PriceTypeDefinition implements PriceTypeDefinitionInterface | |
| */ | ||
| private RepresentationCollection $representationCollection; | ||
|
|
||
| /** | ||
| * @var BehaviorPriceTypeDefinitionCollection<PriceTypeDefinition> | ||
| */ | ||
| private BehaviorPriceTypeDefinitionCollection $behaviorCollection; | ||
|
|
||
| private ?AggregateInterface $aggregate = null; | ||
|
|
||
| /** @psalm-var TParentCollection */ | ||
| private readonly PriceTypeDefinitionCollectionInterface $parent; | ||
|
|
||
| private readonly PriceTypeBehaviorRegistry $behaviorRegistry; | ||
|
|
||
| /** | ||
| * @param TParentCollection $parent | ||
| */ | ||
|
|
@@ -66,7 +63,7 @@ public function __construct( | |
| ) { | ||
| $this->parent = $parent; | ||
| $this->representationCollection = new RepresentationCollection($this); | ||
| $this->behaviorCollection = new BehaviorPriceTypeDefinitionCollection($this, $tariffType); | ||
| $this->behaviorRegistry = new PriceTypeBehaviorRegistry($this, $tariffType); | ||
|
|
||
| $this->init(); | ||
| } | ||
|
|
@@ -107,7 +104,7 @@ public function quantityFormatter(string $formatterClass, $fractionUnit = null): | |
| { | ||
| $this->ensureNotLocked(); | ||
|
|
||
| if (!\class_exists($formatterClass)) { | ||
| if (!class_exists($formatterClass)) { | ||
| throw new InvalidQuantityFormatterException("Formatter class $formatterClass does not exist"); | ||
| } | ||
|
|
||
|
|
@@ -181,29 +178,17 @@ public function withBehaviors() | |
| { | ||
| $this->ensureNotLocked(); | ||
|
|
||
| return $this->behaviorCollection; | ||
| return $this->behaviorRegistry->withBehaviors(); | ||
| } | ||
|
|
||
| public function hasBehavior(string $behaviorClassName): bool | ||
| { | ||
| foreach ($this->behaviorCollection as $behavior) { | ||
| if ($behavior instanceof $behaviorClassName) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| return $this->behaviorRegistry->hasBehavior($behaviorClassName); | ||
| } | ||
|
|
||
| public function findBehaviorByClass(string $class): ?BehaviorInterface | ||
| public function findBehaviorByClass(string $class) | ||
| { | ||
| foreach ($this->behaviorCollection as $behavior) { | ||
| if ($behavior instanceof $class) { | ||
| return $behavior; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| return $this->behaviorRegistry->findBehaviorByClass($class); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -242,7 +227,7 @@ public function getQuantityFormatterDefinition(): ?QuantityFormatterDefinition | |
| protected function afterLock(): void | ||
| { | ||
| $this->representationCollection->lock(); | ||
| $this->behaviorCollection->lock(); | ||
| $this->behaviorRegistry->lock(); | ||
| } | ||
|
|
||
| public function getTariffTypeDefinition(): TariffTypeDefinitionInterface | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.