1 | <?php |
||||
2 | |||||
3 | namespace hiqdev\billing\hiapi\tools; |
||||
4 | |||||
5 | use hiqdev\billing\hiapi\charge\Generalizer; |
||||
6 | use hiqdev\billing\hiapi\type\TypeSemantics; |
||||
7 | use hiqdev\php\billing\bill\BillInterface; |
||||
8 | use hiqdev\php\billing\charge\GeneralizerInterface; |
||||
9 | use hiqdev\php\units\Quantity; |
||||
10 | use hiqdev\php\units\QuantityInterface; |
||||
11 | use hiqdev\php\units\Unit; |
||||
12 | |||||
13 | /** |
||||
14 | * Normally, when monthly charges are put in the single bill, the bill quantity |
||||
15 | * increases accordingly. But it's not the right scenario for monthly bills: all |
||||
16 | * the charges are for the month quantity, but the whole bill is for one month as well. |
||||
17 | * |
||||
18 | * Class MonthlyBillQuantityFixer is applicable only for monthly bills and |
||||
19 | * adjusts the quantity of monthly bills according to the number of days in month. |
||||
20 | * |
||||
21 | * @author Dmytro Naumenko <[email protected]> |
||||
22 | */ |
||||
23 | final class MonthlyBillQuantityFixer |
||||
24 | { |
||||
25 | /** |
||||
26 | * @var Generalizer |
||||
27 | */ |
||||
28 | private $generalizer; |
||||
29 | |||||
30 | /** |
||||
31 | * @var TypeSemantics |
||||
32 | */ |
||||
33 | private $typeSemantics; |
||||
34 | |||||
35 | public function __construct( |
||||
36 | GeneralizerInterface $generalizer, |
||||
37 | TypeSemantics $typeSemantics |
||||
38 | ) { |
||||
39 | $this->generalizer = $generalizer; |
||||
0 ignored issues
–
show
|
|||||
40 | $this->typeSemantics = $typeSemantics; |
||||
41 | } |
||||
42 | |||||
43 | /** |
||||
44 | * @param BillInterface $bill |
||||
45 | */ |
||||
46 | public function __invoke($bill): void |
||||
47 | { |
||||
48 | if ($this->typeSemantics->isMonthly($bill->getType())) { |
||||
49 | $bill->setQuantity($this->calculateMonthlyQuantity($bill)); |
||||
50 | } |
||||
51 | } |
||||
52 | |||||
53 | private function calculateMonthlyQuantity(BillInterface $bill): QuantityInterface |
||||
54 | { |
||||
55 | $res = null; |
||||
56 | foreach ($bill->getCharges() as $charge) { |
||||
57 | $amount = $this->generalizer->generalizeQuantity($charge); |
||||
58 | if (!$amount->getUnit()->isConvertible(Unit::days())) { |
||||
59 | continue; |
||||
60 | } |
||||
61 | if ($res === null || $amount->compare($res)>0) { |
||||
0 ignored issues
–
show
$res of type null is incompatible with the type hiqdev\php\units\QuantityInterface expected by parameter $other of hiqdev\php\units\QuantityInterface::compare() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
62 | $res = $amount; |
||||
63 | } |
||||
64 | } |
||||
65 | return $res ?? Quantity::create('days', 1); |
||||
66 | } |
||||
67 | } |
||||
68 |
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 given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.