assoconnect/php-percent
Small PHP library to represent and work with percentages in a way that pairs well with MoneyPHP, based on Frederik Bosch’s proposal. Install via Composer and use the PHPDoc-documented API to apply percent calculations safely alongside Money values.
assoconnect/php-percent package (v1.1.5) remains ideal for financial applications in Laravel, particularly for percentage-based calculations tied to MoneyPHP. The new release introduces type safety improvements (PHPStan baseline for float return types), reinforcing its suitability for precise monetary operations like discounts, taxes, or dynamic pricing.laravel-money or moneyphp/money), reducing boilerplate while maintaining type safety.float for percentage values), mitigating potential type-related bugs in complex workflows.MoneyPHP v3.0+ required). The PHPStan baseline is an internal tooling improvement and does not affect runtime behavior.int vs. float percentages).apply(), getValue(), setValue()) remain stable.$percent = new Percent(10.5, Money::EUR(100)); // Explicit float support
$discount = $percent->apply(); // Returns Money object for €10.50
MoneyPHP's round() or Laravel’s number_format() cautiously.Percent::getValue() returns int (e.g., for whole-number percentages), explicit casting may be needed:
$rate = (int) $percent->getValue(); // Explicit cast for integer rates
Percent objects from repositories or Blade templates to prevent type mismatches.int percentages (e.g., for whole numbers) or embrace float for precision (e.g., 10.5%)? Document this in a coding guideline."10.5%") to float/int values?int percentages that may now trigger PHPStan warnings? Audit and refactor incrementally.Money facade or a custom Percentage trait (with explicit type hints) achieve similar results with less abstraction?laravel-money or moneyphp/money. No changes to integration patterns.Percent in Laravel services (e.g., DiscountService) rather than models or controllers.Money objects.includes:
- vendor/assoconnect/php-percent/phpstan.neon
Percent methods.int vs. float). Use PHPStan to identify potential issues:
vendor/bin/phpstan analyse --level 5
Percent, ensuring type consistency.assertIsFloat($percent->getValue())).class PercentAdapter {
public static function fromInt(int $rate, Money $amount): Percent {
return new Percent((float) $rate, $amount);
}
}
moneyphp/money:^3.0 to avoid deprecation issues.Percent to interfaces for testability, and explicitly type-hint methods to avoid ambiguity:
interface PercentageCalculator {
public function calculate(float $rate, Money $amount): Money;
}
Percent operations, including:
0.0, 100.0, negative values).public function testFloatPercentageReturnsMoney(): void {
$percent = new Percent(10.5, Money::EUR(100));
$result = $percent->apply();
$this->assertInstanceOf(Money::class, $result);
$this->assertEquals(10.5, $percent->getValue()); // Explicit float check
}
Percent (with float support) vs. raw MoneyPHP for critical paths.float for percentages) across the codebase.int percentages may require refactoring to accommodate floats.public function applyDiscount(float|int $rate, Money $amount): Money {
$percent = new Percent((float) $rate, $amount);
return $percent->apply();
}
int percentages can be resolved by casting to float").# .github/workflows/phpstan.yml
jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer require --dev phpstan/phpstan
- run: vendor/bin/phpstan analyse --level 5 --configuration=phpstan.neon
Percent results for idempotent operations (e.g., precomputed tax rates).| Failure Scenario | Impact | Mitigation | |-------------------------------------|
How can I help you explore Laravel packages today?