Skip to content

Commit 8ef3dab

Browse files
committed
style: upgrade code with function parameters and return types
1 parent 2624854 commit 8ef3dab

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/gettext-context.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* @param string $message The message being translated
99
* @return string Translated string if one is found in the translation table, or the submitted message if not found
1010
*/
11-
function pgettext($context, $message)
11+
function pgettext(string $context, string $message): string
1212
{
13-
$context_message = "{$context}\004{$message}";
13+
$context_message = "$context\004$message";
1414

1515
$translation = gettext($context_message);
1616

@@ -34,15 +34,15 @@ function pgettext($context, $message)
3434
* @param int $number The number (e.g. item count) to determine the translation for the respective grammatical number
3535
* @return string Correct plural form of message identified by $singular and $plural for $number
3636
*/
37-
function npgettext($context, $singular, $plural, $number)
37+
function npgettext(string $context, string $singular, string $plural, int $number): string
3838
{
39-
$context_singular = "{$context}\004{$singular}";
40-
$context_plural = "{$context}\004{$plural}";
39+
$context_singular = "$context\004$singular";
40+
$context_plural = "$context\004$plural";
4141

4242
$translation = ngettext($context_singular, $context_plural, $number);
4343

4444
// If the translation was not found...
45-
if ($translation === $context_singular or $translation === $context_plural) {
45+
if ($translation === $context_singular || $translation === $context_plural) {
4646
// Use native function to return the appropriate string
4747
return ngettext($singular, $plural, $number);
4848
}
@@ -60,9 +60,9 @@ function npgettext($context, $singular, $plural, $number)
6060
* @param string $message The message being translated
6161
* @return string Translated string if one is found in the translation table, or the submitted message if not found
6262
*/
63-
function dpgettext($domain, $context, $message)
63+
function dpgettext(string $domain, string $context, string $message): string
6464
{
65-
$context_message = "{$context}\004{$message}";
65+
$context_message = "$context\004$message";
6666

6767
$translation = dgettext($domain, $context_message);
6868

@@ -87,15 +87,15 @@ function dpgettext($domain, $context, $message)
8787
* @param int $number The number (e.g. item count) to determine the translation for the respective grammatical number
8888
* @return string Translated string if one is found in the translation table, or the submitted message if not found
8989
*/
90-
function dnpgettext($domain, $context, $singular, $plural, $number)
90+
function dnpgettext(string $domain, string $context, string $singular, string $plural, int $number): string
9191
{
92-
$context_singular = "{$context}\004{$singular}";
93-
$context_plural = "{$context}\004{$plural}";
92+
$context_singular = "$context\004$singular";
93+
$context_plural = "$context\004$plural";
9494

9595
$translation = dngettext($domain, $context_singular, $context_plural, $number);
9696

9797
// If the translation was not found...
98-
if ($translation === $context_singular or $translation === $context_plural) {
98+
if ($translation === $context_singular || $translation === $context_plural) {
9999
// Use native function to return the appropriate string
100100
return dngettext($domain, $singular, $plural, $number);
101101
}

tests/Unit/Test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
}
3131
}
3232

33-
public function test_pgettext()
33+
public function test_pgettext(): void
3434
{
3535
// Test translated
3636
$this->assertEquals('Ime', pgettext('User', 'Name'));
@@ -40,22 +40,22 @@ public function test_pgettext()
4040
$this->assertEquals('Name', pgettext('Country', 'Name'));
4141
}
4242

43-
public function test_npgettext()
43+
public function test_npgettext(): void
4444
{
4545
// Test translated
4646
$this->assertEquals('1 ime', sprintf(npgettext('User', '%d name', '%d names', 1), 1));
4747
$this->assertEquals('2 imeni', sprintf(npgettext('User', '%d name', '%d names', 2), 2));
4848
$this->assertEquals('3 imena', sprintf(npgettext('User', '%d name', '%d names', 3), 3));
4949
$this->assertEquals('5 imen', sprintf(npgettext('User', '%d name', '%d names', 5), 5));
50-
$this->assertEquals('101 ime', sprintf(npgettext('User', '%d name', '%d names', 101), 101));
50+
$this->assertEquals('101 ime', sprintf(npgettext('User', '%d name', '%d names', '101'), 101));
5151

5252
$this->assertEquals('1 naziv', sprintf(npgettext('Product', '%d name', '%d names', 1), 1));
5353

5454
// Test non-translated
5555
$this->assertEquals('1 name', sprintf(npgettext('Country', '%d name', '%d names', 1), 1));
5656
}
5757

58-
public function test_dpgettext()
58+
public function test_dpgettext(): void
5959
{
6060
// Switch to a different domain
6161
textdomain('something');
@@ -70,7 +70,7 @@ public function test_dpgettext()
7070
$this->assertEquals('Name', dpgettext('messages', 'Country', 'Name'));
7171
}
7272

73-
public function test_dnpgettext()
73+
public function test_dnpgettext(): void
7474
{
7575
// Switch to a different domain
7676
textdomain('something');

0 commit comments

Comments
 (0)