@@ -20,7 +20,7 @@ class EnvMapper
2020 * @param bool $requireValues
2121 * If true, any unmatched properties will result in an exception. If false, unmatched properties
2222 * will be ignored, which in most cases means they will be uninitialized.
23- * @param array<string, mixed >|null $source
23+ * @param array<string, string|int|float >|null $source
2424 * The array to map from. If not specified, $_ENV will be used. Note that because the
2525 * primary use case is environment variables, the input array MUST have keys that are UPPER_CASE
2626 * strings.
@@ -71,12 +71,12 @@ public function map(string $class, bool $requireValues = false, ?array $source =
7171 * push them into well-typed numeric fields we need to cast them
7272 * appropriately.
7373 *
74- * @param string $val
74+ * @param string|int|float $val
7575 * The value to normalize.
7676 * @return int|float|string|bool
7777 * The passed value, but now with the correct type.
7878 */
79- private function typeNormalize (string $ val , \ReflectionProperty $ rProp ): int |float |string |bool |\BackedEnum
79+ private function typeNormalize (string | int | float $ val , \ReflectionProperty $ rProp ): int |float |string |bool |\BackedEnum
8080 {
8181 $ rType = $ rProp ->getType ();
8282 if ($ rType instanceof \ReflectionNamedType) {
@@ -90,18 +90,18 @@ private function typeNormalize(string $val, \ReflectionProperty $rProp): int|flo
9090 assert ($ backingType instanceof \ReflectionNamedType);
9191 $ isIntBacked = $ backingType ->getName () === 'int ' ;
9292
93- return $ name ::from ($ isIntBacked ? (int ) $ val : $ val );
93+ return $ name ::from ($ isIntBacked ? (int ) $ val : ( string ) $ val );
9494 }
9595
9696 return match ($ name ) {
9797 'string ' => $ val ,
9898 'float ' => is_numeric ($ val )
9999 ? (float ) $ val
100100 : throw TypeMismatch::create ($ rProp ->getDeclaringClass ()->getName (), $ rProp ->getName (), $ val ),
101- 'int ' => (is_numeric ($ val ) && floor (( float ) $ val) === ( float ) $ val )
101+ 'int ' => (is_numeric ($ val ) && is_int ( $ val + 0 ) )
102102 ? (int ) $ val
103103 : throw TypeMismatch::create ($ rProp ->getDeclaringClass ()->getName (), $ rProp ->getName (), $ val ),
104- 'bool ' => in_array (strtolower ($ val ), [1 , '1 ' , 'true ' , 'yes ' , 'on ' ], false ),
104+ 'bool ' => in_array (strtolower (( string ) $ val ), [1 , '1 ' , 'true ' , 'yes ' , 'on ' ], false ),
105105 default => throw TypeMismatch::create ($ rProp ->getDeclaringClass ()->getName (), $ rProp ->getName (), $ val ),
106106 };
107107 }
0 commit comments