Skip to content

Commit c6d0a45

Browse files
committed
Refactor validation logic and exception handling across typed values
- Removed `Assert` utility and replaced with inline validation for enhanced clarity and maintainability. - Introduced type-specific exception classes (`IntegerTypeException`, `FloatTypeException`) for better error specificity. - Updated integer and float types to use strict parsing, improving validation accuracy. - Refactored tests to align with new validation logic and exception hierarchy. - Improved composer scripts and tools configuration, setting mutation testing minimum to 100%. - Upgraded `phpdocumentor/type-resolver` dependency to `v1.12.0`.
1 parent c8a5173 commit c6d0a45

29 files changed

+187
-329
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"./vendor/bin/pest --coverage --min=100"
6262
],
6363
"mutate": [
64-
"./vendor/bin/pest --mutate --parallel --everything --covered-only --min=40"
64+
"./vendor/bin/pest --mutate --parallel --everything --covered-only --min=100"
6565
]
6666
},
6767
"config": {

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Code/Assert/Assert.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/Code/Exception/NumericTypeException.php renamed to src/Code/Exception/FloatTypeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace PhpTypedValues\Code\Exception;
66

7-
class NumericTypeException extends TypeException
7+
class FloatTypeException extends TypeException
88
{
99
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Code\Exception;
6+
7+
class IntegerTypeException extends TypeException
8+
{
9+
}

src/Code/Float/FloatType.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44

55
namespace PhpTypedValues\Code\Float;
66

7-
use PhpTypedValues\Code\Assert\Assert;
8-
use PhpTypedValues\Code\Exception\NumericTypeException;
7+
use PhpTypedValues\Code\Exception\FloatTypeException;
8+
9+
use function sprintf;
910

1011
/**
1112
* @psalm-immutable
1213
*/
1314
abstract readonly class FloatType implements FloatTypeInterface
1415
{
1516
/**
16-
* @throws NumericTypeException
17+
* @throws FloatTypeException
1718
*/
18-
protected static function assertNumericString(string $value): void
19+
protected static function assertFloatString(string $value): void
1920
{
20-
Assert::numeric($value, 'String has no valid float');
21+
if (!is_numeric($value)) {
22+
throw new FloatTypeException(sprintf('String "%s" has no valid float value', $value));
23+
}
2124
}
2225

2326
public function toString(): string

src/Code/Integer/IntType.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44

55
namespace PhpTypedValues\Code\Integer;
66

7-
use PhpTypedValues\Code\Assert\Assert;
8-
use PhpTypedValues\Code\Exception\NumericTypeException;
7+
use PhpTypedValues\Code\Exception\IntegerTypeException;
8+
9+
use function sprintf;
910

1011
/**
1112
* @psalm-immutable
1213
*/
1314
abstract readonly class IntType implements IntTypeInterface
1415
{
1516
/**
16-
* @throws NumericTypeException
17+
* @throws IntegerTypeException
1718
*/
18-
protected static function assertNumericString(string $value): void
19+
protected static function assertIntegerString(string $value): void
1920
{
20-
Assert::integer($value, 'String has no valid integer');
21+
// Strict check, avoid unexpected string conversion
22+
$convertedValue = (string) ((int) $value);
23+
if ($value !== $convertedValue) {
24+
throw new IntegerTypeException(sprintf('String "%s" has no valid integer value', $value));
25+
}
2126
}
2227

2328
public function toString(): string

src/DateTime/DateTimeAtom.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
/**
2424
* @throws DateTimeTypeException
2525
*/
26-
public static function fromString(string $value): self
26+
public static function fromString(string $value): static
2727
{
28-
return new self(
29-
self::createFromFormat(
28+
return new static(
29+
static::createFromFormat(
3030
$value,
3131
static::FORMAT,
32-
new DateTimeZone(self::ZONE)
32+
new DateTimeZone(static::ZONE)
3333
)
3434
);
3535
}
3636

3737
public function toString(): string
3838
{
39-
return $this->value()->format(self::FORMAT);
39+
return $this->value()->format(static::FORMAT);
4040
}
4141

42-
public static function fromDateTime(DateTimeImmutable $value): self
42+
public static function fromDateTime(DateTimeImmutable $value): static
4343
{
44-
return new self($value);
44+
return new static($value);
4545
}
4646
}

src/DateTime/DateTimeRFC3339.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
/**
2424
* @throws DateTimeTypeException
2525
*/
26-
public static function fromString(string $value): self
26+
public static function fromString(string $value): static
2727
{
28-
return new self(
29-
self::createFromFormat(
28+
return new static(
29+
static::createFromFormat(
3030
$value,
3131
static::FORMAT,
32-
new DateTimeZone(self::ZONE)
32+
new DateTimeZone(static::ZONE)
3333
)
3434
);
3535
}
3636

3737
public function toString(): string
3838
{
39-
return $this->value()->format(self::FORMAT);
39+
return $this->value()->format(static::FORMAT);
4040
}
4141

42-
public static function fromDateTime(DateTimeImmutable $value): self
42+
public static function fromDateTime(DateTimeImmutable $value): static
4343
{
44-
return new self($value);
44+
return new static($value);
4545
}
4646
}

src/DateTime/DateTimeRFC3339Extended.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
/**
2424
* @throws DateTimeTypeException
2525
*/
26-
public static function fromString(string $value): self
26+
public static function fromString(string $value): static
2727
{
28-
return new self(
29-
self::createFromFormat(
28+
return new static(
29+
static::createFromFormat(
3030
$value,
3131
static::FORMAT,
32-
new DateTimeZone(self::ZONE)
32+
new DateTimeZone(static::ZONE)
3333
)
3434
);
3535
}
3636

3737
public function toString(): string
3838
{
39-
return $this->value()->format(self::FORMAT);
39+
return $this->value()->format(static::FORMAT);
4040
}
4141

42-
public static function fromDateTime(DateTimeImmutable $value): self
42+
public static function fromDateTime(DateTimeImmutable $value): static
4343
{
44-
return new self($value);
44+
return new static($value);
4545
}
4646
}

0 commit comments

Comments
 (0)