[Web] Some minor fixes and improvements for PHP 8

This commit is contained in:
andryyy
2021-08-08 16:06:55 +02:00
parent eec75690e0
commit cf8fdae277
138 changed files with 2398 additions and 2342 deletions

View File

@@ -94,13 +94,13 @@ EOF
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$provider = $this->providers->get($input->getArgument('provider'));
if (!$this->enabledLocales) {
throw new InvalidArgumentException('You must define "framework.translator.enabled_locales" or "framework.translator.providers.%s.locales" config key in order to work with translation providers.');
throw new InvalidArgumentException(sprintf('You must define "framework.translator.enabled_locales" or "framework.translator.providers.%s.locales" config key in order to work with translation providers.', parse_url($provider, \PHP_URL_SCHEME)));
}
$io = new SymfonyStyle($input, $output);
$provider = $this->providers->get($input->getArgument('provider'));
$domains = $input->getOption('domains');
$locales = $input->getOption('locales');
$force = $input->getOption('force');

View File

@@ -64,6 +64,9 @@ class TranslatorPathsPass extends AbstractRecursivePass
foreach ($this->paths as $class => $_) {
if (($r = $container->getReflectionClass($class)) && !$r->isInterface()) {
$paths[] = $r->getFileName();
foreach ($r->getTraits() as $trait) {
$paths[] = $trait->getFileName();
}
}
}
if ($paths) {

View File

@@ -141,8 +141,8 @@ class XliffFileDumper extends FileDumper
$xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
$xliffFile = $xliff->appendChild($dom->createElement('file'));
if (MessageCatalogue::INTL_DOMAIN_SUFFIX === substr($domain, -($suffixLength = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)))) {
$xliffFile->setAttribute('id', substr($domain, 0, -$suffixLength).'.'.$messages->getLocale());
if (str_ends_with($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
$xliffFile->setAttribute('id', substr($domain, 0, -\strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX)).'.'.$messages->getLocale());
} else {
$xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
}
@@ -198,6 +198,6 @@ class XliffFileDumper extends FileDumper
private function hasMetadataArrayInfo(string $key, array $metadata = null): bool
{
return null !== $metadata && \array_key_exists($key, $metadata) && ($metadata[$key] instanceof \Traversable || \is_array($metadata[$key]));
return is_iterable($metadata[$key] ?? null);
}
}

View File

@@ -13,7 +13,7 @@ namespace Symfony\Component\Translation\Exception;
class IncompleteDsnException extends InvalidArgumentException
{
public function __construct(string $message, string $dsn = null, ?\Throwable $previous = null)
public function __construct(string $message, string $dsn = null, \Throwable $previous = null)
{
if ($dsn) {
$message = sprintf('Invalid "%s" provider DSN: ', $dsn).$message;

View File

@@ -16,7 +16,7 @@ namespace Symfony\Component\Translation\Exception;
*/
class MissingRequiredOptionException extends IncompleteDsnException
{
public function __construct(string $option, string $dsn = null, ?\Throwable $previous = null)
public function __construct(string $option, string $dsn = null, \Throwable $previous = null)
{
$message = sprintf('The option "%s" is required but missing.', $option);

View File

@@ -29,8 +29,6 @@ class ChainExtractor implements ExtractorInterface
/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
*/
public function addExtractor(string $format, ExtractorInterface $extractor)
{

View File

@@ -24,14 +24,12 @@ interface ExtractorInterface
/**
* Extracts translation messages from files, a file or a directory to the catalogue.
*
* @param string|string[] $resource Files, a file or a directory
* @param string|iterable<string> $resource Files, a file or a directory
*/
public function extract($resource, MessageCatalogue $catalogue);
/**
* Sets the prefix that should be used for new found messages.
*
* @param string $prefix The prefix
*/
public function setPrefix(string $prefix);
}

View File

@@ -133,7 +133,7 @@ class PhpStringTokenParser
$str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
// nowdoc string
if (false !== strpos($startToken, '\'')) {
if (str_contains($startToken, '\'')) {
return $str;
}

View File

@@ -27,7 +27,7 @@ class CsvFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
$messages = [];

View File

@@ -55,11 +55,9 @@ abstract class FileLoader extends ArrayLoader
}
/**
* @param string $resource
*
* @return array
*
* @throws InvalidResourceException if stream content has an invalid format
*/
abstract protected function loadResource($resource);
abstract protected function loadResource(string $resource);
}

View File

@@ -21,7 +21,7 @@ class IniFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
return parse_ini_file($resource, true);
}

View File

@@ -23,7 +23,7 @@ class JsonFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
$messages = [];
if ($data = file_get_contents($resource)) {

View File

@@ -41,7 +41,7 @@ class MoFileLoader extends FileLoader
*
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
$stream = fopen($resource, 'r');
@@ -89,7 +89,7 @@ class MoFileLoader extends FileLoader
fseek($stream, $offset);
$singularId = fread($stream, $length);
if (false !== strpos($singularId, "\000")) {
if (str_contains($singularId, "\000")) {
[$singularId, $pluralId] = explode("\000", $singularId);
}
@@ -104,7 +104,7 @@ class MoFileLoader extends FileLoader
fseek($stream, $offset);
$translated = fread($stream, $length);
if (false !== strpos($translated, "\000")) {
if (str_contains($translated, "\000")) {
$translated = explode("\000", $translated);
}

View File

@@ -23,7 +23,7 @@ class PhpFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
if ([] === self::$cache && \function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) || filter_var(ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOLEAN))) {
self::$cache = null;

View File

@@ -60,7 +60,7 @@ class PoFileLoader extends FileLoader
*
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
$stream = fopen($resource, 'r');

View File

@@ -75,7 +75,7 @@ class XliffFileLoader implements LoaderInterface
return $catalogue;
}
private function extract($dom, MessageCatalogue $catalogue, string $domain)
private function extract(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
{
$xliffVersion = XliffUtils::getVersionNumber($dom);

View File

@@ -29,7 +29,7 @@ class YamlFileLoader extends FileLoader
/**
* {@inheritdoc}
*/
protected function loadResource($resource)
protected function loadResource(string $resource)
{
if (null === $this->yamlParser) {
if (!class_exists(\Symfony\Component\Yaml\Parser::class)) {

View File

@@ -27,8 +27,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
private $parent;
/**
* @param string $locale The locale
* @param array $messages An array of messages classified by domain
* @param array $messages An array of messages classified by domain
*/
public function __construct(string $locale, array $messages = [])
{
@@ -50,11 +49,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
public function getDomains()
{
$domains = [];
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
foreach ($this->messages as $domain => $messages) {
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$domain = substr($domain, 0, $i);
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
$domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
}
$domains[$domain] = $domain;
}
@@ -69,7 +67,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
{
if (null !== $domain) {
// skip messages merge if intl-icu requested explicitly
if (false !== strpos($domain, self::INTL_DOMAIN_SUFFIX)) {
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
return $this->messages[$domain] ?? [];
}
@@ -77,11 +75,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
}
$allMessages = [];
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
foreach ($this->messages as $domain => $messages) {
if (\strlen($domain) > $suffixLength && false !== $i = strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$domain = substr($domain, 0, $i);
if (str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
$domain = substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX));
$allMessages[$domain] = $messages + ($allMessages[$domain] ?? []);
} else {
$allMessages[$domain] = ($allMessages[$domain] ?? []) + $messages;
@@ -162,8 +159,7 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
$this->messages[$domain] = [];
}
$intlDomain = $domain;
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
if (\strlen($domain) < $suffixLength || false === strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
if (!str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) {
$intlDomain .= self::INTL_DOMAIN_SUFFIX;
}
foreach ($messages as $id => $message) {

View File

@@ -25,9 +25,6 @@ interface MetadataAwareInterface
* domain and then by key. Passing an empty key will return an array with all
* metadata for the given domain.
*
* @param string $key The key
* @param string $domain The domain name
*
* @return mixed The value that was set or an array with the domains/keys or null
*/
public function getMetadata(string $key = '', string $domain = 'messages');
@@ -35,9 +32,7 @@ interface MetadataAwareInterface
/**
* Adds metadata to a message domain.
*
* @param string $key The key
* @param mixed $value The value
* @param string $domain The domain name
* @param mixed $value
*/
public function setMetadata(string $key, $value, string $domain = 'messages');
@@ -46,9 +41,6 @@ interface MetadataAwareInterface
*
* Passing an empty domain will delete all metadata. Passing an empty key will
* delete all metadata for the given domain.
*
* @param string $key The key
* @param string $domain The domain name
*/
public function deleteMetadata(string $key = '', string $domain = 'messages');
}

View File

@@ -52,7 +52,7 @@ if ($argc > 3) {
}
foreach (array_slice($argv, 1) as $argumentOrOption) {
if (0 === strpos($argumentOrOption, '-')) {
if (str_starts_with($argumentOrOption, '-')) {
$config['verbose_output'] = true;
} else {
$config['locale_to_analyze'] = $argumentOrOption;

View File

@@ -134,6 +134,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
}
$this->assertValidLocale($locale);
$locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';
$this->resources[$locale][] = [$format, $resource, $domain];
@@ -150,7 +151,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
public function setLocale(string $locale)
{
$this->assertValidLocale($locale);
$this->locale = $locale ?? (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
$this->locale = $locale;
}
/**
@@ -158,14 +159,12 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
*/
public function getLocale()
{
return $this->locale;
return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
}
/**
* Sets the fallback locales.
*
* @param array $locales The fallback locales
*
* @throws InvalidArgumentException If a locale contains invalid characters
*/
public function setFallbackLocales(array $locales)
@@ -230,7 +229,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
*/
public function getCatalogue(string $locale = null)
{
if (null === $locale) {
if (!$locale) {
$locale = $this->getLocale();
} else {
$this->assertValidLocale($locale);
@@ -454,7 +453,7 @@ EOF
*/
protected function assertValidLocale(string $locale)
{
if (null !== $locale && 1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', (string) $locale)) {
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
}
}

View File

@@ -27,10 +27,8 @@ class TranslationWriter implements TranslationWriterInterface
/**
* Adds a dumper to the writer.
*
* @param string $format The format of the dumper
*/
public function addDumper($format, DumperInterface $dumper)
public function addDumper(string $format, DumperInterface $dumper)
{
$this->dumpers[$format] = $dumper;
}

View File

@@ -19,7 +19,7 @@
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.15",
"symfony/polyfill-php80": "^1.16",
"symfony/translation-contracts": "^2.3"
},
"require-dev": {
@@ -32,7 +32,7 @@
"symfony/service-contracts": "^1.1.2|^2",
"symfony/yaml": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"psr/log": "~1.0"
"psr/log": "^1|^2|^3"
},
"conflict": {
"symfony/config": "<4.4",