Completed
Push — master ( 256f7a...476c16 )
by Joachim
06:08
created

createMoney()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 2
cts 3
cp 0.6667
crap 2.1481
rs 10
1
<?php
2
3
namespace Loevgaard\DandomainFoundation;
4
5
use Brick\Math\BigDecimal;
6
use Money\Currency;
7
use Money\Money;
8
9
/**
10
 * Creates a Money object.
11
 *
12
 * @param string $currency
13
 * @param int    $amount
14
 *
15
 * @return Money|null
16
 */
17
function createMoney(string $currency, int $amount = 0): ?Money
18
{
19 30
    if (!$currency) {
20
        return null;
21
    }
22
23 30
    return new Money($amount, new Currency($currency));
24
}
25
26
/**
27
 * Creates a Money object from a float/string.
28
 *
29
 * @param string       $currency
30
 * @param float|string $amount
31
 *
32
 * @return Money|null
33
 */
34
function createMoneyFromFloat(string $currency, $amount = 0.0): ?Money
35
{
36 21
    $amount = BigDecimal::of((string) round($amount, 2))->multipliedBy(100)->toInt();
0 ignored issues
show
Bug introduced by
It seems like $amount can also be of type string; however, parameter $val of round() does only seem to accept double, 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

36
    $amount = BigDecimal::of((string) round(/** @scrutinizer ignore-type */ $amount, 2))->multipliedBy(100)->toInt();
Loading history...
37
38 21
    return createMoney($currency, $amount);
39
}
40
41
function objectToArray($obj): array
42
{
43
    if ($obj instanceof \stdClass) {
44
        $obj = json_decode(json_encode($obj), true);
45
    }
46
47
    return (array) $obj;
48
}
49