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 ( d3cdc5...5d7d21 )
by Ransford
01:39
created

PercentageBillInterpreter::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BillingBoss\Interpreter;
4
5
use BillingBoss\Expr;
6
use BillingBoss\BillContext;
7
use BillingBoss\AbstractBillInterpreter;
8
use BillingBoss\RangeHelper;
9
10
final class PercentageBillInterpreter extends AbstractBillInterpreter
11
{
12
    private const EXPRESSION = Expr::PERCENT .
13
                               Expr::COMMA .
14
                               Expr::RANGE;
15
    private $ranges = [];
16
17
    public function __construct()
18
    {
19
        // parent::__construct('/^\s*(-?\d*\.?\d+)\s*%$/');
20
        parent::__construct(sprintf('/^(%1$s)(%2$s%1$s)*$/', self::EXPRESSION, Expr::PIPE));
21
    }
22
23
    public function isValid(BillContext $context): bool
24
    {
25
        list($result, $this->ranges) = RangeHelper::validate($context->getStructure());
26
        return $result === RangeHelper::VALIDATION_OK;
27
    }
28
29
    public function interpret(BillContext $context): float
30
    {
31
        if (!$this->isValid($context)) {
32
            return 0.0;
33
        }
34
        $bill = 0.0;
35
36
        $parts = preg_split(sprintf('/%s/', Expr::PIPE), $context->getStructure());
37
        for ($i = 0; $i < count($parts); $i++) {
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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 ignore-type  annotation

37
        for ($i = 0; $i < count(/** @scrutinizer ignore-type */ $parts); $i++) {
Loading history...
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
38
            $range = $this->ranges[$i];
39
            $matches = [];
40
41
            \preg_match(sprintf('/^%s$/', self::EXPRESSION), $parts[$i], $matches);
42
43
            $amount = $context->getAmount();
44
45
            if (RangeHelper::isInRange($range, $amount)) {
46
                $bill = ($matches[1] * $amount)/ 100.00;
47
                break;
48
            }
49
        }
50
51
        return $bill;
52
    }
53
}
54