Skip to content

Commit

Permalink
refactor(core)!: Translator will not implement `Illuminate\Contract…
Browse files Browse the repository at this point in the history
…s\Translation\Translator` (to have stricter typehints).
  • Loading branch information
LastDragon-ru committed Aug 16, 2023
1 parent 7a9095f commit e63d70d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
34 changes: 17 additions & 17 deletions packages/core/src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Special wrapper around Translator to help translate package's messages.
*/
abstract class Translator implements TranslatorContract {
abstract class Translator {
public function __construct(
protected TranslatorContract $translator,
protected string $package,
Expand All @@ -24,25 +24,26 @@ public function __construct(
}

/**
* @inheritDoc
*
* @param array<string>|string $key
* @param array<mixed> $replace
* @param list<string>|string $key
* @param array<string, mixed> $replace
*/
public function get($key, array $replace = [], $locale = null): string {
public function get(array|string $key, array $replace = [], string $locale = null): string {
return $this->translate($key, function (string $key) use ($replace, $locale): string {
return Cast::toString($this->translator->get($this->key($key), $replace, $locale));
});
}

/**
* @inheritDoc
*
* @param array<string>|string $key
* @param Countable|int|array<mixed> $number
* @param array<mixed> $replace
* @param list<string>|string $key
* @param Countable|int|array<array-key, mixed> $number
* @param array<string, mixed> $replace
*/
public function choice($key, $number, array $replace = [], $locale = null): string {
public function choice(
array|string $key,
Countable|array|int $number,
array $replace = [],
string $locale = null,
): string {
return $this->translate($key, function (string $key) use ($number, $replace, $locale): string {
return $this->translator->choice($this->key($key), $number, $replace, $locale);
});
Expand All @@ -52,11 +53,10 @@ public function getLocale(): string {
return $this->translator->getLocale();
}

/**
* @inheritDoc
*/
public function setLocale($locale) {
public function setLocale(string $locale): static {
$this->translator->setLocale($locale);

return $this;
}

protected function key(string $key): string {
Expand All @@ -66,7 +66,7 @@ protected function key(string $key): string {
}

/**
* @param array<string>|string $variants
* @param list<string>|string $variants
* @param Closure(string): string $callback
*/
protected function translate(array|string $variants, Closure $callback): string {
Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class TranslatorTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
/**
* @param array<string>|string $key
* @param TranslationsFactory $translations
* @param array<mixed> $replace
*
* @dataProvider dataProviderChoice
*
* @param list<string>|string $key
* @param TranslationsFactory $translations
* @param array<string, mixed> $replace
*/
public function testChoice(
string $expected,
Expand All @@ -43,11 +43,11 @@ public function testChoice(
}

/**
* @param array<string>|string $key
* @param TranslationsFactory $translations
* @param array<mixed> $replace
*
* @dataProvider dataProviderGet
*
* @param list<string>|string $key
* @param TranslationsFactory $translations
* @param array<string, mixed> $replace
*/
public function testGet(
string $expected,
Expand All @@ -70,7 +70,7 @@ public function testGet(
// <editor-fold desc="DataProviders">
// =========================================================================
/**
* @return array<string,array{string, TranslationsFactory, array<string>|string, array<string,string>, ?string}>
* @return array<string,array{string, TranslationsFactory, list<string>|string, array<string,string>, ?string}>
*/
public static function dataProviderGet(): array {
return [
Expand Down Expand Up @@ -132,7 +132,7 @@ static function (TestCase $test, string $currentLocale, string $fallbackLocale):
}

/**
* @return array<string,array{string,TranslationsFactory,array<string>|string,int,array<string,string>,?string}>
* @return array<string,array{string,TranslationsFactory,list<string>|string,int,array<string,string>,?string}>
*/
public static function dataProviderChoice(): array {
return [
Expand Down
2 changes: 1 addition & 1 deletion packages/formatter/src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected function getLocaleOptions(string $type, string $option): mixed {
}

/**
* @param array<string>|string $key
* @param list<string>|string $key
* @param array<string, mixed> $replace
*/
protected function getTranslation(array|string $key, array $replace = []): string {
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ parameters:
count: 1
path: packages/formatter/src/Formatter.php

-
message: "#^Method LastDragon_ru\\\\LaraASP\\\\Formatter\\\\Formatter\\:\\:getTranslation\\(\\) should return string but returns \\(array\\|string\\)\\.$#"
count: 1
path: packages/formatter/src/Formatter.php

-
message: "#^Parameter \\#1 \\$builder of method LastDragon_ru\\\\LaraASP\\\\GraphQL\\\\SortBy\\\\Builders\\\\Eloquent\\\\Builder\\:\\:handle\\(\\) expects Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<Illuminate\\\\Database\\\\Eloquent\\\\Model\\>, Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\|Illuminate\\\\Database\\\\Query\\\\Builder given\\.$#"
count: 1
Expand Down

0 comments on commit e63d70d

Please sign in to comment.