Skip to content

Commit 0de5594

Browse files
committed
Type pedantry fixes.
1 parent 95ad083 commit 0de5594

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/EnvMapper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/EnvMapperTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public function mapping_different_types_with_defaults_parses_correctly(): void
3636
/** @var SampleEnvironment $env */
3737
$env = $mapper->map(SampleEnvironment::class, source: $this->source);
3838

39-
self::assertNotNull($env->phpVersion);
40-
self::assertNotNull($env->xdebug_mode);
41-
self::assertNotNull($env->PATH);
42-
self::assertNotNull($env->hostname);
43-
self::assertNotNull($env->shlvl);
39+
self::assertEquals($this->source['PHP_VERSION'], $env->phpVersion);
40+
self::assertEquals($this->source['XDEBUG_MODE'], $env->xdebug_mode);
41+
self::assertEquals($this->source['PATH'], $env->PATH);
42+
self::assertEquals($this->source['HOSTNAME'], $env->hostname);
43+
self::assertEquals($this->source['SHLVL'], $env->shlvl);
4444
self::assertSame('01234', $env->zipCode);
4545
self::assertSame(true, $env->bool);
4646
self::assertEquals(StringBackedEnum::Foo, $env->stringBackedEnum);

0 commit comments

Comments
 (0)