Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Refactor without magic methods / class #60029

Open
@prdatur

Description

@prdatur

Description

I needed to use the symony/validator and I hoped I am not going "back" Zend Framework with all those Magic methods / Magic class generations.

Then I discovered this:

public function validatedBy()
    {
        return static::class.'Validator';
    }

And more badly this:

if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
            if (null === $defaultOption) {
                throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', static::class));
            }

            $options[$defaultOption] = $options['value'];
            unset($options['value']);
        }

which ends in

foreach ($options as $name => $value) {
            $this->$name = $value;
        }

I was really shocked at the moment i saw this. Any refactoring method will fail and break code, due to unused classes.

  • It is not required (also right now) that the validator needs to be a different class then the constraint
  • The options should just be in an option array, or better in a DTO, which can be then accessed through getOption(): DTO or if it is an array getOption(string $key): mixed

If you can not change this, what is the reason why this was build in such a way?

br,
PrDatur

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions