Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/Behavior/EnumCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Convert backed-enum database values to and from PHP enum instances
*
* The enum's backing value (string|int) is stored in the database. On retrieval the raw scalar
* is converted to the corresponding enum case via {@see BackedEnum::from()}; on persistence the
* is converted to the corresponding enum case via {@see BackedEnum::tryFrom()}; on persistence the
* enum instance is unwrapped back to its backing value. Null is preserved as null in both directions.
*
* ```php
Expand Down Expand Up @@ -47,24 +47,14 @@
* @param int|string|null $value
*
* @return ?BackedEnum
*
* @throws ValueConversionException
*/
public function fromDb($value, $key, $_)
{
if ($value === null || $value instanceof $this->enum) {

Check failure on line 53 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Instanceof between int|string and class-string<BackedEnum> will always evaluate to false.

Check failure on line 53 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Instanceof between int|string and class-string<BackedEnum> will always evaluate to false.

Check failure on line 53 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Instanceof between int|string and class-string<BackedEnum> will always evaluate to false.

Check failure on line 53 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Instanceof between int|string and class-string<BackedEnum> will always evaluate to false.
return $value;
}

try {
return $this->enum::from($value);
} catch (ValueError $e) {
throw new ValueConversionException(sprintf(
"%s (key: '%s')",
$e->getMessage(),
$key
));
}
return $this->enum::tryFrom($value);
}

/**
Expand All @@ -85,7 +75,7 @@
}

try {
return $this->enum::from($value)->value;

Check failure on line 78 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Parameter #1 $value of static method BackedEnum::from() expects int|string, BackedEnum|int|string given.

Check failure on line 78 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Parameter #1 $value of static method BackedEnum::from() expects int|string, BackedEnum|int|string given.

Check failure on line 78 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Parameter #1 $value of static method BackedEnum::from() expects int|string, BackedEnum|int|string given.

Check failure on line 78 in src/Behavior/EnumCast.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Parameter #1 $value of static method BackedEnum::from() expects int|string, BackedEnum|int|string given.
} catch (ValueError $e) {
throw new ValueConversionException(sprintf(
"%s (key: '%s')",
Expand Down
4 changes: 1 addition & 3 deletions tests/Behavior/EnumCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public function testToDbPassesThroughNull(): void

public function testFromDbThrowsForUnknownBackingValue(): void
{
$this->expectException(ValueConversionException::class);

$this->behavior->fromDb('purple', 'color', null);
$this->assertNull($this->behavior->fromDb('purple', 'color', null));
}

public function testFromDbIsIdempotentWhenValueIsAlreadyAnEnumInstance(): void
Expand Down
Loading