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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ use PhPhD\ExceptionalMatcher\Rule\Object\Property\Catch_;
#[Try_]
class RegisterUserCommand
{
#[Catch_(LoginAlreadyTakenException::class, 'auth.login.already_taken')]
#[Catch_(LoginAlreadyTakenException::class, message: 'auth.login.already_taken')]
public string $login;

#[Catch_(WeakPasswordException::class, 'auth.password.weak')]
#[Catch_(WeakPasswordException::class, message: 'auth.password.weak')]
public string $password;
}
```
Expand Down Expand Up @@ -181,11 +181,11 @@ A comparison of the approaches would look something like this:
class RegisterUserCommand
{
- #[App\Assert\UniqueLogin]
+ #[Catch_(LoginAlreadyTakenException::class, 'auth.login.already_taken')]
+ #[Catch_(LoginAlreadyTakenException::class, message: 'auth.login.already_taken')]
public string $login;

- #[Assert\PasswordStrength(minScore: 2)]
+ #[Catch_(WeakPasswordException::class, 'auth.password.weak')]
+ #[Catch_(WeakPasswordException::class, message: 'auth.password.weak')]
public string $password;
}
```
Expand Down
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ for the changes not covered by automatic upgrade via Rector (see the "Upgrading"

* Renamed: `PhPhD\ExceptionalMatcher\Rule\Object\Property\Catch_::$formatter` \
was renamed into `$format`.

* Parameter Moved: `PhPhD\ExceptionalMatcher\Rule\Object\Property\Catch_::$message` \
was moved to be after `$format`.

Not moving it will cause this error:
> Parameter #2 `$from` of attribute class `PhPhD\ExceptionalMatcher\Rule\Object\Property\Catch_` constructor expects `array{class-string, non-empty-string}|class-string|null`,
> `'exception.message'` given.

Fix it by passing it as a named parameter: `message: 'exception.message'`.
12 changes: 6 additions & 6 deletions src/ExceptionalMatcher/Rule/Object/Property/Catch_.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ final class Catch_
public function __construct(
/** @var class-string<T1&T2> */
private readonly string $exception,
private readonly ?string $message = null,
/** @var null|class-string|array{class-string,non-empty-string} The origin of the exception */
private readonly array|string|null $from = null,
/** @note match condition class is contravariant to the exception */
Expand All @@ -42,6 +41,7 @@ public function __construct(
private readonly ?array $if = null,
/** @note formatter class is contravariant to the exception */
private readonly string $format = MainExceptionViolationFormatter::class, // @phpstan-ignore phpat.testModelDependencies (really, this's a fair catch - it should not depend on the formatter)
private readonly ?string $message = null,
) {
if (null !== $this->if) {
Assert::count($this->if, 2);
Expand All @@ -58,11 +58,6 @@ public function getExceptionClass(): string
return $this->exception;
}

public function getMessage(): ?string
{
return $this->message;
}

/** @return ?array{class-string,?non-empty-string} */
public function getFrom(): ?array
{
Expand Down Expand Up @@ -98,4 +93,9 @@ public function getFormat(): string
{
return $this->format;
}

public function getMessage(): ?string
{
return $this->message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function assembleRules(MatchConditionFactory $matchConditionFactory): ?Co
$rules->append(new MatchExceptionRule(
$ruleSet,
$condition,
$catchAttribute->getMessage(),
$catchAttribute->getFormat(),
$catchAttribute->getMessage(),
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#[Try_]
final class ConditionalMessage
{
#[Catch_(ConditionallyCaughtException::class, 'oops', if: [self::class, 'firstPropertyMatchesException'])]
#[Catch_(ConditionallyCaughtException::class, if: [self::class, 'firstPropertyMatchesException'], message: 'oops')]
private int $firstProperty;

#[Catch_(ConditionallyCaughtException::class, 'oops', if: [self::class, 'secondPropertyMatchesException'])]
#[Catch_(ConditionallyCaughtException::class, if: [self::class, 'secondPropertyMatchesException'], message: 'oops')]
private int $secondProperty;

public static function createWithConditionalProperties(int $firstConditionalProperty, int $secondConditionalProperty): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function __construct(
private readonly MatchingRule $parent,
/** @var MatchCondition<TException> */
private readonly MatchCondition $condition,
private readonly ?string $messageTemplate,
/** @var class-string<MatchedExceptionFormatter<TException,mixed>> */
private readonly string $formatterId,
private readonly ?string $messageTemplate,
) {
}

Expand Down Expand Up @@ -66,14 +66,14 @@ public function matchesException(Throwable $exception): bool
return $this->condition->matches($exception);
}

public function getMessageTemplate(): ?string
{
return $this->messageTemplate;
}

/** @return class-string<MatchedExceptionFormatter<TException,mixed>> */
public function getFormatterId(): string
{
return $this->formatterId;
}

public function getMessageTemplate(): ?string
{
return $this->messageTemplate;
}
}
2 changes: 0 additions & 2 deletions tests/ArchitectureRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Uid\Exception\InvalidArgumentException as InvalidUidException;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand Down Expand Up @@ -131,7 +130,6 @@ public function layers(): array
'deps' => [
$this->model(),
$this->matchCondition(),
Selector::classname(Valid::class),
Selector::classname(Assert::class),
],
],
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Stub/HandleableMessageStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
#[Try_]
final class HandleableMessageStub
{
#[Catch_(AnException::class, 'oops')]
#[Catch_(AnException::class, message: 'oops')]
private int $property;

#[Catch_(CustomFormattedException::class, 'oops', format: CustomExceptionViolationFormatter::class)]
#[Catch_(CustomFormattedException::class, format: CustomExceptionViolationFormatter::class, message: 'oops')]
private string $formatted;

#[Catch_(ObjectPropertyMatchedException::class, 'oops')]
#[Catch_(ObjectPropertyMatchedException::class, message: 'oops')]
private object $objectProperty;

#[Catch_(StaticPropertyMatchedException::class, 'oops')]
#[Catch_(StaticPropertyMatchedException::class, message: 'oops')]
private static string $staticProperty = 'foo';

private NestedHandleableMessage $nestedObject;
Expand All @@ -51,24 +51,24 @@ final class HandleableMessageStub
#[Catch_(InvalidArgumentException::class, from: [Uuid::class, 'fromString'])]
private string $uid;

#[Catch_(LogicException::class, 'oops')]
#[Catch_(LogicException::class, message: 'oops')]
private string $messageText;

#[Catch_(SomeValueException::class, 'oops', match: ExceptionValueMatchCondition::class)]
#[Catch_(SomeValueException::class, match: ExceptionValueMatchCondition::class, message: 'oops')]
#[Catch_(ValidationFailedException::class, match: ValidationFailedExceptionMatchCondition::class, format: ValidationFailedExceptionFormatter::class)]
private string $notMatchedProperty = 'not matched';

#[Catch_(SomeValueException::class, 'oops', match: ExceptionValueMatchCondition::class)]
#[Catch_(SomeValueException::class, match: ExceptionValueMatchCondition::class, message: 'oops')]
#[Catch_(ValidationFailedException::class, match: ValidationFailedExceptionMatchCondition::class, format: ValidationFailedExceptionFormatter::class)]
private string $matchedProperty = 'matched!';

#[Catch_(SomeValueException::class, 'oops')]
#[Catch_(SomeValueException::class, message: 'oops')]
private string $anotherMatchedAsNoCondition;

#[Catch_(MessageContainingException::class)]
private int $fallBackToExceptionMessage;

#[Catch_(MessageContainingException::class, '')]
#[Catch_(MessageContainingException::class, message: '')]
private string $emptyTranslationMessage;

private function __construct()
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Stub/NestedHandleableMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#[Try_]
final class NestedHandleableMessage
{
#[Catch_(NestedPropertyMatchedException::class, 'nested.message')]
#[Catch_(NestedPropertyMatchedException::class, message: 'nested.message')]
private string $nestedProperty;

#[Valid]
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Stub/NestedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class NestedItem
{
public function __construct(
#[Catch_(NestedItemMatchedException::class, 'oops', if: [self::class, 'matchesValue'])]
#[Catch_(NestedItemMatchedException::class, if: [self::class, 'matchesValue'], message: 'oops')]
private readonly int $property,
) {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Stub/NotHandleableMessageStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class NotHandleableMessageStub
{
public function __construct(
#[Catch_(AnException::class, 'not matched')]
#[Catch_(AnException::class, message: 'not matched')]
private readonly int $property,
) {
}
Expand Down
Loading