GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3a3beb...943a46 )
by Ransford
04:49 queued 11s
created

CappedBillInterpreter::interpret()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 1
dl 0
loc 32
ccs 20
cts 20
cp 1
crap 4
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace BillingBoss\Interpreter;
4
5
use BillingBoss\AbstractBillInterpreter;
6
use BillingBoss\BillContext;
7
use BillingBoss\Expr;
8
use BillingBoss\RangeHelper;
9
10
/**
11
 * Capped bill interpreter
12
 *
13
 * @package   BillingBoss
14
 * @link      https://github.com/ranskills/billing-boss-php
15
 * @copyright Copyright (c) 2018 Ransford Ako Okpoti
16
 * @license   Refer to the LICENSE distributed with this library
17
 * @since     1.0.0
18
 */
19
final class CappedBillInterpreter extends AbstractBillInterpreter
20
{
21
    private const A =
22
        Expr::PERCENT .
23
        Expr::SPACES .
24
        '\[' .
25
        Expr::SPACES .
26
        Expr::POSITIVE_NUMBER .
27
        Expr::COMMA .
28
        Expr::POSITIVE_NUMBER .
29
        Expr::SPACES .
30
        '\]' .
31
        Expr::COMMA .
32
        Expr::RANGE;
33
34 4
    public function __construct()
35
    {
36 4
        parent::__construct(sprintf('/^%1$s(%2$s%1$s)*$/', self::A, Expr::PIPE));
37 4
    }
38
39 1
    private function enforceBoundaries($bill, $lowerLimit, $upperLimit)
40
    {
41 1
        if ($bill > $upperLimit) {
42 1
            $bill = $upperLimit;
43
        }
44 1
        if ($bill < $lowerLimit) {
45 1
            $bill = $lowerLimit;
46
        }
47
48 1
        return $bill;
49
    }
50
51 2
    public function interpret(BillContext $context): float
52
    {
53 2
        $bill = 0.0;
54
55 2
        if (!$this->isValid($context)) {
56 1
            return 0.0;
57
        }
58
59 1
        $parts = preg_split(sprintf('/%s/', Expr::PIPE), $context->getStructure());
60
61 1
        foreach ($parts as $part) {
62 1
            $matches = [];
63
64 1
            \preg_match(sprintf('/^%s$/', self::A), $part, $matches);
65
66 1
            $percent = $matches[1];
67 1
            $capMin = $matches[2];
68 1
            $capMax = $matches[3];
69 1
            $rangeStart = $matches[4];
70 1
            $rangeEnd = $matches[5];
71
72 1
            $amount = $context->getAmount();
73 1
            if (RangeHelper::isInRange([$rangeStart, $rangeEnd], $amount)) {
74 1
                $ctxt = new BillContext($amount, $percent . '%' . ', 1 - * ');
75 1
                $bill = (new PercentageBillInterpreter())->interpret($ctxt);
76
77 1
                $bill = $this->enforceBoundaries($bill, $capMin, $capMax);
78 1
                break;
79
            }
80
        }
81
82 1
        return $bill;
83
    }
84
}
85