Skip to content

Commit

Permalink
remove value type and variable type checking
Browse files Browse the repository at this point in the history
per review feedback, this can be checked with tests rather than at runtime
  • Loading branch information
brettmc committed Apr 23, 2024
1 parent ab0630a commit 740e226
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 340 deletions.
55 changes: 8 additions & 47 deletions src/Config/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,23 @@ public static function has(string $name): bool
public static function getInt(string $key, int $default = null): int
{
return (int) self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::INTEGER),
$default
),
CompositeResolver::instance()->resolve($key, $default),
FILTER_VALIDATE_INT
);
}

public static function getString(string $key, string $default = null): string
{
return (string) self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::STRING),
$default
)
CompositeResolver::instance()->resolve($key, $default),
);
}

public static function getBoolean(string $key, bool $default = null): bool
{
$resolved = self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::BOOL),
$key,
null === $default ? $default : ($default ? 'true' : 'false')
)
);
Expand All @@ -71,40 +65,28 @@ public static function getMixed(string $key, $default = null): mixed
public static function getMap(string $key, array $default = null): array
{
return MapParser::parse(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::MAP),
$default
)
CompositeResolver::instance()->resolve($key, $default),
);
}

public static function getList(string $key, array $default = null): array
{
return ListParser::parse(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::LIST),
$default
)
CompositeResolver::instance()->resolve($key, $default),
);
}

public static function getEnum(string $key, string $default = null): string
{
return (string) self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::ENUM),
$default
)
CompositeResolver::instance()->resolve($key, $default),
);
}

public static function getFloat(string $key, float $default = null): float
{
return (float) self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::FLOAT),
$default
),
CompositeResolver::instance()->resolve($key, $default),
FILTER_VALIDATE_FLOAT
);
}
Expand All @@ -113,10 +95,7 @@ public static function getRatio(string $key, float $default = null): float
{
return RatioParser::parse(
self::validateVariableValue(
CompositeResolver::instance()->resolve(
self::validateVariableType($key, VariableTypes::RATIO),
$default
)
CompositeResolver::instance()->resolve($key, $default),
)
);
}
Expand All @@ -131,30 +110,12 @@ public static function getDefault(string $variableName)
return ClassConstantAccessor::getValue(Defaults::class, $variableName);
}

public static function getType(string $variableName): ?string
{
return ClassConstantAccessor::getValue(ValueTypes::class, $variableName);
}

public static function isEmpty($value): bool
{
// don't use 'empty()', since '0' is not considered to be empty
return $value === null || $value === '';
}

private static function validateVariableType(string $variableName, string $type): string
{
$variableType = self::getType($variableName);

if ($variableType !== null && $variableType !== $type && $variableType !== VariableTypes::MIXED) {
throw new UnexpectedValueException(
sprintf('Variable "%s" is not supposed to be of type "%s" but type "%s"', $variableName, $type, $variableType)
);
}

return $variableName;
}

private static function validateVariableValue(mixed $value, ?int $filterType = null): mixed
{
if ($filterType !== null && filter_var($value, $filterType) === false) {
Expand Down
136 changes: 0 additions & 136 deletions src/Config/Configuration/ValueTypes.php

This file was deleted.

62 changes: 0 additions & 62 deletions src/Config/Configuration/VariableTypes.php

This file was deleted.

Loading

0 comments on commit 740e226

Please sign in to comment.