Skip to content

Commit

Permalink
misc: move "float type accepting integer value" logic in Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Sep 2, 2024
1 parent 84b1ffb commit 0479532
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
9 changes: 0 additions & 9 deletions src/Mapper/Tree/Builder/TreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use CuyZ\Valinor\Mapper\Tree\Message\Message;
use CuyZ\Valinor\Mapper\Tree\Node;
use CuyZ\Valinor\Mapper\Tree\Shell;
use CuyZ\Valinor\Type\FloatType;
use CuyZ\Valinor\Type\Type;
use Throwable;

Expand All @@ -35,14 +34,6 @@ final class TreeNode

private function __construct(Shell $shell, mixed $value)
{
// When the value is an integer and the type is a float, the value needs
// to be cast to float — this special case needs to be handled in case a
// node is not a *native* PHP float type (for instance a class property
// with a `@var float` annotation).
if ($shell->type() instanceof FloatType && is_int($value)) {
$value = (float)$value;
}

$this->shell = $shell;
$this->value = $value;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Mapper/Tree/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CuyZ\Valinor\Definition\Attributes;
use CuyZ\Valinor\Library\Settings;
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType;
use CuyZ\Valinor\Type\FloatType;
use CuyZ\Valinor\Type\Type;
use CuyZ\Valinor\Type\Types\UnresolvableType;

Expand Down Expand Up @@ -87,6 +88,14 @@ public function type(): Type

public function withValue(mixed $value): self
{
// When the value is an integer and the type is a float, the value is
// cast to float, to follow the rule of PHP regarding acceptance of an
// integer value in a float type. Note that PHPStan/Psalm analysis
// applies the same rule.
if ($this->type instanceof FloatType && is_int($value)) {
$value = (float)$value;
}

$clone = clone $this;
$clone->hasValue = true;
$clone->value = $value;
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Types/NativeFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use function assert;
use function is_float;
use function is_integer;
use function is_numeric;

/** @internal */
Expand All @@ -22,7 +21,7 @@ final class NativeFloatType implements FloatType

public function accepts(mixed $value): bool
{
return is_float($value) || is_integer($value);
return is_float($value);
}

public function matches(Type $other): bool
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Type/Types/NativeFloatTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ protected function setUp(): void
public function test_accepts_correct_values(): void
{
self::assertTrue($this->floatType->accepts(42.1337));
self::assertTrue($this->floatType->accepts(404));
}

public function test_does_not_accept_incorrect_values(): void
{
self::assertFalse($this->floatType->accepts(null));
self::assertFalse($this->floatType->accepts('Schwifty!'));
self::assertFalse($this->floatType->accepts(404));
self::assertFalse($this->floatType->accepts(['foo' => 'bar']));
self::assertFalse($this->floatType->accepts(false));
self::assertFalse($this->floatType->accepts(new stdClass()));
Expand Down

0 comments on commit 0479532

Please sign in to comment.