Skip to content

[Translation] Fix docblock and ensure that preg_match isn't called on null #33247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface
private $configCacheFactory;

/**
* @param string $locale The locale
* @param string|null $locale The locale
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right to me. If we were to make this change, we would also have to change the return type of getLocale() which would break the contract of the TranslatorInterface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the interface is lying and needs to be fixed. That is what I wrote in #33154 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a use case for a null locale. My suggestion is to deprecate support for this instead (see #33272).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that support for passing null is to be removed in Symfony 5.0 I would revert the docblock changes and only keep the check for null below.

* @param MessageFormatterInterface|null $formatter The message formatter
* @param string|null $cacheDir The directory to use for the cache
* @param bool $debug Use cache in debug mode ?
Expand Down Expand Up @@ -422,13 +422,13 @@ protected function computeFallbackLocales($locale)
/**
* Asserts that the locale is valid, throws an Exception if not.
*
* @param string $locale Locale to tests
* @param string|null $locale Locale to tests
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
protected function assertValidLocale($locale)
{
if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
if (null !== $locale && !preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
}
}
Expand Down