Skip to content

Commit f84e786

Browse files
committed
chore: move int to float casting outside Shell
1 parent 3b0afa3 commit f84e786

2 files changed

Lines changed: 11 additions & 20 deletions

File tree

src/Mapper/Tree/Builder/ScalarNodeBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace CuyZ\Valinor\Mapper\Tree\Builder;
66

77
use CuyZ\Valinor\Mapper\Tree\Shell;
8+
use CuyZ\Valinor\Type\FloatType;
89
use CuyZ\Valinor\Type\ScalarType;
910

1011
use function assert;
12+
use function is_int;
1113

1214
/** @internal */
1315
final class ScalarNodeBuilder implements NodeBuilder
@@ -19,6 +21,14 @@ public function build(Shell $shell): Node
1921

2022
assert($type instanceof ScalarType);
2123

24+
// When the value is an integer and the type is a float, the value is
25+
// cast to float, to follow the rule of PHP regarding acceptance of an
26+
// integer value in a float type. Note that PHPStan/Psalm analysis
27+
// applies the same rule.
28+
if ($type instanceof FloatType && is_int($value)) {
29+
$value = (float)$value;
30+
}
31+
2232
if ($type->accepts($value)) {
2333
return $shell->node($value);
2434
}

src/Mapper/Tree/Shell.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType;
1212
use CuyZ\Valinor\Mapper\Tree\Message\Message;
1313
use CuyZ\Valinor\Type\Dumper\TypeDumper;
14-
use CuyZ\Valinor\Type\FloatType;
1514
use CuyZ\Valinor\Type\Type;
1615
use CuyZ\Valinor\Type\Types\UnionType;
1716
use CuyZ\Valinor\Type\Types\UnresolvableType;
@@ -22,7 +21,6 @@
2221
use function array_map;
2322
use function assert;
2423
use function implode;
25-
use function is_int;
2624

2725
/** @internal */
2826
final class Shell
@@ -54,9 +52,7 @@ public function __construct(
5452
private array $nameMap = [],
5553
/** @var array<string, null> */
5654
private array $childrenWithScalarValueCasting = [],
57-
) {
58-
$this->castFloatValue();
59-
}
55+
) {}
6056

6157
public function build(): Node
6258
{
@@ -119,8 +115,6 @@ public function withType(Type $newType): self
119115
$self = clone $this;
120116
$self->type = $newType;
121117

122-
$self->castFloatValue();
123-
124118
return $self;
125119
}
126120

@@ -131,8 +125,6 @@ public function withValue(mixed $newValue): self
131125
$self->value = $newValue;
132126
$self->hasValue = true;
133127

134-
$self->castFloatValue();
135-
136128
return $self;
137129
}
138130

@@ -236,15 +228,4 @@ public function dumpValue(): string
236228
{
237229
return $this->hasValue ? ValueDumper::dump($this->value) : '*missing*';
238230
}
239-
240-
private function castFloatValue(): void
241-
{
242-
// When the value is an integer and the type is a float, the value is
243-
// cast to float, to follow the rule of PHP regarding acceptance of an
244-
// integer value in a float type. Note that PHPStan/Psalm analysis
245-
// applies the same rule.
246-
if ($this->type instanceof FloatType && is_int($this->value)) {
247-
$this->value = (float)$this->value;
248-
}
249-
}
250231
}

0 commit comments

Comments
 (0)