Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/action/UsageInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,18 @@ public function ratioOfMonth(): float

return $usageSeconds / $secondsInCurrentMonth;
}

/**
* Extends the usage interval to include both current and other intervals.
*
* @param UsageInterval $other
* @return self
*/
public function extend(self $other): self
{
return new self(
min($this->start, $other->start),
max($this->end, $other->end),
);
}
Comment on lines +132 to +145
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.

🛠️ Refactor suggestion

Add validation to prevent invalid interval combinations.

The extend() method implementation looks correct for combining intervals, but could benefit from additional validation to prevent edge cases.

Consider adding validation checks:

 public function extend(self $other): self
 {
+    $newStart = min($this->start, $other->start);
+    $newEnd = max($this->end, $other->end);
+    
+    if ($newStart > $newEnd) {
+        throw new InvalidArgumentException('Cannot extend intervals: resulting interval would be invalid');
+    }
+
     return new self(
-        min($this->start, $other->start),
-        max($this->end, $other->end),
+        $newStart,
+        $newEnd,
     );
 }
📝 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
/**
* Extends the usage interval to include both current and other intervals.
*
* @param UsageInterval $other
* @return self
*/
public function extend(self $other): self
{
return new self(
min($this->start, $other->start),
max($this->end, $other->end),
);
}
/**
* Extends the usage interval to include both current and other intervals.
*
* @param UsageInterval $other
* @return self
*/
public function extend(self $other): self
{
$newStart = min($this->start, $other->start);
$newEnd = max($this->end, $other->end);
if ($newStart > $newEnd) {
throw new InvalidArgumentException('Cannot extend intervals: resulting interval would be invalid');
}
return new self(
$newStart,
$newEnd,
);
}

}
14 changes: 14 additions & 0 deletions src/bill/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace hiqdev\php\billing\bill;

use DateTimeImmutable;
use hiqdev\php\billing\action\UsageInterval;
use hiqdev\php\billing\charge\ChargeInterface;
use hiqdev\php\billing\customer\CustomerInterface;
use hiqdev\php\billing\Exception\CannotReassignException;
Expand Down Expand Up @@ -63,6 +64,9 @@ class Bill implements BillInterface
/** @var string */
protected $comment;

/** @var UsageInterval */
protected $usageInterval;

public function __construct(
$id,
TypeInterface $type,
Expand All @@ -85,6 +89,7 @@ public function __construct(
$this->plan = $plan;
$this->charges = $charges;
$this->state = $state;
$this->setUsageInterval(UsageInterval::wholeMonth($time));
}

/**
Expand All @@ -104,6 +109,10 @@ public function getUniqueString(): string
return implode('-', $parts);
}

public function getUsageInterval(): UsageInterval
{
}
Comment on lines +112 to +114
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

Critical: Implement getUsageInterval() method.

The method is empty and needs to be implemented. Based on the standard getter pattern in this class, it should return the protected $usageInterval property.

 public function getUsageInterval(): UsageInterval
 {
+    return $this->usageInterval;
 }
📝 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 getUsageInterval(): UsageInterval
{
}
public function getUsageInterval(): UsageInterval
{
return $this->usageInterval;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Missing implementation


public function calculatePrice()
{
$quantity = $this->quantity->getQuantity();
Expand Down Expand Up @@ -233,4 +242,9 @@ public function jsonSerialize(): array
{
return array_filter(get_object_vars($this));
}

public function setUsageInterval(UsageInterval $usageInterval): void
{
$this->usageInterval = $usageInterval;
}
}
5 changes: 5 additions & 0 deletions src/bill/BillInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace hiqdev\php\billing\bill;

use DateTimeImmutable;
use hiqdev\php\billing\action\UsageInterval;
use hiqdev\php\billing\charge\ChargeInterface;
use hiqdev\php\billing\customer\CustomerInterface;
use hiqdev\php\billing\EntityInterface;
Expand Down Expand Up @@ -50,4 +51,8 @@ public function getPlan(): ?PlanInterface;
* @return ChargeInterface[]
*/
public function getCharges();

public function getUsageInterval(): UsageInterval;

public function setUsageInterval(UsageInterval $usageInterval): void;
Comment on lines +55 to +57
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.

💡 Codebase verification

Implementation of getUsageInterval is incomplete in Bill class

The verification revealed:

  • Both Bill and StatementBill implement BillInterface
  • Bill class has an empty implementation of getUsageInterval()
  • StatementBill inherits from Bill but doesn't override these methods
  • setUsageInterval() is properly implemented in Bill class and initializes $usageInterval in the constructor
🔗 Analysis chain

Verify interface implementation in all concrete classes.

The addition of new interface methods requires updates to all implementing classes.

Let's verify the implementation:


Let me gather more information about the implementation details in the identified files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find all PHP files that might implement BillInterface
echo "Searching for potential BillInterface implementations..."
rg -l "implements.*BillInterface" --type php

# Look for classes that have the new methods
echo -e "\nChecking for getUsageInterval implementation..."
ast-grep --pattern 'class $_ {
  $$$
  getUsageInterval(): UsageInterval {
    $$$
  }
  $$$
}'

echo -e "\nChecking for setUsageInterval implementation..."
ast-grep --pattern 'class $_ {
  $$$
  setUsageInterval($_): void {
    $$$
  }
  $$$
}'

Length of output: 630


Script:

#!/bin/bash
# Check the content of both implementing classes
echo "Content of src/bill/Bill.php:"
cat src/bill/Bill.php

echo -e "\nContent of src/statement/StatementBill.php:"
cat src/statement/StatementBill.php

Length of output: 8788

}
14 changes: 12 additions & 2 deletions src/charge/Generalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace hiqdev\php\billing\charge;

use hiqdev\php\billing\action\UsageInterval;
use hiqdev\php\billing\bill\Bill;
use hiqdev\php\billing\bill\BillInterface;
use hiqdev\php\billing\customer\CustomerInterface;
Expand All @@ -26,7 +27,7 @@ class Generalizer implements GeneralizerInterface
{
public function createBill(ChargeInterface $charge): BillInterface
{
return new Bill(
$bill = new Bill(
null,
$this->generalizeType($charge),
$this->generalizeTime($charge),
Expand All @@ -35,8 +36,12 @@ public function createBill(ChargeInterface $charge): BillInterface
$this->generalizeCustomer($charge),
$this->generalizeTarget($charge),
$this->generalizePlan($charge),
[$charge]
[$charge],
);

$bill->setUsageInterval($this->generalizeUsageInterval($charge));

return $bill;
}

public function generalizeType(ChargeInterface $charge): TypeInterface
Expand Down Expand Up @@ -83,4 +88,9 @@ public function specializeTarget(TargetInterface $first, TargetInterface $other)
{
return $first;
}

private function generalizeUsageInterval(ChargeInterface $charge): UsageInterval
{
return $charge->getAction()->getUsageInterval();
}
}
12 changes: 11 additions & 1 deletion src/tools/Aggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace hiqdev\php\billing\tools;

use hiqdev\php\billing\action\UsageInterval;
use hiqdev\php\billing\bill\Bill;
use hiqdev\php\billing\bill\BillInterface;
use hiqdev\php\billing\charge\ChargeInterface;
Expand Down Expand Up @@ -77,7 +78,7 @@ protected function aggregateBills(array $bills, array $others): array

protected function aggregateBill(BillInterface $first, BillInterface $other): BillInterface
{
return new Bill(
$bill = new Bill(
$this->aggregateId($first, $other),
$first->getType(),
$first->getTime(),
Expand All @@ -88,6 +89,15 @@ protected function aggregateBill(BillInterface $first, BillInterface $other): Bi
$first->getPlan(),
array_merge($first->getCharges(), $other->getCharges())
);

$bill->setUsageInterval($this->aggregateUsageInterval($first, $other));

return $bill;
}

protected function aggregateUsageInterval(BillInterface $first, BillInterface $other): UsageInterval
{
return $first->getUsageInterval()->extend($other->getUsageInterval());
}

/**
Expand Down