Passed
Pull Request — master (#67)
by
unknown
13:46
created

PriceHelper::buildMoney()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use hiqdev\php\units\Quantity;
8
use hiqdev\php\units\Unit;
9
use Money\Currency;
10
use Money\Money;
11
12
class PriceHelper
13
{
14
    public static function buildMoney(string $price, string $currency): ?Money
15
    {
16
        if (!is_numeric($price)) {
17
            throw new \InvalidArgumentException('price of the Money must be numeric');
18
        }
19
        if (is_int($price)) {
0 ignored issues
show
introduced by
The condition is_int($price) is always false.
Loading history...
20
            return new Money($price, new Currency($currency));
21
        }
22
        if (!is_int($price) && ($price * 100) == ((int) ($price * 100))) {
0 ignored issues
show
introduced by
The condition is_int($price) is always false.
Loading history...
23
            return new Money((int) $price * 100, new Currency($currency));
24
        }
25
        list($int, $float) = explode('.', $price);
26
        return new Money(
27
            (int) ($price * (int) (1 . implode(array_fill(0, strlen($float),0)))),
28
            new Currency($currency)
29
        );
30
    }
31
32
    public static function buildQuantityByMoneyPrice(string $price, string $unit, string $quantity): ?Quantity
33
    {
34
        if (!is_numeric($price)) {
35
            throw new \InvalidArgumentException('price of the Money must be numeric');
36
        }
37
        if (is_int($price)) {
0 ignored issues
show
introduced by
The condition is_int($price) is always false.
Loading history...
38
            return Quantity::create(Unit::create($unit), $quantity);
39
        }
40
        if (!is_int((int) $price) && ($price * 100) == ((int) ($price * 100))) {
0 ignored issues
show
introduced by
The condition is_int((int)$price) is always true.
Loading history...
41
            return Quantity::create(Unit::create($unit), $quantity * 100);
42
        }
43
        list($int, $float) = explode('.', $price);
44
        return Quantity::create(
45
            Unit::create($unit),
0 ignored issues
show
Bug introduced by
hiqdev\php\units\Unit::create($unit) of type hiqdev\php\units\Unit is incompatible with the type string expected by parameter $unit of hiqdev\php\units\AbstractQuantity::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            /** @scrutinizer ignore-type */ Unit::create($unit),
Loading history...
46
            $quantity * (int) (1 . implode(array_fill(0, strlen($float),0)))
47
        );
48
    }
49
50
}
51