Skip to content

[Validator] Add missing types and return types #18570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2023
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
18 changes: 9 additions & 9 deletions reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Configuration
class Author
{
#[Assert\Callback]
public function validate(ExecutionContextInterface $context, $payload): void
public function validate(ExecutionContextInterface $context, mixed $payload): void
{
// ...
}
Expand Down Expand Up @@ -80,7 +80,7 @@ Configuration
$metadata->addConstraint(new Assert\Callback('validate'));
}

public function validate(ExecutionContextInterface $context, $payload): void
public function validate(ExecutionContextInterface $context, mixed $payload): void
{
// ...
}
Expand All @@ -101,7 +101,7 @@ field those errors should be attributed::
// ...
private string $firstName;

public function validate(ExecutionContextInterface $context, $payload)
public function validate(ExecutionContextInterface $context, mixed $payload): void
{
// somehow you have an array of "fake names"
$fakeNames = [/* ... */];
Expand All @@ -121,13 +121,13 @@ Static Callbacks
You can also use the constraint with static methods. Since static methods don't
have access to the object instance, they receive the object as the first argument::

public static function validate($object, ExecutionContextInterface $context, $payload)
public static function validate(mixed $value, ExecutionContextInterface $context, mixed $payload): void
{
// somehow you have an array of "fake names"
$fakeNames = [/* ... */];

// check if the name is actually a fake name
if (in_array($object->getFirstName(), $fakeNames)) {
if (in_array($value->getFirstName(), $fakeNames)) {
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation()
Expand All @@ -149,7 +149,7 @@ Suppose your validation function is ``Acme\Validator::validate()``::

class Validator
{
public static function validate($object, ExecutionContextInterface $context, $payload)
public static function validate(mixed $value, ExecutionContextInterface $context, mixed $payload): void
{
// ...
}
Expand Down Expand Up @@ -206,7 +206,7 @@ You can then use the following configuration to invoke this validator:

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Assert\Callback([
Validator::class,
Expand Down Expand Up @@ -235,9 +235,9 @@ constructor of the Callback constraint::

class Author
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$callback = function ($object, ExecutionContextInterface $context, $payload): void {
$callback = function (mixed $value, ExecutionContextInterface $context, mixed $payload): void {
// ...
};

Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/DisableAutoMapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ metadata:
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Assert\DisableAutoMapping());
}
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/EnableAutoMapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ metadata:
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Assert\EnableAutoMapping());
}
Expand Down
2 changes: 1 addition & 1 deletion validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The validator class only has one required method ``validate()``::

class ContainsAlphanumericValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint): void
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof ContainsAlphanumeric) {
throw new UnexpectedTypeException($constraint, ContainsAlphanumeric::class);
Expand Down