Repository: symfony/symfony
File: src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php (PHP)

Code before:
        } elseif (str_contains($name, ':')) {
            [$prefix, $className] = explode(':', $name, 2);

            if (!isset($this->namespaces[$prefix])) {
                throw new MappingException(\sprintf('Undefined namespace prefix "%s".', $prefix));
            }

            $className = $this->namespaces[$prefix].$className;
        } else {
            $className = self::DEFAULT_NAMESPACE.$name;
        }

        if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
            if (null === $options) {
                return new $className();
            }

            if (!\is_array($options)) {
                return new $className($options);
            }

            if (1 === \count($options) && isset($options['value'])) {
                return new $className($options['value']);
            }

            if (array_is_list($options) || isset($options['value'])) {
                return new $className($options);
            }

            return new $className(...$options);
        }

        if ($options) {
            trigger_deprecation('symfony/validator', '7.3', 'Using constraints not supporting named arguments is deprecated. Try adding the HasNamedArguments attribute to %s.', $className);

            return new $className($options);
        }

        return new $className();
    }
}


Review comment (suggestion):
This might be a bit risky if the constraint has a constructor argument named `$value`.

An alternative solution would be wrapping the call further down in a `try` `catch` block like this:

```php
try {
    return new $className(...$options);
} catch (\Error $e) {
    if (str_starts_with($e->getMessage(), 'Unknown named parameter')) {
        return new $className($options);
    }

    throw $e;
}
```

Code after:
        } elseif (str_contains($name, ':')) {
            [$prefix, $className] = explode(':', $name, 2);

            if (!isset($this->namespaces[$prefix])) {
                throw new MappingException(\sprintf('Undefined namespace prefix "%s".', $prefix));
            }

            $className = $this->namespaces[$prefix].$className;
        } else {
            $className = self::DEFAULT_NAMESPACE.$name;
        }

        if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
            if (null === $options) {
                return new $className();
            }

            if (!\is_array($options)) {
                return new $className($options);
            }

            if (1 === \count($options) && isset($options['value'])) {
                return new $className($options['value']);
            }

            if (array_is_list($options)) {
                return new $className($options);
            }

            try {
                return new $className(...$options);
            } catch (\Error $e) {
                if (str_starts_with($e->getMessage(), 'Unknown named parameter ')) {
                    return new $className($options);
                }

                throw $e;
            }
        }

        if ($options) {
            trigger_deprecation('symfony/validator', '7.3', 'Using constraints not supporting named arguments is deprecated. Try adding the HasNamedArguments attribute to %s.', $className);

            return new $className($options);
        }

        return new $className();
    }
}