FloatType::asFloat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tdn\PhpTypes\Type;
6
7
use Tdn\PhpTypes\Exception\InvalidTypeCastException;
8
use Tdn\PhpTypes\Math\MathAdapterInterface;
9
10
/**
11
 * Class FloatType.
12
 *
13
 * A FloatType is a TypeInterface implementation that wraps around a regular PHP float / double.
14
 *
15
 * {@inheritdoc}
16
 */
17
class FloatType extends AbstractNumberType
18
{
19
    /**
20
     * @param float                     $float
21
     * @param null|int                  $precision
22
     * @param MathAdapterInterface|null $mathAdapter
23
     */
24 31
    public function __construct(float $float, int $precision = null, MathAdapterInterface $mathAdapter = null)
25
    {
26 31
        parent::__construct($float, $precision, $mathAdapter);
27 31
    }
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @return string|int|float
33
     */
34 4
    public function __invoke(int $toType = Type::FLOAT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        switch ($toType) {
37 4
            case Type::FLOAT:
38 2
                return $this->value;
39 3
            case Type::INT:
40 1
                return (int) $this->get();
41 3
            case Type::STRING:
42 1
                return (string) $this->get();
43
            default:
44 2
                throw new InvalidTypeCastException(static::class, $this->getTranslatedType($toType));
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @return FloatType
52
     */
53 27
    public static function valueOf($mixed, int $precision = null): FloatType
54
    {
55 27
        return new static(self::asFloat($mixed), $precision);
56
    }
57
58
    /**
59
     * Returns a mixed variable as a float.
60
     *
61
     * @param mixed $mixed
62
     *
63
     * @return float
64
     */
65 27
    private static function asFloat($mixed): float
66
    {
67 27
        return static::asSubType('floatval', $mixed);
68
    }
69
}
70