Skip to content

Commit

Permalink
Refactor some validators (#34262)
Browse files Browse the repository at this point in the history
- remove unneeded $context local variable
- remove usage of Oro\Bundle\OrganizationBundle\Entity\OrganizationAwareInterface in MultiCurrencyBundle validation constraints
- add assertion of a constraint type
- add strict types
  • Loading branch information
vsoroka authored Nov 10, 2022
1 parent 504136f commit 132da7d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
class AllValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof All) {
throw new UnexpectedTypeException($constraint, All::class);
Expand All @@ -42,8 +42,7 @@ public function validate($value, Constraint $constraint)
return;
}

$context = $this->context;
$validator = $context->getValidator()->inContext($context);
$validator = $this->context->getValidator()->inContext($this->context);
foreach ($value as $key => $element) {
$validator->atPath('[' . $key . ']')->validate($element, $constraint->constraints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityExtendBundle\Validator\FieldNameValidationHelper;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* The base class for entity field configuration validators.
*/
abstract class AbstractFieldValidator extends ConstraintValidator
{
/** @var FieldNameValidationHelper */
Expand Down Expand Up @@ -39,13 +41,7 @@ protected function assertValidatingValue($value)
*/
protected function addViolation($message, $newFieldName, $existingFieldName)
{
/** @var ExecutionContextInterface $context */
$context = $this->context;
$context
->buildViolation(
$message,
['{{ value }}' => $newFieldName, '{{ field }}' => $existingFieldName]
)
$this->context->buildViolation($message, ['{{ value }}' => $newFieldName, '{{ field }}' => $existingFieldName])
->atPath('fieldName')
->addViolation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand Down Expand Up @@ -94,9 +93,7 @@ public function validate($value, Constraint $constraint)

if (!$this->validateOwner($ownershipMetadata, $em, $entityClass, $value)) {
$ownerFieldName = $ownershipMetadata->getOwnerFieldName();
/** @var ExecutionContextInterface $context */
$context = $this->context;
$context->buildViolation($constraint->message)
$this->context->buildViolation($constraint->message)
->atPath($ownerFieldName)
->setParameter('{{ owner }}', $ownerFieldName)
->addViolation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\ORM\PersistentCollection;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
Expand All @@ -28,27 +27,26 @@ class ValidLoadedItemsValidator extends ConstraintValidator
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof ValidLoadedItems) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\ValidLoadedItems');
throw new UnexpectedTypeException($constraint, ValidLoadedItems::class);
}

if (null === $value) {
return;
}

if (!is_array($value) && !$value instanceof \Traversable) {
if (!\is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
}

if ($value instanceof AbstractLazyCollection && !$value->isInitialized()) {
return;
}

/** @var ExecutionContextInterface $context */
$context = $this->context;
$validator = $context->getValidator()->inContext($context);
if ($value instanceof PersistentCollection) {
$value = $value->unwrap();
}

$validator = $this->context->getValidator()->inContext($this->context);
foreach ($value as $key => $element) {
$validator->atPath('[' . $key . ']')->validate($element, $constraint->constraints);
}
Expand Down

0 comments on commit 132da7d

Please sign in to comment.