hiqdev /
php-billing
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * PHP Billing Library |
||||
| 4 | * |
||||
| 5 | * @link https://github.com/hiqdev/php-billing |
||||
| 6 | * @package php-billing |
||||
| 7 | * @license BSD-3-Clause |
||||
| 8 | * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace hiqdev\php\billing\charge\modifiers; |
||||
| 12 | |||||
| 13 | use hiqdev\php\billing\action\ActionInterface; |
||||
| 14 | use hiqdev\php\billing\charge\Charge; |
||||
| 15 | use hiqdev\php\billing\charge\ChargeInterface; |
||||
| 16 | use hiqdev\php\billing\charge\modifiers\addons\Discount; |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | use hiqdev\php\billing\type\Type; |
||||
| 18 | use hiqdev\php\units\Quantity; |
||||
| 19 | use Money\Money; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Fixed discount. |
||||
| 23 | * |
||||
| 24 | * @author Andrii Vasyliev <[email protected]> |
||||
| 25 | */ |
||||
| 26 | class FixedDiscount extends Modifier |
||||
| 27 | { |
||||
| 28 | const VALUE = 'value'; |
||||
| 29 | |||||
| 30 | 13 | public function __construct($value, array $addons = []) |
|||
| 31 | { |
||||
| 32 | 13 | parent::__construct($addons); |
|||
| 33 | 13 | $this->addAddon(self::VALUE, new Discount($value)); |
|||
| 34 | 13 | } |
|||
| 35 | |||||
| 36 | 13 | public function getNext() |
|||
| 37 | { |
||||
| 38 | 13 | return $this; |
|||
| 39 | } |
||||
| 40 | |||||
| 41 | 5 | public function getValue(ChargeInterface $charge = null): Discount |
|||
| 42 | { |
||||
| 43 | 5 | return $this->getAddon(self::VALUE); |
|||
|
0 ignored issues
–
show
|
|||||
| 44 | } |
||||
| 45 | |||||
| 46 | 3 | public function isAbsolute() |
|||
| 47 | { |
||||
| 48 | 3 | return $this->getValue()->isAbsolute(); |
|||
| 49 | } |
||||
| 50 | |||||
| 51 | 5 | public function isRelative() |
|||
| 52 | { |
||||
| 53 | 5 | return !$this->isAbsolute(); |
|||
| 54 | } |
||||
| 55 | |||||
| 56 | 4 | public function calculateSum(ChargeInterface $charge = null): Money |
|||
| 57 | { |
||||
| 58 | 4 | return $this->getValue($charge)->calculateSum($charge); |
|||
|
0 ignored issues
–
show
It seems like
$charge can also be of type null; however, parameter $charge of hiqdev\php\billing\charg...iscount::calculateSum() does only seem to accept hiqdev\php\billing\charge\ChargeInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 59 | } |
||||
| 60 | |||||
| 61 | 4 | private function getType() |
|||
| 62 | { |
||||
| 63 | 4 | $chargeTypeAddon = $this->getChargeType(); |
|||
| 64 | |||||
| 65 | 4 | $type = $chargeTypeAddon ? $chargeTypeAddon->getValue() : 'discount,discount'; |
|||
|
0 ignored issues
–
show
|
|||||
| 66 | |||||
| 67 | 4 | return new Type(Type::ANY, $type); |
|||
| 68 | } |
||||
| 69 | |||||
| 70 | 4 | public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
|||
| 71 | { |
||||
| 72 | 4 | if ($charge === null) { |
|||
| 73 | return []; |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | 4 | $month = $action->getTime()->modify('first day of this month midnight'); |
|||
| 77 | 4 | if (!$this->checkPeriod($month)) { |
|||
| 78 | return [$charge]; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | 4 | $sum = $this->calculateSum($charge)->multiply((string) -1); |
|||
| 82 | 4 | $usage = Quantity::create('items', 0); |
|||
| 83 | 4 | $type = $this->getType(); |
|||
| 84 | 4 | $price = $charge->getPrice(); |
|||
| 85 | 4 | $target = $charge->getTarget(); |
|||
| 86 | |||||
| 87 | 4 | $discount = new Charge(null, $type, $target, $action, $price, $usage, $sum); |
|||
| 88 | 4 | $discount->setParent($charge); |
|||
| 89 | |||||
| 90 | 4 | $reason = $this->getReason(); |
|||
| 91 | 4 | if ($reason) { |
|||
|
0 ignored issues
–
show
|
|||||
| 92 | $discount->setComment($reason->getValue()); |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | 4 | return [$charge, $discount]; |
|||
| 96 | } |
||||
| 97 | } |
||||
| 98 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: