Skip to content
Permalink
Browse files

minor #32218 [5.0] [Translator] Add parameter type-hints where possib…

…le (Matts)

This PR was squashed before being merged into the 5.0-dev branch (closes #32218).

Discussion
----------

[5.0] [Translator] Add parameter type-hints where possible

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32179  <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Adding scalar typehints in Translator.

Commits
-------

470996f [5.0] [Translator] Add parameter type-hints where possible
  • Loading branch information...
fabpot committed Jul 5, 2019
2 parents 78dc3c3 + 470996f commit 53e68f7824da66d0b3364c5079d35376016342b3
Showing with 82 additions and 106 deletions.
  1. +4 −4 src/Symfony/Component/Translation/Catalogue/AbstractOperation.php
  2. +1 −1 src/Symfony/Component/Translation/Catalogue/MergeOperation.php
  3. +3 −9 src/Symfony/Component/Translation/Catalogue/OperationInterface.php
  4. +1 −1 src/Symfony/Component/Translation/Catalogue/TargetOperation.php
  5. +1 −1 src/Symfony/Component/Translation/DataCollectorTranslator.php
  6. +2 −5 src/Symfony/Component/Translation/Dumper/CsvFileDumper.php
  7. +1 −1 src/Symfony/Component/Translation/Dumper/DumperInterface.php
  8. +3 −7 src/Symfony/Component/Translation/Dumper/FileDumper.php
  9. +3 −3 src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php
  10. +1 −1 src/Symfony/Component/Translation/Dumper/IniFileDumper.php
  11. +1 −1 src/Symfony/Component/Translation/Dumper/JsonFileDumper.php
  12. +1 −1 src/Symfony/Component/Translation/Dumper/MoFileDumper.php
  13. +1 −1 src/Symfony/Component/Translation/Dumper/PhpFileDumper.php
  14. +1 −1 src/Symfony/Component/Translation/Dumper/PoFileDumper.php
  15. +1 −1 src/Symfony/Component/Translation/Dumper/QtFileDumper.php
  16. +1 −1 src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
  17. +1 −1 src/Symfony/Component/Translation/Dumper/YamlFileDumper.php
  18. +2 −6 src/Symfony/Component/Translation/Extractor/AbstractFileExtractor.php
  19. +2 −2 src/Symfony/Component/Translation/Extractor/ChainExtractor.php
  20. +1 −1 src/Symfony/Component/Translation/Extractor/ExtractorInterface.php
  21. +1 −1 src/Symfony/Component/Translation/Extractor/PhpExtractor.php
  22. +3 −3 src/Symfony/Component/Translation/Extractor/PhpStringTokenParser.php
  23. +1 −1 src/Symfony/Component/Translation/Formatter/MessageFormatter.php
  24. +1 −1 src/Symfony/Component/Translation/Formatter/MessageFormatterInterface.php
  25. +1 −1 src/Symfony/Component/Translation/Loader/ArrayLoader.php
  26. +1 −5 src/Symfony/Component/Translation/Loader/CsvFileLoader.php
  27. +1 −1 src/Symfony/Component/Translation/Loader/FileLoader.php
  28. +1 −1 src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php
  29. +2 −2 src/Symfony/Component/Translation/Loader/IcuResFileLoader.php
  30. +1 −1 src/Symfony/Component/Translation/Loader/JsonFileLoader.php
  31. +1 −1 src/Symfony/Component/Translation/Loader/LoaderInterface.php
  32. +1 −1 src/Symfony/Component/Translation/Loader/QtFileLoader.php
  33. +2 −2 src/Symfony/Component/Translation/Loader/XliffFileLoader.php
  34. +1 −1 src/Symfony/Component/Translation/LoggingTranslator.php
  35. +10 −10 src/Symfony/Component/Translation/MessageCatalogue.php
  36. +7 −7 src/Symfony/Component/Translation/MessageCatalogueInterface.php
  37. +3 −3 src/Symfony/Component/Translation/MetadataAwareInterface.php
  38. +2 −2 src/Symfony/Component/Translation/Reader/TranslationReader.php
  39. +1 −4 src/Symfony/Component/Translation/Reader/TranslationReaderInterface.php
  40. +5 −5 src/Symfony/Component/Translation/Translator.php
  41. +1 −1 src/Symfony/Component/Translation/TranslatorBagInterface.php
  42. +1 −1 src/Symfony/Component/Translation/Util/ArrayConverter.php
  43. +1 −1 src/Symfony/Component/Translation/Writer/TranslationWriter.php
  44. +1 −1 src/Symfony/Component/Translation/Writer/TranslationWriterInterface.php
@@ -88,7 +88,7 @@ public function getDomains()
/**
* {@inheritdoc}
*/
public function getMessages($domain)
public function getMessages(string $domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
@@ -104,7 +104,7 @@ public function getMessages($domain)
/**
* {@inheritdoc}
*/
public function getNewMessages($domain)
public function getNewMessages(string $domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
@@ -120,7 +120,7 @@ public function getNewMessages($domain)
/**
* {@inheritdoc}
*/
public function getObsoleteMessages($domain)
public function getObsoleteMessages(string $domain)
{
if (!\in_array($domain, $this->getDomains())) {
throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain));
@@ -153,5 +153,5 @@ public function getResult()
*
* @param string $domain The domain which the operation will be performed for
*/
abstract protected function processDomain($domain);
abstract protected function processDomain(string $domain);
}
@@ -27,7 +27,7 @@ class MergeOperation extends AbstractOperation
/**
* {@inheritdoc}
*/
protected function processDomain($domain)
protected function processDomain(string $domain)
{
$this->messages[$domain] = [
'all' => [],
@@ -44,29 +44,23 @@ public function getDomains();
/**
* Returns all valid messages ('all') after operation.
*
* @param string $domain
*
* @return array
*/
public function getMessages($domain);
public function getMessages(string $domain);
/**
* Returns new messages ('new') after operation.
*
* @param string $domain
*
* @return array
*/
public function getNewMessages($domain);
public function getNewMessages(string $domain);
/**
* Returns obsolete messages ('obsolete') after operation.
*
* @param string $domain
*
* @return array
*/
public function getObsoleteMessages($domain);
public function getObsoleteMessages(string $domain);
/**
* Returns resulting catalogue ('result').
@@ -28,7 +28,7 @@ class TargetOperation extends AbstractOperation
/**
* {@inheritdoc}
*/
protected function processDomain($domain)
protected function processDomain(string $domain)
{
$this->messages[$domain] = [
'all' => [],
@@ -74,7 +74,7 @@ public function getLocale()
/**
* {@inheritdoc}
*/
public function getCatalogue($locale = null)
public function getCatalogue(string $locale = null)
{
return $this->translator->getCatalogue($locale);
}
@@ -26,7 +26,7 @@ class CsvFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$handle = fopen('php://memory', 'r+b');
@@ -43,11 +43,8 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
/**
* Sets the delimiter and escape character for CSV.
*
* @param string $delimiter Delimiter character
* @param string $enclosure Enclosure character
*/
public function setCsvControl($delimiter = ';', $enclosure = '"')
public function setCsvControl(string $delimiter = ';', string $enclosure = '"')
{
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
@@ -27,5 +27,5 @@ interface DumperInterface
* @param MessageCatalogue $messages The message catalogue
* @param array $options Options that are used by the dumper
*/
public function dump(MessageCatalogue $messages, $options = []);
public function dump(MessageCatalogue $messages, array $options = []);
}
@@ -37,15 +37,15 @@ abstract class FileDumper implements DumperInterface
*
* @param string $relativePathTemplate A template for the relative paths to files
*/
public function setRelativePathTemplate($relativePathTemplate)
public function setRelativePathTemplate(string $relativePathTemplate)
{
$this->relativePathTemplate = $relativePathTemplate;
}
/**
* {@inheritdoc}
*/
public function dump(MessageCatalogue $messages, $options = [])
public function dump(MessageCatalogue $messages, array $options = [])
{
if (!\array_key_exists('path', $options)) {
throw new InvalidArgumentException('The file dumper needs a path option.');
@@ -87,13 +87,9 @@ public function dump(MessageCatalogue $messages, $options = [])
/**
* Transforms a domain of a message catalogue to its string representation.
*
* @param MessageCatalogue $messages
* @param string $domain
* @param array $options
*
* @return string representation
*/
abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []);
abstract public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []);
/**
* Gets the file extension of the dumper.
@@ -28,7 +28,7 @@ class IcuResFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$data = $indexes = $resources = '';
@@ -82,7 +82,7 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
return $header.$root.$data;
}
private function writePadding($data)
private function writePadding(string $data)
{
$padding = \strlen($data) % 4;
@@ -91,7 +91,7 @@ private function writePadding($data)
}
}
private function getPosition($data)
private function getPosition(string $data)
{
return (\strlen($data) + 28) / 4;
}
@@ -23,7 +23,7 @@ class IniFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$output = '';
@@ -23,7 +23,7 @@ class JsonFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$flags = $options['json_encoding'] ?? JSON_PRETTY_PRINT;
@@ -24,7 +24,7 @@ class MoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$sources = $targets = $sourceOffsets = $targetOffsets = '';
$offsets = [];
@@ -23,7 +23,7 @@ class PhpFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
return "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";
}
@@ -23,7 +23,7 @@ class PoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$output = 'msgid ""'."\n";
$output .= 'msgstr ""'."\n";
@@ -23,7 +23,7 @@ class QtFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
@@ -24,7 +24,7 @@ class XliffFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
$xliffVersion = '1.2';
if (\array_key_exists('xliff_version', $options)) {
@@ -33,7 +33,7 @@ public function __construct(string $extension = 'yml')
/**
* {@inheritdoc}
*/
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = [])
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');
@@ -49,13 +49,11 @@ private function toSplFileInfo(string $file): \SplFileInfo
}
/**
* @param string $file
*
* @return bool
*
* @throws InvalidArgumentException
*/
protected function isFile($file)
protected function isFile(string $file)
{
if (!is_file($file)) {
throw new InvalidArgumentException(sprintf('The "%s" file does not exist.', $file));
@@ -65,11 +63,9 @@ protected function isFile($file)
}
/**
* @param string $file
*
* @return bool
*/
abstract protected function canBeExtracted($file);
abstract protected function canBeExtracted(string $file);
/**
* @param string|array $resource Files, a file or a directory
@@ -33,15 +33,15 @@ class ChainExtractor implements ExtractorInterface
* @param string $format The format of the loader
* @param ExtractorInterface $extractor The loader
*/
public function addExtractor($format, ExtractorInterface $extractor)
public function addExtractor(string $format, ExtractorInterface $extractor)
{
$this->extractors[$format] = $extractor;
}
/**
* {@inheritdoc}
*/
public function setPrefix($prefix)
public function setPrefix(string $prefix)
{
foreach ($this->extractors as $extractor) {
$extractor->setPrefix($prefix);
@@ -34,5 +34,5 @@ public function extract($resource, MessageCatalogue $catalogue);
*
* @param string $prefix The prefix
*/
public function setPrefix($prefix);
public function setPrefix(string $prefix);
}
@@ -72,7 +72,7 @@ public function extract($resource, MessageCatalogue $catalog)
/**
* {@inheritdoc}
*/
public function setPrefix($prefix)
public function setPrefix(string $prefix)
{
$this->prefix = $prefix;
}
@@ -67,7 +67,7 @@ class PhpStringTokenParser
*
* @return string The parsed string
*/
public static function parse($str)
public static function parse(string $str)
{
$bLength = 0;
if ('b' === $str[0]) {
@@ -93,7 +93,7 @@ public static function parse($str)
*
* @return string String with escape sequences parsed
*/
public static function parseEscapeSequences($str, $quote)
public static function parseEscapeSequences(string $str, string $quote = null)
{
if (null !== $quote) {
$str = str_replace('\\'.$quote, $quote, $str);
@@ -127,7 +127,7 @@ private static function parseCallback($matches)
*
* @return string Parsed string
*/
public static function parseDocString($startToken, $str)
public static function parseDocString(string $startToken, string $str)
{
// strip last newline (thanks tokenizer for sticking it into the string!)
$str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
@@ -34,7 +34,7 @@ public function __construct(TranslatorInterface $translator = null, IntlFormatte
/**
* {@inheritdoc}
*/
public function format($message, $locale, array $parameters = [])
public function format(string $message, string $locale, array $parameters = [])
{
if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, $parameters, null, $locale);
@@ -26,5 +26,5 @@ interface MessageFormatterInterface
*
* @return string
*/
public function format($message, $locale, array $parameters = []);
public function format(string $message, string $locale, array $parameters = []);
}
@@ -23,7 +23,7 @@ class ArrayLoader implements LoaderInterface
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
public function load($resource, string $locale, string $domain = 'messages')
{
$resource = $this->flatten($resource);
$catalogue = new MessageCatalogue($locale);
@@ -51,12 +51,8 @@ protected function loadResource($resource)
/**
* Sets the delimiter, enclosure, and escape character for CSV.
*
* @param string $delimiter Delimiter character
* @param string $enclosure Enclosure character
* @param string $escape Escape character
*/
public function setCsvControl($delimiter = ';', $enclosure = '"', $escape = '\\')
public function setCsvControl(string $delimiter = ';', string $enclosure = '"', string $escape = '\\')
{
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
@@ -23,7 +23,7 @@ abstract class FileLoader extends ArrayLoader
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
public function load($resource, string $locale, string $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));

0 comments on commit 53e68f7

Please sign in to comment.
You can’t perform that action at this time.