Skip to content

Commit

Permalink
Add own assertions with better exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 6, 2024
1 parent 196d943 commit 75dd942
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/DataTransferObject/AbstractDataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Setono\PeakWMS\DataTransferObject;

use Webmozart\Assert\Assert;

abstract class AbstractDataTransferObject implements \JsonSerializable
{
public function jsonSerialize(): array
Expand Down Expand Up @@ -44,10 +42,12 @@ protected static function convertDateTime(null|\DateTimeImmutable|string $value)
return $value;
}

$value = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $value);
Assert::notFalse($value);
$res = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $value);
if (false === $res) {

Check warning on line 46 in src/DataTransferObject/AbstractDataTransferObject.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ return $value; } $res = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $value); - if (false === $res) { + if (true === $res) { throw new \InvalidArgumentException(sprintf('The value %s is not a valid date time string based on the format %s', $value, \DATE_ATOM)); } return $res;
throw new \InvalidArgumentException(sprintf('The value %s is not a valid date time string based on the format %s', $value, \DATE_ATOM));
}

return $value;
return $res;
}

/**
Expand All @@ -67,7 +67,9 @@ protected static function convertEnum(null|int|string|\BackedEnum $value, string
}

if ($value instanceof \BackedEnum) {

Check warning on line 69 in src/DataTransferObject/AbstractDataTransferObject.php

View workflow job for this annotation

GitHub Actions / Mutation tests (8.3, highest)

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ if (null === $value) { return null; } - if ($value instanceof \BackedEnum) { + if (false) { if (!is_a($value, $enumClass)) { throw new \InvalidArgumentException(sprintf('The value %s is not an instance of the enum class %s', $value::class, $enumClass)); }
Assert::isInstanceOf($value, $enumClass);
if (!is_a($value, $enumClass)) {
throw new \InvalidArgumentException(sprintf('The value %s is not an instance of the enum class %s', $value::class, $enumClass));
}

return $value;
}
Expand Down

0 comments on commit 75dd942

Please sign in to comment.