NumberUtil::convertToDouble()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Ipag\Classes\Util;
4
5
final class NumberUtil
6
{
7
    /**
8
     * @param mixed $number
9
     *
10
     * @return float
11
     */
12
    public function convertToDouble($number)
13
    {
14
        $number = str_replace(',', '.', (string) $number);
15
16
        if (!is_numeric($number)) {
17
            throw new \UnexpectedValueException("{$number} não é um número válido");
18
        }
19
20
        return (float) number_format($number, 2, '.', '');
0 ignored issues
show
Bug introduced by
$number of type string is incompatible with the type double expected by parameter $num of number_format(). ( Ignorable by Annotation )

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

20
        return (float) number_format(/** @scrutinizer ignore-type */ $number, 2, '.', '');
Loading history...
21
    }
22
23
    /**
24
     * @param string $string
25
     *
26
     * @return string
27
     */
28
    public function getOnlyNumbers($string)
29
    {
30
        return (string) preg_replace('/\D/', '', $string);
31
    }
32
}
33