1 | <?php |
||
17 | class IntType extends AbstractNumberType |
||
18 | { |
||
19 | /** |
||
20 | * @param int $int |
||
21 | * @param MathAdapterInterface|null $mathAdapter |
||
22 | */ |
||
23 | 39 | public function __construct(int $int, MathAdapterInterface $mathAdapter = null) |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | * |
||
31 | * @return string|float|int |
||
32 | */ |
||
33 | 3 | public function __invoke(int $toType = Type::INT) |
|
|
|||
34 | { |
||
35 | switch ($toType) { |
||
36 | 3 | case Type::INT: |
|
37 | 1 | return $this->value; |
|
38 | 3 | case Type::FLOAT: |
|
39 | 1 | return (float) $this->get(); |
|
40 | 3 | case Type::STRING: |
|
41 | 1 | return (string) $this->get(); |
|
42 | default: |
||
43 | 2 | throw new InvalidTypeCastException(static::class, $this->getTranslatedType($toType)); |
|
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | * |
||
50 | * @return IntType |
||
51 | */ |
||
52 | 27 | public static function valueOf($mixed, int $precision = null): IntType |
|
61 | |||
62 | /** |
||
63 | * Returns a mixed variable as a int. |
||
64 | * |
||
65 | * @param mixed $mixed |
||
66 | * |
||
67 | * @return int |
||
68 | */ |
||
69 | 26 | private static function asInt($mixed): int |
|
78 | } |
||
79 |
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.