diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 543b92d..f9a24c4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,6 +12,7 @@ timeoutForMediumTests="10" timeoutForLargeTests="60" displayDetailsOnPhpunitDeprecations="true" + displayDetailsOnTestsThatTriggerWarnings="true" > diff --git a/src/Boolean/Base/BooleanValueBase.php b/src/Boolean/Base/BooleanValueBase.php index 92b78f8..ed6bac0 100644 --- a/src/Boolean/Base/BooleanValueBase.php +++ b/src/Boolean/Base/BooleanValueBase.php @@ -9,6 +9,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; /** * 真偽値の値オブジェクトの基底クラス @@ -17,7 +18,7 @@ { protected function __construct(public bool $value) { - assert(static::isValid($value)->isOk()); + Utils::assertResultIsOk(static::isValid($value)); } #[Override] diff --git a/src/Collection/ArrayList.php b/src/Collection/ArrayList.php index 8b11844..857bdeb 100644 --- a/src/Collection/ArrayList.php +++ b/src/Collection/ArrayList.php @@ -122,15 +122,15 @@ final public static function tryFromResults(iterable $results): Result { $elements = is_array($results) ? $results : iterator_to_array($results); - $elementsResult = Result\combineWithErrorValue(...$elements); + $elementsResult = Result\combine(...$elements); if ($elementsResult->isErr()) { $flattenErrs = []; foreach ($elementsResult->unwrapErr() as $err) { - if ($err instanceof IErrorValue) { // @phpstan-ignore-line + if ($err instanceof IErrorValue) { $flattenErrs[] = $err; } elseif (is_array($err)) { foreach ($err as $e) { - if ($e instanceof IErrorValue) { // @phpstan-ignore-line + if ($e instanceof IErrorValue) { $flattenErrs[] = $e; } else { throw new LogicException( diff --git a/src/Collection/Base/CollectionBase.php b/src/Collection/Base/CollectionBase.php index bdef9a6..a2f53c2 100644 --- a/src/Collection/Base/CollectionBase.php +++ b/src/Collection/Base/CollectionBase.php @@ -9,6 +9,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use function count; @@ -27,8 +28,8 @@ */ protected function __construct(protected array $elements) { - assert(static::isValid($elements)->isOk()); - assert(static::isValidCount($elements)->isOk()); + Utils::assertResultIsOk(static::isValid($elements)); + Utils::assertResultIsOk(static::isValidCount($elements)); } #[Override] diff --git a/src/DateTime/LocalDate.php b/src/DateTime/LocalDate.php index 6771c94..3ae367e 100644 --- a/src/DateTime/LocalDate.php +++ b/src/DateTime/LocalDate.php @@ -13,6 +13,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use WizDevelop\PhpValueObject\ValueObjectMeta; /** @@ -57,11 +58,11 @@ final private function __construct( private int $day ) { // NOTE: 不変条件(invariant) - assert(static::isValid($year, $month, $day)->isOk()); - assert(static::isValidYear($year)->isOk()); - assert(static::isValidMonth($month)->isOk()); - assert(static::isValidDay($day)->isOk()); - assert(static::isValidDate($year, $month, $day)->isOk()); + Utils::assertResultIsOk(static::isValid($year, $month, $day)); + Utils::assertResultIsOk(static::isValidYear($year)); + Utils::assertResultIsOk(static::isValidMonth($month)); + Utils::assertResultIsOk(static::isValidDay($day)); + Utils::assertResultIsOk(static::isValidDate($year, $month, $day)); } // ------------------------------------------------------------------------- diff --git a/src/DateTime/LocalDateRange.php b/src/DateTime/LocalDateRange.php index 2df6649..31484bb 100644 --- a/src/DateTime/LocalDateRange.php +++ b/src/DateTime/LocalDateRange.php @@ -12,6 +12,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; /** * @template TStart of LocalDate @@ -42,8 +43,8 @@ final private function __construct( private mixed $to, ) { // NOTE: 不変条件(invariant) - assert(static::isValid($from, $to)->isOk()); - assert(static::isValidRange($from, $to)->isOk()); + Utils::assertResultIsOk(static::isValid($from, $to)); + Utils::assertResultIsOk(static::isValidRange($from, $to)); } // ------------------------------------------------------------------------- diff --git a/src/DateTime/LocalDateTime.php b/src/DateTime/LocalDateTime.php index 6e8a31e..d390a75 100644 --- a/src/DateTime/LocalDateTime.php +++ b/src/DateTime/LocalDateTime.php @@ -13,6 +13,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use WizDevelop\PhpValueObject\ValueObjectMeta; /** @@ -38,7 +39,7 @@ final private function __construct( private LocalTime $time, ) { // NOTE: 不変条件(invariant) - assert(static::isValid($date, $time)->isOk()); + Utils::assertResultIsOk(static::isValid($date, $time)); } // ------------------------------------------------------------------------- diff --git a/src/DateTime/LocalDateTimeRange.php b/src/DateTime/LocalDateTimeRange.php index 4fa084d..7abc013 100644 --- a/src/DateTime/LocalDateTimeRange.php +++ b/src/DateTime/LocalDateTimeRange.php @@ -10,6 +10,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use WizDevelop\PhpValueObject\ValueObjectMeta; /** @@ -37,7 +38,7 @@ final private function __construct( private RangeType $rangeType ) { // NOTE: 不変条件(invariant) - assert(static::isValid($from, $to)->isOk()); + Utils::assertResultIsOk(static::isValid($from, $to)); } // ------------------------------------------------------------------------- diff --git a/src/DateTime/LocalTime.php b/src/DateTime/LocalTime.php index a41e100..af77b07 100644 --- a/src/DateTime/LocalTime.php +++ b/src/DateTime/LocalTime.php @@ -13,6 +13,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use WizDevelop\PhpValueObject\ValueObjectMeta; /** @@ -50,11 +51,11 @@ final private function __construct( private int $micro ) { // NOTE: 不変条件(invariant) - assert(static::isValid($hour, $minute, $second, $micro)->isOk()); - assert(static::isValidHour($hour)->isOk()); - assert(static::isValidMinute($minute)->isOk()); - assert(static::isValidSecond($second)->isOk()); - assert(static::isValidMicro($micro)->isOk()); + Utils::assertResultIsOk(static::isValid($hour, $minute, $second, $micro)); + Utils::assertResultIsOk(static::isValidHour($hour)); + Utils::assertResultIsOk(static::isValidMinute($minute)); + Utils::assertResultIsOk(static::isValidSecond($second)); + Utils::assertResultIsOk(static::isValidMicro($micro)); } // ------------------------------------------------------------------------- @@ -586,7 +587,7 @@ private static function extractTime(DateTimeInterface $value): array $second = (int)$value->format('s'); /** @var Micro */ - $micro = (int)$value->format('u'); // @phpstan-ignore varTag.type + $micro = (int)$value->format('u'); return [$hour, $minute, $second, $micro]; } diff --git a/src/Number/Decimal/DecimalValueBase.php b/src/Number/Decimal/DecimalValueBase.php index ce6c440..80bcbc4 100644 --- a/src/Number/Decimal/DecimalValueBase.php +++ b/src/Number/Decimal/DecimalValueBase.php @@ -10,6 +10,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; /** * 少数の値オブジェクトの基底クラス @@ -26,11 +27,9 @@ protected function __construct(public Number $value) { // NOTE: 不変条件(invariant) assert(static::min() <= static::max()); - // assert(static::min()->scale <= static::scale()); - // assert(static::max()->scale <= static::scale()); - assert(static::isValidRange($value)->isOk()); - assert(static::isValidDigits($value)->isOk()); - assert(static::isValid($value)->isOk()); + Utils::assertResultIsOk(static::isValidRange($value)); + Utils::assertResultIsOk(static::isValidDigits($value)); + Utils::assertResultIsOk(static::isValid($value)); } #[Override] diff --git a/src/Number/Integer/IntegerValueBase.php b/src/Number/Integer/IntegerValueBase.php index e2e198e..b1b16b4 100644 --- a/src/Number/Integer/IntegerValueBase.php +++ b/src/Number/Integer/IntegerValueBase.php @@ -9,6 +9,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; /** * 整数の値オブジェクトの基底クラス @@ -25,8 +26,8 @@ protected function __construct(public int $value) { // NOTE: 不変条件(invariant) assert(static::min() <= static::max()); - assert(static::isValidRange($value)->isOk()); - assert(static::isValid($value)->isOk()); + Utils::assertResultIsOk(static::isValidRange($value)); + Utils::assertResultIsOk(static::isValid($value)); } #[Override] diff --git a/src/String/Base/StringValueBase.php b/src/String/Base/StringValueBase.php index e01edd4..b659d47 100644 --- a/src/String/Base/StringValueBase.php +++ b/src/String/Base/StringValueBase.php @@ -9,6 +9,7 @@ use WizDevelop\PhpMonad\Result; use WizDevelop\PhpValueObject\Error\ValueObjectError; use WizDevelop\PhpValueObject\IValueObject; +use WizDevelop\PhpValueObject\Utils; use function assert; @@ -25,9 +26,9 @@ protected function __construct(public string $value) { // NOTE: 不変条件(invariant) assert(static::minLength() <= static::maxLength()); - assert(static::isValid($value)->isOk()); - assert(static::isValidLength($value)->isOk()); - assert(static::isValidRegex($value)->isOk()); + Utils::assertResultIsOk(static::isValid($value)); + Utils::assertResultIsOk(static::isValidLength($value)); + Utils::assertResultIsOk(static::isValidRegex($value)); } #[Override] diff --git a/src/Utils.php b/src/Utils.php new file mode 100644 index 0000000..484e73c --- /dev/null +++ b/src/Utils.php @@ -0,0 +1,25 @@ + $result + */ + public static function assertResultIsOk(Result $result): void + { + assert($result->isOk(), $result->mapOrElse( + static fn () => 'Ok', + static fn ($error) => "error_code: {$error->getCode()}, message: {$error->getMessage()}, details: " . json_encode($error->getDetails(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) + )); + } +}