Skip to content

Commit

Permalink
Make all interfaces more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 10, 2023
1 parent 24887b4 commit e59c279
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 43 deletions.
15 changes: 10 additions & 5 deletions src/GettextTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function encodeMessageWithContext($message, $context)
return "{$context}\x04{$message}";
}

public function translate($message, $context = null)
public function translate(string $message, ?string $context = null): string
{
if ($context !== null) {
$messageForGettext = $this->encodeMessageWithContext($message, $context);
Expand All @@ -236,7 +236,7 @@ public function translate($message, $context = null)
return $translation;
}

public function translateInDomain($domain, $message, $context = null)
public function translateInDomain(string $domain, string $message, ?string $context = null): string
{
if ($context !== null) {
$messageForGettext = $this->encodeMessageWithContext($message, $context);
Expand All @@ -263,7 +263,7 @@ public function translateInDomain($domain, $message, $context = null)
return $translation;
}

public function translatePlural($singular, $plural, $number, $context = null)
public function translatePlural(string $singular, string $plural, int $number, ?string $context = null): string
{
if ($context !== null) {
$singularForGettext = $this->encodeMessageWithContext($singular, $context);
Expand All @@ -285,8 +285,13 @@ public function translatePlural($singular, $plural, $number, $context = null)
return $translation;
}

public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null)
{
public function translatePluralInDomain(
string $domain,
string $singular,
string $plural,
int $number,
?string $context = null
): string {
if ($context !== null) {
$singularForGettext = $this->encodeMessageWithContext($singular, $context);
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Locale
*
* @return string
*/
public function getDefaultLocale()
public function getDefaultLocale(): string
{
return $this->defaultLocale;
}
Expand All @@ -24,7 +24,7 @@ public function getDefaultLocale()
*
* @return $this
*/
public function setDefaultLocale($defaultLocale)
public function setDefaultLocale(string $defaultLocale): self
{
$this->defaultLocale = $defaultLocale;

Expand All @@ -39,7 +39,7 @@ public function setDefaultLocale($defaultLocale)
*
* @return string The browser's preferred locale code
*/
public function getPreferred($header, array $available)
public function getPreferred(string $header, array $available): string
{
$headerValues = explode(',', $header);
for ($i = 0; $i < count($headerValues); $i++) {
Expand Down Expand Up @@ -117,7 +117,7 @@ function ($a, $b) {
*
* @return object Output of {@link \Locale::parseLocale()} converted to an object
*/
public function parseLocale($locale)
public function parseLocale(string $locale): object
{
return (object) \Locale::parseLocale($locale);
}
Expand Down
15 changes: 10 additions & 5 deletions src/NoopTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@
*/
class NoopTranslator implements Translator
{
public function translate($message, $context = null)
public function translate(string $message, ?string $context = null): string
{
return $message;
}

public function translateInDomain($domain, $message, $context = null)
public function translateInDomain(string $domain, string $message, ?string $context = null): string
{
return $message;
}

public function translatePlural($singular, $plural, $number, $context = null)
public function translatePlural(string $singular, string $plural, int $number, ?string $context = null): string
{
return $number === 1 ? $singular : $plural;
}

public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null)
{
public function translatePluralInDomain(
string $domain,
string $singular,
string $plural,
int $number,
?string $context = null
): string {
return $number === 1 ? $singular : $plural;
}
}
45 changes: 25 additions & 20 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ trait Translation
/**
* Translate a message
*
* @param string $message
* @param string $context Message context
* @param string $message
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
public function translate($message, $context = null)
public function translate(string $message, ?string $context = null): string
{
return $this->translationDomain === null
? StaticTranslator::$instance->translate($message, $context)
Expand All @@ -35,13 +35,13 @@ public function translate($message, $context = null)
*
* If no translation is found in the specified domain, the translation is also searched for in the default domain.
*
* @param string $domain
* @param string $message
* @param string $context Message context
* @param string $domain
* @param string $message
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
public function translateInDomain($domain, $message, $context = null)
public function translateInDomain(string $domain, string $message, ?string $context = null): string
{
return StaticTranslator::$instance->translateInDomain($domain, $message, $context);
}
Expand All @@ -52,14 +52,14 @@ public function translateInDomain($domain, $message, $context = null)
* The returned message is based on the given number to decide between the singular and plural forms.
* That is also the case if no translation is found.
*
* @param string $singular Singular message
* @param string $plural Plural message
* @param ?int $number Number to decide between the returned singular and plural forms
* @param string $context Message context
* @param string $singular Singular message
* @param string $plural Plural message
* @param ?int $number Number to decide between the returned singular and plural forms
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
public function translatePlural($singular, $plural, $number, $context = null)
public function translatePlural(string $singular, string $plural, ?int $number, ?string $context = null): string
{
return $this->translationDomain === null
? StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context)
Expand All @@ -80,21 +80,26 @@ public function translatePlural($singular, $plural, $number, $context = null)
* The returned message is based on the given number to decide between the singular and plural forms.
* That is also the case if no translation is found.
*
* @param string $domain
* @param string $singular Singular message
* @param string $plural Plural message
* @param ?int $number Number to decide between the returned singular and plural forms
* @param string $context Message context
* @param string $domain
* @param string $singular Singular message
* @param string $plural Plural message
* @param ?int $number Number to decide between the returned singular and plural forms
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null)
{
public function translatePluralInDomain(
string $domain,
string $singular,
string $plural,
?int $number,
?string $context = null
): string {
return StaticTranslator::$instance->translatePluralInDomain(
$domain,
$singular,
$plural,
$number ?? 0,
$number ?? 0,
$context
);
}
Expand Down
18 changes: 9 additions & 9 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
/**
* Translate a message
*
* @param string $message
* @param string $context Message context
* @param string $message
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
function t($message, $context = null)
function t(string $message, ?string $context = null): string
{
return StaticTranslator::$instance->translate($message, $context);
}
Expand All @@ -21,14 +21,14 @@ function t($message, $context = null)
* The returned message is based on the given number to decide between the singular and plural forms.
* That is also the case if no translation is found.
*
* @param string $singular Singular message
* @param string $plural Plural message
* @param int $number Number to decide between the returned singular and plural forms
* @param string $context Message context
* @param string $singular Singular message
* @param string $plural Plural message
* @param ?int $number Number to decide between the returned singular and plural forms
* @param ?string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
function tp($singular, $plural, $number, $context = null)
function tp(string $singular, string $plural, ?int $number, ?string $context = null): string
{
return StaticTranslator::$instance->translatePlural($singular, $plural, $number, $context);
return StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context);
}

0 comments on commit e59c279

Please sign in to comment.