Skip to content

Commit

Permalink
Merge pull request #9 from Dropelikeit/change_return_types
Browse files Browse the repository at this point in the history
* fix return types from int to float
  • Loading branch information
Dropelikeit committed Jan 30, 2018
2 parents 557ebd9 + a603ff6 commit 1e00862
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
53 changes: 26 additions & 27 deletions src/PriceCalculator.php
Expand Up @@ -2,7 +2,6 @@

namespace MarcelStrahl\PriceCalculator;

use MarcelStrahl\PriceCalculator\Factory\ConverterFactoryInterface;
use MarcelStrahl\PriceCalculator\Helpers\Entity\VatInterface;

/**
Expand All @@ -28,69 +27,69 @@ public function __construct(VatInterface $vat)
/**
* Add an price to total
*
* @param int $total
* @param int $price
* @return int
* @param float $total
* @param float $price
* @return float
*/
public function addPrice(int $total, int $price): int
public function addPrice(float $total, float $price): float
{
return (int)bcadd($total, $price);
return (float)bcadd($total, $price);
}

/**
* Sub an price from total
*
* @param int $total
* @param int $price
* @return int
* @param float $total
* @param float $price
* @return float
*/
public function subPrice(int $total, int $price): int
public function subPrice(float $total, float $price): float
{
$total = (int)bcsub($total, $price);
$total = (float)bcsub($total, $price);
return ($total < 0) ? 0 : $total;
}

/**
* Mul an price with amount
*
* @param int $amount
* @param int $price
* @return int
* @param float $amount
* @param float $price
* @return float
*/
public function mulPrice(int $amount, int $price): int
public function mulPrice(float $amount, float $price): float
{
return bcmul($price, $amount);
return (float)bcmul($price, $amount);
}

/**
* Calculates the gross price
*
* @param int $netPrice
* @return int
* @param float $netPrice
* @return float
*/
public function calculatePriceWithSalesTax(int $netPrice): int
public function calculatePriceWithSalesTax(float $netPrice): float
{
return (int)round((float)bcmul($netPrice, $this->vat->getVatToCalculate(), 2));
return (float)round((float)bcmul($netPrice, $this->vat->getVatToCalculate(), 2));
}

/**
* Calculates the value added tax from the current total price
*
* @param int $total
* @return int
* @param float $total
* @return float
*/
public function calculateSalesTaxFromTotalPrice(int $total) : int
public function calculateSalesTaxFromTotalPrice(float $total) : float
{
return (int)bcsub($total, round((float)bcdiv($total, $this->vat->getVatToCalculate(), 2)));
return (float)bcsub($total, round((float)bcdiv($total, $this->vat->getVatToCalculate(), 2)));
}

/**
* Calculates the net price from the gross price
*
* @param int $total
* @return int
* @param float $total
* @return float
*/
public function calculateNetPriceFromGrossPrice(int $total) : int
public function calculateNetPriceFromGrossPrice(float $total) : float
{
$vatPrice = $this->calculateSalesTaxFromTotalPrice($total);
return $this->subPrice($total, $vatPrice);
Expand Down
42 changes: 22 additions & 20 deletions tests/PriceCalculatorTest.php
Expand Up @@ -36,12 +36,12 @@ public function testCanInitPriceCalculator(): void

/**
* @dataProvider dataProviderAddPrice
* @param int $price
* @param int $amount
* @param int $expected
* @param float $price
* @param float $amount
* @param float $expected
* @return void
*/
public function testCanAdd(int $price, int $amount, int $expected): void
public function testCanAdd(float $price, float $amount, float $expected): void
{
$priceCalculator = $this->getPriceCalculator();
$this->assertSame($expected, $priceCalculator->addPrice($price, $amount));
Expand All @@ -64,12 +64,12 @@ public function dataProviderAddPrice(): array

/**
* @dataProvider dataProviderSubPrice
* @param int $price
* @param int $amount
* @param int $total
* @param float $price
* @param float $amount
* @param float $total
* @return void
*/
public function testCanSub(int $price, int $amount, int $total): void
public function testCanSub(float $price, float $amount, float $total): void
{
$priceCalculator = $this->getPriceCalculator();
$this->assertSame($total, $priceCalculator->subPrice($price, $amount));
Expand Down Expand Up @@ -120,19 +120,19 @@ public function dataProviderMulPrice(): array

/**
* @dataProvider dataProviderSalesTaxOfTotal
* @param int $grossPrice
* @param int $netPrice
* @param int $expectedVat
* @param float $grossPrice
* @param float $netPrice
* @param float $expectedVat
* @return void
*/
public function testCanCalculateSalesTaxOfTotal(int $grossPrice, int $netPrice, int $expectedVat): void
public function testCanCalculateSalesTaxOfTotal(float $grossPrice, float $netPrice, float $expectedVat): void
{
$priceCalculator = $this->getPriceCalculator();
$priceCalculator->setVat(19);
$vatPrice = $priceCalculator->calculateSalesTaxFromTotalPrice($grossPrice);
$this->assertSame($expectedVat, $vatPrice);
$this->assertSame($netPrice, (int)bcsub($grossPrice, $vatPrice));
$this->assertSame($grossPrice, (int)bcadd($netPrice, $vatPrice));
$this->assertSame($netPrice, (float)bcsub($grossPrice, $vatPrice));
$this->assertSame($grossPrice, (float)bcadd($netPrice, $vatPrice));
}

/**
Expand Down Expand Up @@ -161,10 +161,11 @@ public function dataProviderSalesTaxOfTotal(): array

/**
* @dataProvider dataProviderPriceWithSalesTax
* @param int $netPrice
* @param int $expectedPrice
* @param float $netPrice
* @param float $expectedPrice
* @return void
*/
public function testCanCalculatePriceWithSalesTax(int $netPrice, int $expectedPrice): void
public function testCanCalculatePriceWithSalesTax(float $netPrice, float $expectedPrice): void
{
$priceCalculator = $this->getPriceCalculator();
$priceCalculator->setVat(19);
Expand Down Expand Up @@ -198,10 +199,11 @@ public function dataProviderPriceWithSalesTax(): array

/**
* @dataProvider dataProviderCalculateNetPriceFromGrossPrice
* @param int $grossPrice
* @param int $expectedNetPrice
* @param float $grossPrice
* @param float $expectedNetPrice
* @return void
*/
public function testCanCalculateNetPriceFromGrossPrice(int $grossPrice, int $expectedNetPrice): void
public function testCanCalculateNetPriceFromGrossPrice(float $grossPrice, float $expectedNetPrice): void
{
$priceCalculator = $this->getPriceCalculator();
$priceCalculator->setVat(19);
Expand Down

0 comments on commit 1e00862

Please sign in to comment.