1 | <?php |
||
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) |
|
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | * |
||
32 | * @return string|int|float |
||
33 | */ |
||
34 | 4 | public function __invoke(int $toType = Type::FLOAT) |
|
|
|||
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 |
|
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 |
|
69 | } |
||
70 |
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.