Skip to content

Commit b5ecc4f

Browse files
committed
Rename NullableType to OptionalType and update references across codebase and tests
1 parent 946afa8 commit b5ecc4f

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

src/Usage/Composite/Composite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use const PHP_EOL;
88

99
use PhpTypedValues\Usage\Example\AnyType;
10-
use PhpTypedValues\Usage\Example\NullableType;
10+
use PhpTypedValues\Usage\Example\OptionalType;
1111
use PhpTypedValues\Usage\Example\StrictType;
1212

1313
/**
@@ -27,7 +27,7 @@
2727
echo $test->getFirstName()->toString() . PHP_EOL;
2828
echo $test->getHeight()->toString() . PHP_EOL;
2929

30-
$test = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
30+
$test = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
3131

3232
echo $test->getId()->toString() . PHP_EOL;
3333
echo $test->getFirstName()->toString() . PHP_EOL;
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @psalm-internal PhpTypedValues
2020
*/
21-
final readonly class NullableType
21+
final readonly class OptionalType
2222
{
2323
public function __construct(
2424
private IntegerPositive $id,
@@ -34,12 +34,14 @@ public function __construct(
3434
public static function fromScalars(
3535
int $id,
3636
?string $firstName,
37-
string|float|int|null $height,
37+
string|float|int|null $height = null,
3838
): self {
3939
return new self(
4040
IntegerPositive::fromInt($id), // Early fail
4141
StringNonEmpty::tryFromMixed($firstName), // Late fail
42-
$height !== null ? FloatPositive::fromString((string) $height) : Undefined::create(), // Late fail for NULL, Early fail for anything else
42+
$height !== null
43+
? FloatPositive::fromString((string) $height) // Early fail for not NULL
44+
: Undefined::create(), // Late fail for NULL
4345
);
4446
}
4547

src/Usage/Util/Performance.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function main(array $argv): int
5151
$objects['mem_delta_mb'],
5252
);
5353

54-
// Percent difference of objects vs ints (how much more/less objects use than ints)
54+
// Percent difference of objects vs. ints (how much more/fewer objects use than ints)
5555
$timePct = formatPct(pctDiff($objects['time_ms'], $ints['time_ms']));
5656
$memPct = formatPct(pctDiff($objects['mem_delta_mb'], $ints['mem_delta_mb']));
5757

@@ -90,7 +90,7 @@ function measureInts(int $n): array
9090
$baseUsage = memory_get_usage(true);
9191

9292
$ints = [];
93-
$ints[$n - 1] = 0; // pre-allocate last index (sparse) to reduce reallocs
93+
$ints[$n - 1] = 0; // pre-allocate last index (sparse) to reduce reallocating
9494

9595
$t0 = hrtime(true);
9696
for ($i = 0; $i < $n; ++$i) {
@@ -142,7 +142,7 @@ function measureObjects(int $n): array
142142
// Percentage helpers **************************************************************************************************
143143

144144
/**
145-
* Compute percentage difference: (a / b - 1) * 100. Returns null when baseline is zero or not finite.
145+
* Compute percentage difference: (a / b - 1) * 100. Returns null when the baseline is zero or not finite.
146146
*/
147147
function pctDiff(float $a, float $b): ?float
148148
{

tests/Unit/Usage/Example/NullableTypeTest.php renamed to tests/Unit/Usage/Example/OptionalTypeTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@
66
use PhpTypedValues\Exception\IntegerTypeException;
77
use PhpTypedValues\Float\FloatPositive;
88
use PhpTypedValues\Undefined\Alias\Undefined;
9-
use PhpTypedValues\Usage\Example\NullableType;
9+
use PhpTypedValues\Usage\Example\OptionalType;
1010

11-
it('constructs NullableType from scalars and exposes typed values', function (): void {
12-
$vo = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
11+
it('constructs OptionalType from scalars and exposes typed values', function (): void {
12+
$vo = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
1313

1414
expect($vo->getId()->toString())->toBe('1');
1515
expect($vo->getFirstName()->toString())->toBe('Foobar');
1616
expect($vo->getHeight()->toString())->toBe('170');
1717
});
1818

1919
it('checks string cast', function (): void {
20-
$voInt = NullableType::fromScalars(id: 1, firstName: 'Test', height: 99);
20+
$voInt = OptionalType::fromScalars(id: 1, firstName: 'Test', height: 99);
2121
expect($voInt->getHeight()->value())->toBe(99.0);
2222
});
2323

2424
it('treats empty firstName as Undefined (late-fail semantics)', function (): void {
25-
$vo = NullableType::fromScalars(id: 1, firstName: '', height: 10.0);
25+
$vo = OptionalType::fromScalars(id: 1, firstName: '', height: 10.0);
2626
expect($vo->getFirstName())->toBeInstanceOf(Undefined::class);
2727
});
2828

2929
it('fails early when height is negative', function (): void {
30-
expect(fn() => NullableType::fromScalars(id: 1, firstName: 'Foobar', height: -10.0))
30+
expect(fn() => OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: -10.0))
3131
->toThrow(FloatTypeException::class, 'Expected positive float, got "-10"');
3232
});
3333

3434
it('accepts int/float/numeric-string heights and preserves string formatting via fromString casting', function (): void {
35-
$asInt = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
36-
$asFloat = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: 170.5);
37-
$asString = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: '42.25');
35+
$asInt = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170);
36+
$asFloat = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: 170.5);
37+
$asString = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: '42.25');
3838

3939
expect($asInt->getHeight())->toBeInstanceOf(FloatPositive::class)
4040
->and($asInt->getHeight()->toString())->toBe('170')
@@ -45,22 +45,22 @@
4545
});
4646

4747
it('treats null height as Undefined (late-fail semantics)', function (): void {
48-
$obj = NullableType::fromScalars(id: 1, firstName: 'Foobar', height: null);
48+
$obj = OptionalType::fromScalars(id: 1, firstName: 'Foobar', height: null);
4949
expect($obj->getHeight())->toBeInstanceOf(Undefined::class);
5050
});
5151

5252
it('null firstName produces Undefined via tryFromMixed while height succeeds', function (): void {
53-
$obj = NullableType::fromScalars(id: 1, firstName: null, height: 180);
53+
$obj = OptionalType::fromScalars(id: 1, firstName: null, height: 180);
5454
expect($obj->getFirstName())->toBeInstanceOf(Undefined::class)
5555
->and($obj->getHeight())->toBeInstanceOf(FloatPositive::class);
5656
});
5757

5858
it('invalid id throws IntegerTypeException with exact message', function (): void {
59-
expect(fn() => NullableType::fromScalars(id: 0, firstName: 'Name', height: 100))
59+
expect(fn() => OptionalType::fromScalars(id: 0, firstName: 'Name', height: 100))
6060
->toThrow(IntegerTypeException::class, 'Expected positive integer, got "0"');
6161
});
6262

6363
it('non-numeric height string throws FloatTypeException from assertFloatString', function (): void {
64-
expect(fn() => NullableType::fromScalars(id: 1, firstName: 'Name', height: 'abc'))
64+
expect(fn() => OptionalType::fromScalars(id: 1, firstName: 'Name', height: 'abc'))
6565
->toThrow(FloatTypeException::class, 'String "abc" has no valid float value');
6666
});

0 commit comments

Comments
 (0)