Modernize Bill with native PHP 8.3 type declarations#117
Modernize Bill with native PHP 8.3 type declarations#117kai3769 wants to merge 1 commit intohiqdev:masterfrom
Conversation
- Replace PHPDoc @var annotations with native property types - Add return types to all methods (getId, calculatePrice, getComment, setId, setComment) - Add union type int|string|null for $id property - Use nullable types where appropriate (?TargetInterface, ?PlanInterface, ?BillState) - Change self to static for fluent setters (setQuantity, setCharges) - Add array return type to BillInterface::getCharges() - Fix PHPDoc @param typo: $prices → $charges in setCharges - Fix @throws annotation: \Exception → CannotReassignException Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdded strict type declarations to the Bill class properties and method signatures, replacing PHPDoc-only annotations with typed properties and return types. Updated the BillInterface to explicitly declare the getCharges() return type as array. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/bill/Bill.php`:
- Around line 110-115: In calculatePrice() in Bill.php replace the
float-formatting usage when dividing money by quantity: instead of passing
sprintf('%.14F', $quantity) to $this->sum->divide(), cast the quantity to a
string (e.g. (string)$quantity) so Money::divide() receives the original numeric
string and avoids float precision artifacts; update the call in calculatePrice()
(and apply the same change to other price/charge calculation methods that use
sprintf('%.14F', $quantity) / getQuantity()).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0e04d2d0-079e-428e-91a8-a6721556b3dc
📒 Files selected for processing (2)
src/bill/Bill.phpsrc/bill/BillInterface.php
| public function calculatePrice(): Money | ||
| { | ||
| $quantity = $this->quantity->getQuantity(); | ||
|
|
||
| return $quantity ? $this->sum->divide(sprintf('%.14F', $quantity)) : $this->sum; | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify quantity scalar type and money-division call patterns.
fd -i 'QuantityInterface.php'
rg -n -C3 'interface QuantityInterface|function getQuantity\(' --type=php
rg -n -C3 'calculatePrice\(|->divide\(|sprintf\(\'%.14F\'' --type=phpRepository: hiqdev/php-billing
Length of output: 1929
🏁 Script executed:
fd -i 'quantityinterface.php' --type fRepository: hiqdev/php-billing
Length of output: 102
🏁 Script executed:
rg -n 'class QuantityInterface|interface QuantityInterface' --type=php -A 10Repository: hiqdev/php-billing
Length of output: 44
🏁 Script executed:
rg -n 'function getQuantity\(\)' --type=php -B 2 -A 3Repository: hiqdev/php-billing
Length of output: 1315
🏁 Script executed:
rg -n 'public function divide' --type=php -B 2 -A 5Repository: hiqdev/php-billing
Length of output: 44
🏁 Script executed:
fd -i 'quantity' --type fRepository: hiqdev/php-billing
Length of output: 448
🏁 Script executed:
fd -i 'money' --type f | head -20Repository: hiqdev/php-billing
Length of output: 193
🏁 Script executed:
rg -n 'class Money|interface QuantityInterface' --type=php -A 5Repository: hiqdev/php-billing
Length of output: 44
🏁 Script executed:
cat composer.json | grep -A 10 '"require"'Repository: hiqdev/php-billing
Length of output: 367
🏁 Script executed:
head -50 src/bill/Bill.phpRepository: hiqdev/php-billing
Length of output: 1248
🏁 Script executed:
find . -name "QuantityInterface.php" -o -name "*Quantity*.php" | xargs ls -laRepository: hiqdev/php-billing
Length of output: 1252
🏁 Script executed:
cat vendor/hiqdev/php-units/src/QuantityInterface.phpRepository: hiqdev/php-billing
Length of output: 3043
🏁 Script executed:
rg -n 'getQuantity' vendor/hiqdev/php-units/src/ --type=php -B 2 -A 2Repository: hiqdev/php-billing
Length of output: 2688
🏁 Script executed:
rg -n '->divide\(' src/ --type=php -B 2 -A 2Repository: hiqdev/php-billing
Length of output: 466
🏁 Script executed:
web_search
MoneyPHP Money::divide() method signature and type requirementsRepository: hiqdev/php-billing
Length of output: 261
🏁 Script executed:
rg -n 'divide\(' src/ --type=php -B 2 -A 2Repository: hiqdev/php-billing
Length of output: 1523
🌐 Web query:
MoneyPHP Money divide method signature PHP
💡 Result:
Web search failed: Server error: no LLM provider could handle the message
Use string casting instead of float formatting in monetary division.
The sprintf('%.14F', $quantity) pattern formats the quantity as a float string before passing it to Money::divide(). This float formatting stage can introduce precision artifacts. Since getQuantity() already returns int|float|string, a simple string cast is safer and more direct.
Proposed fix
public function calculatePrice(): Money
{
$quantity = $this->quantity->getQuantity();
- return $quantity ? $this->sum->divide(sprintf('%.14F', $quantity)) : $this->sum;
+ if ($quantity === 0 || $quantity === '0') {
+ return $this->sum;
+ }
+
+ return $this->sum->divide((string)$quantity);
}Note: This same pattern appears in multiple price and charge calculation classes—consider applying the same fix systematically.
📝 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.
| public function calculatePrice(): Money | |
| { | |
| $quantity = $this->quantity->getQuantity(); | |
| return $quantity ? $this->sum->divide(sprintf('%.14F', $quantity)) : $this->sum; | |
| } | |
| public function calculatePrice(): Money | |
| { | |
| $quantity = $this->quantity->getQuantity(); | |
| if ($quantity === 0 || $quantity === '0') { | |
| return $this->sum; | |
| } | |
| return $this->sum->divide((string)$quantity); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/bill/Bill.php` around lines 110 - 115, In calculatePrice() in Bill.php
replace the float-formatting usage when dividing money by quantity: instead of
passing sprintf('%.14F', $quantity) to $this->sum->divide(), cast the quantity
to a string (e.g. (string)$quantity) so Money::divide() receives the original
numeric string and avoids float precision artifacts; update the call in
calculatePrice() (and apply the same change to other price/charge calculation
methods that use sprintf('%.14F', $quantity) / getQuantity()).
Summary
@varannotations on Bill properties with native PHP typed propertiesgetId(),calculatePrice(),getComment(),setId(),setComment())arrayreturn type toBillInterface::getCharges()int|string|nullfor$idconstructor parameter@param $prices→@param $chargesinsetCharges()@throws \Exception→@throws CannotReassignException(actual exception thrown)staticinstead ofself/BillInterfacefor fluent setter return typesMotivation
The project requires PHP 8.3+ (
composer.json). Native type declarations improve:Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit