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
54 changes: 24 additions & 30 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,6 @@ parameters:
count: 1
path: src/Validator/Constraints/Email.php

-
message: '#^Method AssoConnect\\ValidatorBundle\\Validator\\Constraints\\Email\:\:__construct\(\) has parameter \$options with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: src/Validator/Constraints/Email.php

-
message: '#^Public property `checkDNS` not marked as readonly\.$#'
identifier: shipmonk.publicPropertyNotReadonly
Expand All @@ -582,18 +576,6 @@ parameters:
count: 1
path: src/Validator/Constraints/EmployerIdentificationNumber.php

-
message: '#^Class AssoConnect\\ValidatorBundle\\Validator\\Constraints\\FloatScale has an uninitialized property \$scale\. Give it default value or assign it in the constructor\.$#'
identifier: property.uninitialized
count: 1
path: src/Validator/Constraints/FloatScale.php

-
message: '#^Method getRequiredOptions always return list, but is marked as array\<string\>$#'
identifier: shipmonk.returnListNotUsed
count: 1
path: src/Validator/Constraints/FloatScale.php

-
message: '#^Public property `message` not marked as readonly\.$#'
identifier: shipmonk.publicPropertyNotReadonly
Expand Down Expand Up @@ -636,6 +618,30 @@ parameters:
count: 1
path: src/Validator/Constraints/LastDigitsUsSocialSecurityNumber.php

-
message: '#^Parameter \#1 \$min of method AssoConnect\\ValidatorBundle\\Validator\\Constraints\\Money\:\:__construct\(\) cannot have float as its type \- floats are not allowed\.$#'
identifier: float.type
count: 1
path: src/Validator/Constraints/Money.php

-
message: '#^Parameter \#2 \$max of method AssoConnect\\ValidatorBundle\\Validator\\Constraints\\Money\:\:__construct\(\) cannot have float as its type \- floats are not allowed\.$#'
identifier: float.type
count: 1
path: src/Validator/Constraints/Money.php

-
message: '#^Cannot assign float to \$this\->min \- floats are not allowed\.$#'
identifier: float.assign
count: 1
path: src/Validator/Constraints/Money.php

-
message: '#^Cannot assign float to \$this\->max \- floats are not allowed\.$#'
identifier: float.assign
count: 1
path: src/Validator/Constraints/Money.php

-
message: '#^Property AssoConnect\\ValidatorBundle\\Validator\\Constraints\\Money\:\:\$max cannot have float as its type \- floats are not allowed\.$#'
identifier: float.property
Expand Down Expand Up @@ -714,18 +720,6 @@ parameters:
count: 1
path: src/Validator/Constraints/PhoneMobile.php

-
message: '#^Class AssoConnect\\ValidatorBundle\\Validator\\Constraints\\Postal has an uninitialized property \$countryPropertyPath\. Give it default value or assign it in the constructor\.$#'
identifier: property.uninitialized
count: 1
path: src/Validator/Constraints/Postal.php

-
message: '#^Method getRequiredOptions always return list, but is marked as array\<string\>$#'
identifier: shipmonk.returnListNotUsed
count: 1
path: src/Validator/Constraints/Postal.php

-
message: '#^Public property `countryPropertyPath` not marked as readonly\.$#'
identifier: shipmonk.publicPropertyNotReadonly
Expand Down
21 changes: 19 additions & 2 deletions src/Validator/Constraints/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,32 @@

public bool $checkDNS = false;

public function __construct(
?array $options = null,
?string $message = null,
?string $mode = null,
?callable $normalizer = null,
?array $groups = null,
mixed $payload = null,
?string $tldMessage = null,
?string $dnsMessage = null,
?bool $checkDNS = null,
) {

Check warning on line 32 in src/Validator/Constraints/Email.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 8 parameters, which is greater than the 7 authorized.

See more on https://sonarcloud.io/project/issues?id=assoconnect_validator-bundle&issues=AZz9iZ34bxTFOK82p332&open=AZz9iZ34bxTFOK82p332&pullRequest=74
parent::__construct($options, $message, $mode, $normalizer, $groups, $payload);
parent::__construct(
message: $message,
mode: $mode,
normalizer: $normalizer,
groups: $groups,
payload: $payload,
);
$this->mode ??= 'strict';
if (null !== $tldMessage) {
$this->tldMessage = $tldMessage;
}
if (null !== $dnsMessage) {
$this->dnsMessage = $dnsMessage;
}
if (null !== $checkDNS) {
$this->checkDNS = $checkDNS;
}
}
}
2 changes: 1 addition & 1 deletion src/Validator/Constraints/EntityValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getConstraints(string $class, string $field): array
}
} elseif (($fieldMapping['type'] & ClassMetadataInfo::TO_MANY) !== 0) {
// ToMany
$constraints[] = new All([
$constraints[] = new All(constraints: [
new Type($fieldMapping['targetEntity']),
]);
} else {
Expand Down
26 changes: 3 additions & 23 deletions src/Validator/Constraints/FloatScale.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,12 @@ class FloatScale extends Constraint

public int $scale;

/**
* @param int|array<mixed> $scale
* @param array<mixed> $options
*/
public function __construct(
$scale,
int $scale,
?array $groups = null,
mixed $payload = null,
array $options = []
) {
if (\is_array($scale)) {
$options = array_merge($scale, $options);
} elseif (null !== $scale) {
$options['value'] = $scale;
}

parent::__construct($options, $groups, $payload);
}

public function getDefaultOption(): string
{
return 'scale';
}

public function getRequiredOptions(): array
{
return ['scale'];
$this->scale = $scale;
parent::__construct(groups: $groups, payload: $payload);
}
}
11 changes: 11 additions & 0 deletions src/Validator/Constraints/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ class Money extends Constraint

public float $min = 0.0;
public float $max = self::MAX;

public function __construct(
float $min = 0.0,
float $max = self::MAX,
?array $groups = null,
mixed $payload = null,
) {
parent::__construct(groups: $groups, payload: $payload);
$this->min = $min;
$this->max = $max;
}
}
26 changes: 3 additions & 23 deletions src/Validator/Constraints/Postal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,13 @@ class Postal extends Constraint

public string $countryPropertyPath;

/**
* @param string|array<mixed> $countryPropertyPath
* @param array<mixed> $options
*/
public function __construct(
$countryPropertyPath,
string $countryPropertyPath,
?array $groups = null,
mixed $payload = null,
array $options = []
) {
if (\is_array($countryPropertyPath)) {
$options = array_merge($countryPropertyPath, $options);
} elseif (null !== $countryPropertyPath) {
$options['value'] = $countryPropertyPath;
}

parent::__construct($options, $groups, $payload);
}

public function getDefaultOption(): string
{
return 'countryPropertyPath';
}

public function getRequiredOptions(): array
{
return ['countryPropertyPath'];
$this->countryPropertyPath = $countryPropertyPath;
parent::__construct(groups: $groups, payload: $payload);
}

public string $message = 'The value {{ value }} is not a valid postal code.';
Expand Down
6 changes: 5 additions & 1 deletion src/Validator/ConstraintsSetProvider/Field/EmailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AssoConnect\DoctrineTypesBundle\Doctrine\DBAL\Types\EmailType;
use AssoConnect\ValidatorBundle\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Webmozart\Assert\Assert;

class EmailProvider implements FieldConstraintsSetProviderInterface
{
Expand All @@ -17,9 +18,12 @@ public function supports(string $type): bool

public function getConstraints(array $fieldMapping): array
{
$length = $fieldMapping['length'] ?? 255;
Assert::positiveInteger($length);

return [
new Email(),
new Length(['max' => ($fieldMapping['length'] ?? 255)]),
new Length(max: $length),
];
}
}
2 changes: 1 addition & 1 deletion src/Validator/ConstraintsSetProvider/Field/IpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function supports(string $type): bool
public function getConstraints(array $fieldMapping): array
{
return [
new Ip(['version' => 'all']),
new Ip(version: 'all'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function supports(string $type): bool
public function getConstraints(array $fieldMapping): array
{
return [
new Locale(['canonicalize' => true]),
new Locale(canonicalize: true),
];
}
}
8 changes: 6 additions & 2 deletions src/Validator/ConstraintsSetProvider/Field/StringProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Types\Types;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Webmozart\Assert\Assert;

class StringProvider implements FieldConstraintsSetProviderInterface
{
Expand All @@ -17,10 +18,13 @@ public function supports(string $type): bool

public function getConstraints(array $fieldMapping): array
{
$constraints = [new Length(['max' => $fieldMapping['length'] ?? 255])];
$length = $fieldMapping['length'] ?? 255;
Assert::positiveInteger($length);

$constraints = [new Length(max: $length)];

if (isset($fieldMapping['nullable']) && true === $fieldMapping['nullable']) {
$constraints[] = new NotBlank(['allowNull' => true]);
$constraints[] = new NotBlank(allowNull: true);
}

return $constraints;
Expand Down
9 changes: 5 additions & 4 deletions src/Validator/ConstraintsSetProvider/Field/TextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Types\Types;
use Symfony\Component\Validator\Constraints\Length;
use Webmozart\Assert\Assert;

class TextProvider implements FieldConstraintsSetProviderInterface
{
Expand All @@ -16,11 +17,11 @@ public function supports(string $type): bool

public function getConstraints(array $fieldMapping): array
{
$length = $fieldMapping['length'] ?? 65535;
Assert::positiveInteger($length);

return [
new Length([
'max' => $fieldMapping['length'] ?? 65535,
'charset' => '8bit',
]),
new Length(max: $length, charset: '8bit'),
];
}
}
24 changes: 12 additions & 12 deletions tests/Validator/Constraints/EmailValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function createValidator(): ConstraintValidator

protected function getConstraint(): Constraint
{
return new Email([
'message' => 'myMessage',
'tldMessage' => 'myTldMessage',
'dnsMessage' => 'myDnsMessage',
'checkDNS' => true,
]);
return new Email(
message: 'myMessage',
tldMessage: 'myTldMessage',
dnsMessage: 'myDnsMessage',
checkDNS: true,
);
}

public function testValidateUnknownConstraint(): void
Expand Down Expand Up @@ -137,12 +137,12 @@ public static function providerValidValues(): iterable

public function testDisabledCheckDns(): void
{
$this->validator->validate('john.doe@xn--gmail-9fa.com', new Email([
'message' => 'myMessage',
'tldMessage' => 'myTldMessage',
'dnsMessage' => 'myDnsMessage',
'checkDNS' => false,
]));
$this->validator->validate('john.doe@xn--gmail-9fa.com', new Email(
message: 'myMessage',
tldMessage: 'myTldMessage',
dnsMessage: 'myDnsMessage',
checkDNS: false,
));

self::assertNoViolation();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Validator/Constraints/EntityValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function testGetConstraintsForRelationToMany(): void
$constraints = $this->validator->getConstraints('class', 'owningToMany');
self::assertArrayContainsSameObjects(
$constraints,
[new All([new Type(MyEntityParent::class)])]
[new All(constraints: [new Type(MyEntityParent::class)])]
);
self::assertInstanceOf(All::class, $constraints[0]);
self::assertIsArray($constraints[0]->constraints);
Expand Down
2 changes: 1 addition & 1 deletion tests/Validator/Constraints/FloatScaleValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FloatScaleValidatorTest extends ConstraintValidatorTestCase
{
protected function getConstraint(): Constraint
{
return new FloatScale(['scale' => 2]);
return new FloatScale(scale: 2);
}

public function createValidator(): ConstraintValidator
Expand Down
2 changes: 1 addition & 1 deletion tests/Validator/Constraints/MoneyValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MoneyValidatorTest extends ConstraintValidatorTestCase
{
protected function getConstraint(): Constraint
{
return new Money(['min' => 0.0, 'max' => 90.0]);
return new Money(min: 0.0, max: 90.0);
}

public function createValidator(): ConstraintValidator
Expand Down
11 changes: 1 addition & 10 deletions tests/Validator/Constraints/PostalValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\MissingOptionsException;

/**
* @extends ConstraintValidatorTestCase<PostalValidator>
Expand All @@ -19,22 +18,14 @@ class PostalValidatorTest extends ConstraintValidatorTestCase
{
protected function getConstraint(): Constraint
{
return new Postal([
'countryPropertyPath' => 'country',
]);
return new Postal(countryPropertyPath: 'country');
}

public function createValidator(): ConstraintValidator
{
return new PostalValidator();
}

public function testMissingPropertyPath(): void
{
$this->expectException(MissingOptionsException::class);
new Postal([]);
}

public function testMissingObject(): void
{
$this->setObject(null);
Expand Down
Loading
Loading