pronamic /
wp-money
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Taxed Money |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2021 Pronamic |
||
| 7 | * @license GPL-3.0-or-later |
||
| 8 | * @package Pronamic\WordPress\Money |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Pronamic\WordPress\Money; |
||
| 12 | |||
| 13 | use JsonSerializable; |
||
| 14 | use Pronamic\WordPress\Number\Number; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Taxed Money |
||
| 18 | * |
||
| 19 | * @author Remco Tolsma |
||
| 20 | * @version 1.2.2 |
||
| 21 | * @since 1.2.0 |
||
| 22 | */ |
||
| 23 | class TaxedMoney extends Money { |
||
| 24 | /** |
||
| 25 | * Tax value. |
||
| 26 | * |
||
| 27 | * @var float|null |
||
| 28 | */ |
||
| 29 | private $tax_value; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Tax percentage. |
||
| 33 | * |
||
| 34 | * @var float|null |
||
| 35 | */ |
||
| 36 | private $tax_percentage; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Construct and initialize money object. |
||
| 40 | * |
||
| 41 | * @param string|int|float $value Amount value. |
||
| 42 | * @param Currency|string $currency Currency. |
||
| 43 | * @param string|int|float $tax_value Tax value. |
||
| 44 | * @param string|int|float $tax_percentage Tax percentage. |
||
| 45 | */ |
||
| 46 | public function __construct( $value = 0, $currency = 'EUR', $tax_value = null, $tax_percentage = null ) { |
||
| 47 | parent::__construct( $value, $currency ); |
||
| 48 | |||
| 49 | // Calculate tax amount if tax percentage is set. |
||
| 50 | if ( null === $tax_value && null !== $tax_percentage ) { |
||
| 51 | $calculator = $this->get_calculator(); |
||
| 52 | |||
| 53 | $one_percent_value = $calculator->divide( strval( $value ), $calculator->add( strval( 100 ), strval( $tax_percentage ) ) ); |
||
| 54 | |||
| 55 | if ( null !== $one_percent_value ) { |
||
| 56 | $tax_value = $calculator->multiply( strval( $one_percent_value ), $tax_percentage ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->set_tax_value( $tax_value ); |
||
| 61 | $this->set_tax_percentage( $tax_percentage ); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get tax amount. |
||
| 66 | * |
||
| 67 | * @return Money|null Tax amount. |
||
| 68 | */ |
||
| 69 | public function get_tax_amount() { |
||
| 70 | if ( null === $this->tax_value ) { |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | |||
| 74 | return new Money( $this->tax_value, $this->get_currency() ); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get tax value. |
||
| 79 | * |
||
| 80 | * @return string|null |
||
| 81 | */ |
||
| 82 | public function get_tax_value() { |
||
| 83 | return null === $this->tax_value ? null : $this->tax_value->get_value(); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Set tax value. |
||
| 88 | * |
||
| 89 | * @param float|int|string|null $value Tax value. |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function set_tax_value( $value ) { |
||
| 93 | $this->tax_value = ( null === $value ? null : Number::from_mixed( $value ) ); |
||
|
0 ignored issues
–
show
|
|||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Has tax? |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function has_tax() { |
||
| 102 | return ( null !== $this->get_tax_value() ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get tax percentage. |
||
| 107 | * |
||
| 108 | * @return string|null |
||
| 109 | */ |
||
| 110 | public function get_tax_percentage() { |
||
| 111 | return null === $this->tax_percentage ? null : $this->tax_percentage->get_value(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set tax percentage. |
||
| 116 | * |
||
| 117 | * 100% = 100 |
||
| 118 | * 21% = 21 |
||
| 119 | * 6% = 6 |
||
| 120 | * 1.5% = 1.5 |
||
| 121 | * |
||
| 122 | * @param string|int|float|null $percentage Tax percentage. |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function set_tax_percentage( $percentage ) { |
||
| 126 | $this->tax_percentage = ( null === $percentage ? null : Number::from_mixed( $percentage ) ); |
||
|
0 ignored issues
–
show
It seems like
null === $percentage ? n...from_mixed($percentage) can also be of type Pronamic\WordPress\Number\Number. However, the property $tax_percentage is declared as type double|null. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get including tax. |
||
| 131 | * |
||
| 132 | * @return Money |
||
| 133 | */ |
||
| 134 | public function get_including_tax() { |
||
| 135 | return new Money( $this->get_value(), $this->get_currency() ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get excluding tax. |
||
| 140 | * |
||
| 141 | * @return Money |
||
| 142 | */ |
||
| 143 | public function get_excluding_tax() { |
||
| 144 | $tax_amount = $this->get_tax_amount(); |
||
| 145 | |||
| 146 | if ( null === $tax_amount ) { |
||
| 147 | return $this->get_including_tax(); |
||
| 148 | } |
||
| 149 | |||
| 150 | return $this->subtract( $tax_amount ); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * JSON serialize. |
||
| 155 | * |
||
| 156 | * @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php |
||
| 157 | * @return object |
||
| 158 | */ |
||
| 159 | public function jsonSerialize() { |
||
| 160 | $object = parent::jsonSerialize(); |
||
| 161 | |||
| 162 | $properties = (array) $object; |
||
| 163 | |||
| 164 | if ( null !== $this->tax_value ) { |
||
| 165 | $properties['tax_value'] = $this->tax_value->jsonSerialize(); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ( null !== $this->tax_percentage ) { |
||
| 169 | $properties['tax_percentage'] = $this->tax_percentage->jsonSerialize(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $object = (object) $properties; |
||
| 173 | |||
| 174 | return $object; |
||
| 175 | } |
||
| 176 | } |
||
| 177 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.