From c34fcd91d1b4b2d7403463d504b3c34616c7315f Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 17 Jun 2019 20:33:53 +0200 Subject: [PATCH] Add BC layer for updated constructor types --- src/Symfony/Component/Config/Definition/BaseNode.php | 5 +---- .../Config/Definition/Builder/NodeDefinition.php | 2 +- src/Symfony/Component/Form/FormConfigBuilder.php | 8 +++++--- src/Symfony/Component/Form/FormError.php | 7 ++++++- .../Component/HttpFoundation/RedirectResponse.php | 9 +++++++-- src/Symfony/Component/Validator/ConstraintViolation.php | 7 ++++++- .../Validator/Violation/ConstraintViolationBuilder.php | 6 +++++- 7 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Config/Definition/BaseNode.php b/src/Symfony/Component/Config/Definition/BaseNode.php index 866feb6e1ad65..39511c15c6b85 100644 --- a/src/Symfony/Component/Config/Definition/BaseNode.php +++ b/src/Symfony/Component/Config/Definition/BaseNode.php @@ -47,10 +47,7 @@ abstract class BaseNode implements NodeInterface */ public function __construct(?string $name, NodeInterface $parent = null, string $pathSeparator = self::DEFAULT_PATH_SEPARATOR) { - if (null === $name) { - $name = ''; - } - if (false !== strpos($name, $pathSeparator)) { + if (false !== strpos($name = (string) $name, $pathSeparator)) { throw new \InvalidArgumentException('The name must not contain "'.$pathSeparator.'".'); } diff --git a/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php index 948a68f6b8d32..a771a43cd2a50 100644 --- a/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php @@ -41,7 +41,7 @@ abstract class NodeDefinition implements NodeParentInterface public function __construct(?string $name, NodeParentInterface $parent = null) { $this->parent = $parent; - $this->name = $name ?? ''; + $this->name = $name; } /** diff --git a/src/Symfony/Component/Form/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index 9effe1a7c64a0..74fa35a3d32f1 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -123,7 +123,7 @@ public function __construct(?string $name, ?string $dataClass, EventDispatcherIn throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass)); } - $this->name = $name ?? ''; + $this->name = (string) $name; $this->dataClass = $dataClass; $this->dispatcher = $dispatcher; $this->options = $options; @@ -772,7 +772,7 @@ public function getFormConfig() * @throws UnexpectedTypeException if the name is not a string or an integer * @throws InvalidArgumentException if the name contains invalid characters * - * @internal since Symfony 4.4 + * @internal since Symfony 4.4, to be removed in 5.0 */ public static function validateName($name) { @@ -794,8 +794,10 @@ public static function validateName($name) * * starts with a letter, digit or underscore * * contains only letters, digits, numbers, underscores ("_"), * hyphens ("-") and colons (":") + * + * @final since Symfony 4.4 */ - final public static function isValidName(?string $name): bool + public static function isValidName(?string $name): bool { return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name); } diff --git a/src/Symfony/Component/Form/FormError.php b/src/Symfony/Component/Form/FormError.php index f390854aeee1c..899631257b1ea 100644 --- a/src/Symfony/Component/Form/FormError.php +++ b/src/Symfony/Component/Form/FormError.php @@ -47,8 +47,13 @@ class FormError * * @see \Symfony\Component\Translation\Translator */ - public function __construct(string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null) + public function __construct(?string $message, string $messageTemplate = null, array $messageParameters = [], int $messagePluralization = null, $cause = null) { + if (null === $message) { + @trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED); + $message = ''; + } + $this->message = $message; $this->messageTemplate = $messageTemplate ?: $message; $this->messageParameters = $messageParameters; diff --git a/src/Symfony/Component/HttpFoundation/RedirectResponse.php b/src/Symfony/Component/HttpFoundation/RedirectResponse.php index c6b559beb1d80..1d4f37cae3324 100644 --- a/src/Symfony/Component/HttpFoundation/RedirectResponse.php +++ b/src/Symfony/Component/HttpFoundation/RedirectResponse.php @@ -32,8 +32,13 @@ class RedirectResponse extends Response * * @see http://tools.ietf.org/html/rfc2616#section-10.3 */ - public function __construct(string $url, int $status = 302, array $headers = []) + public function __construct(?string $url, int $status = 302, array $headers = []) { + if (null === $url) { + @trigger_error(sprintf('Passing a null url when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED); + $url = ''; + } + parent::__construct('', $status, $headers); $this->setTargetUrl($url); @@ -82,7 +87,7 @@ public function getTargetUrl() */ public function setTargetUrl($url) { - if ('' == $url) { + if ('' === ($url ?? '')) { throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); } diff --git a/src/Symfony/Component/Validator/ConstraintViolation.php b/src/Symfony/Component/Validator/ConstraintViolation.php index a0ce00e33426c..2ec15e4309c54 100644 --- a/src/Symfony/Component/Validator/ConstraintViolation.php +++ b/src/Symfony/Component/Validator/ConstraintViolation.php @@ -49,8 +49,13 @@ class ConstraintViolation implements ConstraintViolationInterface * caused the violation * @param mixed $cause The cause of the violation */ - public function __construct(string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null) + public function __construct(?string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null) { + if (null === $message) { + @trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED); + $message = ''; + } + $this->message = $message; $this->messageTemplate = $messageTemplate; $this->parameters = $parameters; diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index 553f17f14acf7..ed18f6fa8e542 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -47,8 +47,12 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface /** * @param TranslatorInterface $translator */ - public function __construct(ConstraintViolationList $violations, Constraint $constraint, string $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null) + public function __construct(ConstraintViolationList $violations, Constraint $constraint, ?string $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null) { + if (null === $message) { + @trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED); + $message = ''; + } if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) { throw new \TypeError(sprintf('Argument 8 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator))); }