Skip to content

Commit

Permalink
Simplify ScalarType::isValid() method (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Nov 22, 2023
1 parent 181f332 commit 651d0c4
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/core/etl/src/Flow/ETL/PHP/Type/Native/ScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Flow\ETL\PHP\Type\Type;

/**
* @implements NativeType<array{value: string, nullable: bool}>
* @implements NativeType<array{value: ScalarType::*, nullable: bool}>
*/
final class ScalarType implements NativeType
{
Expand All @@ -19,6 +19,9 @@ final class ScalarType implements NativeType

public const STRING = 'string';

/**
* @param self::* $value
*/
private function __construct(private readonly string $value, private readonly bool $nullable)
{
}
Expand Down Expand Up @@ -85,27 +88,17 @@ public function isValid(mixed $value) : bool
return true;
}

if (!\is_scalar($value)) {
return false;
}

if ($this->value === 'float') {
// php gettype returns double for floats for historical reasons
if ('double' !== \gettype($value)) {
return false;
}
} else {
if ($this->value !== \gettype($value)) {
return false;
}
}

return true;
return match ($this->value) {
self::STRING => \is_string($value),
self::INTEGER => \is_int($value),
self::FLOAT => \is_float($value),
self::BOOLEAN => \is_bool($value),
};
}

public function isValidArrayKey() : bool
{
return $this->isString() || $this->isInteger();
return \in_array($this->value, [self::INTEGER, self::STRING], true);
}

public function nullable() : bool
Expand Down

0 comments on commit 651d0c4

Please sign in to comment.