Skip to content

Commit

Permalink
php8.1 types
Browse files Browse the repository at this point in the history
  • Loading branch information
manwe committed Jun 28, 2024
1 parent 0caadcc commit c86cff3
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 95 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Library for working with Czech taxes",
"minimum-stability": "stable",
"require": {
"php": ">=7.1"
"php": ">=8.0"
},
"license": "MIT",
"authors": [
Expand All @@ -15,4 +15,4 @@
"autoload": {
"classmap": ["src/"]
}
}
}
21 changes: 11 additions & 10 deletions src/Taxes/CalcLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CalcLogic implements ICalcLogic
* @param Price $price
* @return float|int
*/
public function getUnitPriceWithVatFromPriceObject(Price $price)
public function getUnitPriceWithVatFromPriceObject(Price $price): float|int
{
return $price->getUnitPriceWithoutVat() * $price->getVatRatio();
}
Expand All @@ -27,7 +27,7 @@ public function getUnitPriceWithVatFromPriceObject(Price $price)
* @param Price $price
* @return float
*/
public function getUnitPriceWithoutVatFromPriceObject(Price $price)
public function getUnitPriceWithoutVatFromPriceObject(Price $price): float
{
return $price->getUnitPriceWithVat() - round($price->getUnitPriceWithVat() * $price->getVatCoefficient(), 2);
}
Expand All @@ -39,7 +39,7 @@ public function getUnitPriceWithoutVatFromPriceObject(Price $price)
* @param Price $price
* @return float|int
*/
public function getTotalPriceWithVatFromPriceObject(Price $price)
public function getTotalPriceWithVatFromPriceObject(Price $price): float|int
{
return $price->getUnitPriceWithVat() * $price->getQuantity();
// return round($price->getUnitPriceWithoutVat() * $price->quantity * $price->getVatRatio(), 2);
Expand All @@ -51,7 +51,7 @@ public function getTotalPriceWithVatFromPriceObject(Price $price)
* @param Price $price
* @return float|int
*/
public function getTotalPriceWithoutVatFromPriceObject(Price $price)
public function getTotalPriceWithoutVatFromPriceObject(Price $price): float|int
{
return round($price->getUnitPriceWithoutVat() * $price->quantity, 2);
}
Expand All @@ -63,7 +63,7 @@ public function getTotalPriceWithoutVatFromPriceObject(Price $price)
* @param Price[] $prices
* @return array
*/
public function getTotalsWithVatFromPrices($prices)
public function getTotalsWithVatFromPrices($prices): array
{
$totalsWithVat = [];
foreach ($this->getTotalsWithoutVatFromPrices($prices) as $vatPercent => $totalWithoutVat) {
Expand All @@ -87,7 +87,7 @@ public function getTotalsWithVatFromPrices($prices)
* @param Price[] $prices
* @return mixed
*/
public function getTotalsWithoutVatFromPrices($prices)
public function getTotalsWithoutVatFromPrices($prices): mixed
{
$totals = [];
foreach ($prices as $price) {
Expand All @@ -98,7 +98,8 @@ public function getTotalsWithoutVatFromPrices($prices)
return $totals;
}

public function getTotalVatFromPriceObject(Price $price){
public function getTotalVatFromPriceObject(Price $price): mixed
{
return round($price->getTotalPriceWithVat() - $price->getTotalPriceWithoutVat(), 2);
}

Expand All @@ -108,7 +109,7 @@ public function getTotalVatFromPriceObject(Price $price){
* @param $vatPercent
* @return float
*/
public function getVatCoefficient($vatPercent)
public function getVatCoefficient($vatPercent): float
{
return round($vatPercent / (100 + $vatPercent), 4);
}
Expand All @@ -117,12 +118,12 @@ public function getVatCoefficient($vatPercent)
* @param $vatPercent
* @return bool|mixed
*/
public function validateVatPercent($vatPercent)
public function validateVatPercent($vatPercent): mixed
{
return in_array($vatPercent, [
Rates::LOW_PERCENT,
Rates::MEDIUM_PERCENT,
Rates::HIGH_PERCENT
]);
}
}
}
9 changes: 5 additions & 4 deletions src/Taxes/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
class Discount
{
/** @var float */
public $amount = 0.0;
public float $amount = 0.0;

/** @var bool */
public $isOnVat = true;
public mixed $isOnVat = true;

// /** @var null|int */
// public $vatPercent = null;
Expand All @@ -27,7 +27,8 @@ public function __construct($amount, $isOnVat = true)
$this->isOnVat = $isOnVat;
}

public function getAmount($precision = 2){
public function getAmount($precision = 2): float
{
return round($this->amount, $precision);
}
}
}
20 changes: 10 additions & 10 deletions src/Taxes/ICalcLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,69 @@ interface ICalcLogic
* @param Price $price
* @return float|int
*/
public function getUnitPriceWithVatFromPriceObject(Price $price);
public function getUnitPriceWithVatFromPriceObject(Price $price): float|int;

/**
* Method for calculating price without vat from price with vat
*
* @param Price $price
* @return float
*/
public function getUnitPriceWithoutVatFromPriceObject(Price $price);
public function getUnitPriceWithoutVatFromPriceObject(Price $price): float;

/**
* Method for calculating total amount with vat from price object
*
* @param Price $price
* @return float|int
*/
public function getTotalPriceWithVatFromPriceObject(Price $price);
public function getTotalPriceWithVatFromPriceObject(Price $price): float|int;

/**
* Method for calculating total amount without vat from price object
*
* @param Price $price
* @return float|int
*/
public function getTotalPriceWithoutVatFromPriceObject(Price $price);
public function getTotalPriceWithoutVatFromPriceObject(Price $price): float|int;

/**
* Method for calculating array of totals with vat from prices array
*
* @param Price[] $prices
* @return array
*/
public function getTotalsWithVatFromPrices($prices);
public function getTotalsWithVatFromPrices(array $prices): array;

/**
* Method for calculating array of totals without vat from prices array
*
* @param Price[] $prices
* @return mixed
*/
public function getTotalsWithoutVatFromPrices($prices);
public function getTotalsWithoutVatFromPrices(array $prices): mixed;

/**
* Method for calculating amout of vat
*
* @param Price $price
* @return mixed
*/
public function getTotalVatFromPriceObject(Price $price);
public function getTotalVatFromPriceObject(Price $price): mixed;

/**
* Returns correctly rounded var coefficient
*
* @param $vatPercent
* @return float
*/
public function getVatCoefficient($vatPercent);
public function getVatCoefficient(float|int $vatPercent): float;

/**
* Returns false if passed percentage is not allowed
*
* @param $vatPercent
* @return mixed
*/
public function validateVatPercent($vatPercent);
}
public function validateVatPercent(float|int $vatPercent): mixed;
}
49 changes: 26 additions & 23 deletions src/Taxes/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
class Price
{
/** @var float */
public $priceWithVat;
public ?float $priceWithVat;

/** @var float */
public $priceWithoutVat;
public ?float $priceWithoutVat;

/** @var int */
public $vatPercent;
public int $vatPercent;

/** @var float */
public $quantity = 1.0;
public float $quantity = 1.0;

/** @var Discount|null */
public $discount = null;
public ?Discount $discount = null;

/** @var ICalcLogic */
protected $calcLogic;
protected ICalcLogic $calcLogic;

/** @var float */
protected $originalPriceWithVat;
protected ?float $originalPriceWithVat;

/** @var float */
protected $originalPriceWithoutVat;
protected ?float $originalPriceWithoutVat;

/**
* Price constructor.
Expand Down Expand Up @@ -61,7 +61,7 @@ public function __construct(ICalcLogic $compLogic, $vatPercent, $priceWithVat =
* @param float $quantity
* @return Price
*/
public static function createFromPriceWithVat(ICalcLogic $compLogic, $vatPercent, $priceWithVat, $quantity = 1.0)
public static function createFromPriceWithVat(ICalcLogic $compLogic, $vatPercent, $priceWithVat, $quantity = 1.0): Price
{
return new static($compLogic, $vatPercent, $priceWithVat, null, $quantity);
}
Expand All @@ -73,23 +73,23 @@ public static function createFromPriceWithVat(ICalcLogic $compLogic, $vatPercent
* @param float $quantity
* @return Price
*/
public static function createFromPriceWithoutVat(ICalcLogic $compLogic, $vatPercent, $priceWithoutVat, $quantity = 1.0)
public static function createFromPriceWithoutVat(ICalcLogic $compLogic, $vatPercent, $priceWithoutVat, $quantity = 1.0): Price
{
return new static($compLogic, $vatPercent, null, $priceWithoutVat, $quantity);
}

/**
* @return float
*/
public function getTotalPriceWithVat()
public function getTotalPriceWithVat(): float
{
return $this->calcLogic->getTotalPriceWithVatFromPriceObject($this);
}

/**
* @return float
*/
public function getTotalPriceWithoutVat()
public function getTotalPriceWithoutVat(): float
{
return $this->calcLogic->getTotalPriceWithoutVatFromPriceObject($this);
}
Expand All @@ -101,7 +101,7 @@ public function getTotalVat(){
/**
* @return float
*/
public function getUnitPriceWithVat()
public function getUnitPriceWithVat(): ?float
{
if (!isset($this->priceWithVat)) {
$this->priceWithVat = $this->calcLogic->getUnitPriceWithVatFromPriceObject($this);
Expand All @@ -114,7 +114,7 @@ public function getUnitPriceWithVat()
/**
* @return float
*/
public function getUnitPriceWithoutVat()
public function getUnitPriceWithoutVat(): ?float
{
if (!isset($this->priceWithoutVat)) {
$this->priceWithoutVat = $this->calcLogic->getUnitPriceWithoutVatFromPriceObject($this);
Expand All @@ -124,31 +124,31 @@ public function getUnitPriceWithoutVat()
return $this->priceWithoutVat;
}

public function getQuantity()
public function getQuantity(): float
{
return $this->quantity;
}

/**
* @return int
*/
public function getVatPercent()
public function getVatPercent(): int
{
return $this->vatPercent;
}

/**
* @return float
*/
public function getVatCoefficient()
public function getVatCoefficient(): float
{
return $this->calcLogic->getVatCoefficient($this->getVatPercent());
}

/**
* @return float
*/
public function getVatRatio()
public function getVatRatio(): float
{
return static::calculateVatRatio($this->getVatPercent());
}
Expand All @@ -157,7 +157,7 @@ public function getVatRatio()
* @param $vatPercent
* @return float
*/
public static function calculateVatRatio($vatPercent)
public static function calculateVatRatio($vatPercent): float
{
return round((100 + $vatPercent) / 100, 4);
}
Expand All @@ -167,7 +167,8 @@ public static function calculateVatRatio($vatPercent)
* @param bool $isOnVat
* @return $this
*/
public function setDiscount($amount, $isOnVat = true){
public function setDiscount($amount, $isOnVat = true): static
{
if($isOnVat){
$discountedPriceWithVat = $this->getUnitPriceWithVat() - $amount;
$this->priceWithVat = $discountedPriceWithVat;
Expand All @@ -180,11 +181,13 @@ public function setDiscount($amount, $isOnVat = true){
return $this;
}

public function getOriginalPriceWithVat(){
public function getOriginalPriceWithVat(): ?float
{
return $this->originalPriceWithVat;
}

public function getOriginalPriceWithoutVat(){
public function getOriginalPriceWithoutVat(): ?float
{
return $this->originalPriceWithoutVat;
}
}
}
Loading

0 comments on commit c86cff3

Please sign in to comment.