1 | <?php |
||
2 | |||
3 | namespace BillingBoss\Interpreter; |
||
4 | |||
5 | use BillingBoss\BillContext; |
||
6 | use BillingBoss\AbstractBillInterpreter; |
||
7 | |||
8 | /** |
||
9 | * Progressive bill interpreter |
||
10 | * |
||
11 | * @package BillingBoss |
||
12 | * @link https://github.com/ranskills/billing-boss-php |
||
13 | * @copyright Copyright (c) 2018 Ransford Ako Okpoti |
||
14 | * @license Refer to the LICENSE distributed with this library |
||
15 | * @since 1.0.0 |
||
16 | */ |
||
17 | final class ProgressiveBillInterpreter extends AbstractBillInterpreter |
||
18 | { |
||
19 | |||
20 | 5 | public function __construct() |
|
21 | { |
||
22 | 5 | parent::__construct('/^(-?\d*\.?\d+)%,\s*(\d*\.?\d+)' . |
|
23 | 5 | '(\s*>\s*(-?\d*\.?\d+)%,\s*(\d*\.?\d+))*(\s*>\s*(-?\d*\.?\d+)%,\s*(\*))$/'); |
|
24 | 5 | } |
|
25 | |||
26 | 2 | public function interpret(BillContext $context): float |
|
27 | { |
||
28 | 2 | if (!$this->isValid($context)) { |
|
29 | 1 | return 0.0; |
|
30 | } |
||
31 | |||
32 | 1 | $parts = preg_split('/\s*>\s*/', $context->getStructure()); |
|
33 | 1 | $billableAmount = $context->getAmount(); |
|
34 | 1 | $percentageBillInterpreter = new PercentageBillInterpreter(); |
|
35 | 1 | $percentageCtxt = new BillContext(0, '0%'); |
|
36 | 1 | $bill = 0; |
|
37 | |||
38 | 1 | foreach ($parts as $part) { |
|
39 | 1 | if ($billableAmount === 0.0) { |
|
40 | 1 | break; |
|
41 | } |
||
42 | 1 | $matches = []; |
|
43 | |||
44 | 1 | \preg_match('/^((-?\d*\.?\d+)%),\s*(\d*\.?\d+|\*)$/', $part, $matches); |
|
45 | // print_r($matches); |
||
0 ignored issues
–
show
|
|||
46 | 1 | $amount = $matches[3]; |
|
47 | |||
48 | 1 | if ($amount === '*') { |
|
49 | 1 | $percentageCtxt->setAmount(floatval($billableAmount)) |
|
50 | 1 | ->setStructure($matches[1] . ', 1 - * '); |
|
51 | 1 | $bill += $percentageBillInterpreter->interpret($percentageCtxt); |
|
52 | } else { |
||
53 | 1 | $billableAmount -= $amount; |
|
54 | 1 | $percentageCtxt->setAmount($amount) |
|
55 | 1 | ->setStructure($matches[1] . ', 1 - * '); |
|
56 | 1 | $bill += $percentageBillInterpreter->interpret($percentageCtxt); |
|
57 | } |
||
58 | } |
||
59 | |||
60 | 1 | return $bill; |
|
61 | } |
||
62 | } |
||
63 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.