Skip to content

Commit b2c3e88

Browse files
committed
better types
1 parent f6041c1 commit b2c3e88

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

src/Facades/Translator.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,30 @@
1212
use Illuminate\Support\Facades\Facade;
1313

1414
/**
15+
* @method static string getJsonNamespace()
1516
* @method static array<int, string> getLocales()
1617
* @method static array<int, string> getNamespaces(string $locale)
17-
* @method static PhpTranslations|JsonTranslations getTranslations(string $locale, string $namespace)
18-
* @method static Collection<int, string> getMissingTranslations(string $source, string $target, string $namespace)
18+
* @method static PhpTranslations|JsonTranslations getTranslations(string $locale, ?string $namespace)
19+
* @method static Collection<int, string> getMissingTranslations(string $source, string $target, ?string $namespace)
1920
* @method static Collection<string, Collection<string, Collection<int, string>>> getAllMissingTranslations(string $source)
20-
* @method static Collection<int, string> getDeadTranslations(string $locale, string $namespace, ?SearchCodeServiceInterface $service = null, ?Closure $progress = null, ?array $ignore = null)
21+
* @method static Collection<int, string> getDeadTranslations(string $locale, ?string $namespace, ?SearchCodeServiceInterface $service = null, ?Closure $progress = null, ?array $ignore = null)
2122
* @method static Collection<string, Collection<string, Collection<int, string>>> getAllDeadTranslations(?Closure $progress = null, ?array $ignore = null)
2223
* @method static array<string, array{ count: int, files: string[] }> getFilesByUsedTranslations(?SearchCodeServiceInterface $service, ?Closure $progress = null)
2324
* @method static array<string, string[]> getUsedTranslationsByFiles(?SearchCodeServiceInterface $service = null, ?Closure $progress = null)
24-
* @method static PhpTranslations|JsonTranslations setTranslations(string $locale, string $namespace, array $values)
25-
* @method static PhpTranslations|JsonTranslations setTranslation(string $locale, string $namespace, string $key, mixed $value)
26-
* @method static PhpTranslations|JsonTranslations translateTranslations(string $source, string $target, string $namespace, array $keys, ?TranslateServiceInterface $service = null)
27-
* @method static PhpTranslations|JsonTranslations translateTranslation(string $source, string $target, string $namespace, string $key, ?TranslateServiceInterface $service = null)
28-
* @method static PhpTranslations|JsonTranslations proofreadTranslations(string $locale, string $namespace, array $keys, ?ProofreadServiceInterface $service = null)
29-
* @method static PhpTranslations|JsonTranslations proofreadTranslation(string $locale, string $namespace, string $key, ?ProofreadServiceInterface $service = null)
30-
* @method static PhpTranslations|JsonTranslations deleteTranslations(string $locale, string $namespace, array $keys)
31-
* @method static PhpTranslations|JsonTranslations deleteTranslation(string $locale, string $namespace, string $key)
32-
* @method static PhpTranslations|JsonTranslations sortTranslations(string $locale, string $namespace)
25+
* @method static PhpTranslations|JsonTranslations setTranslations(string $locale, ?string $namespace, array $values)
26+
* @method static PhpTranslations|JsonTranslations setTranslation(string $locale, ?string $namespace, string $key, mixed $value)
27+
* @method static PhpTranslations|JsonTranslations translateTranslations(string $source, string $target, ?string $namespace, array $keys, ?TranslateServiceInterface $service = null)
28+
* @method static PhpTranslations|JsonTranslations translateTranslation(string $source, string $target, ?string $namespace, string $key, ?TranslateServiceInterface $service = null)
29+
* @method static PhpTranslations|JsonTranslations proofreadTranslations(string $locale, ?string $namespace, array $keys, ?ProofreadServiceInterface $service = null)
30+
* @method static PhpTranslations|JsonTranslations proofreadTranslation(string $locale, ?string $namespace, string $key, ?ProofreadServiceInterface $service = null)
31+
* @method static PhpTranslations|JsonTranslations deleteTranslations(string $locale, ?string $namespace, array $keys)
32+
* @method static PhpTranslations|JsonTranslations deleteTranslation(string $locale, ?string $namespace, string $key)
33+
* @method static PhpTranslations|JsonTranslations sortTranslations(string $locale, ?string $namespace)
3334
* @method static Collection<string, Collection<string, PhpTranslations|JsonTranslations>> sortAllTranslations()
34-
* @method static PhpTranslations|JsonTranslations transformTranslations(string $locale, string $namespace, Closure(PhpTranslations|JsonTranslations $translations):PhpTranslations|JsonTranslations $callback)
35-
* @method static bool saveTranslations(string $locale, string $namespace, PhpTranslations|JsonTranslations $translations)
36-
* @method static string getTranslationsPath(string $locale, string $namespace)
37-
* @method static PhpTranslations|JsonTranslations getNewTranslationsCollection(string $namespace)
35+
* @method static PhpTranslations|JsonTranslations transformTranslations(string $locale, ?string $namespace, Closure(PhpTranslations|JsonTranslations $translations):PhpTranslations|JsonTranslations $callback)
36+
* @method static bool saveTranslations(string $locale, ?string $namespace, PhpTranslations|JsonTranslations $translations)
37+
* @method static string getTranslationsPath(string $locale, ?string $namespace)
38+
* @method static PhpTranslations|JsonTranslations getNewTranslationsCollection(?string $namespace)
3839
* @method static void clearCache()
3940
*
4041
* @see \Elegantly\Translator\Translator

src/Translator.php

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function __construct(
2727
//
2828
}
2929

30+
public function getJsonNamespace(): string
31+
{
32+
return static::JSON_NAMESPACE;
33+
}
34+
3035
/**
3136
* @return string[]
3237
*/
@@ -54,7 +59,7 @@ public function getNamespaces(string $locale): array
5459

5560
public function getTranslations(
5661
string $locale,
57-
?string $namespace = null
62+
?string $namespace
5863
): PhpTranslations|JsonTranslations {
5964
$namespace ??= static::JSON_NAMESPACE;
6065

@@ -76,7 +81,7 @@ public function getTranslations(
7681

7782
protected function getTranslationsFileContent(
7883
string $locale,
79-
?string $namespace = null
84+
?string $namespace
8085
): ?string {
8186
$namespace ??= static::JSON_NAMESPACE;
8287

@@ -95,8 +100,10 @@ protected function getTranslationsFileContent(
95100
public function getMissingTranslations(
96101
string $source,
97102
string $target,
98-
string $namespace,
103+
?string $namespace,
99104
): Collection {
105+
$namespace ??= static::JSON_NAMESPACE;
106+
100107
return $this
101108
->getTranslations($source, $namespace)
102109
->diffTranslationsKeys(
@@ -135,11 +142,12 @@ public function getAllMissingTranslations(
135142
*/
136143
public function getDeadTranslations(
137144
string $locale,
138-
string $namespace,
145+
?string $namespace,
139146
?SearchCodeServiceInterface $service = null,
140147
?Closure $progress = null,
141148
?array $ignore = null,
142149
): Collection {
150+
$namespace ??= static::JSON_NAMESPACE;
143151
$ignoredTranslations = $ignore ?? config('translator.searchcode.ignored_translations', []);
144152

145153
$translationsKeys = $this
@@ -231,9 +239,10 @@ public function getUsedTranslationsByFiles(
231239

232240
public function setTranslations(
233241
string $locale,
234-
string $namespace,
242+
?string $namespace,
235243
array $values
236244
): PhpTranslations|JsonTranslations {
245+
$namespace ??= static::JSON_NAMESPACE;
237246

238247
if (empty($values)) {
239248
return $this->getNewTranslationsCollection($namespace);
@@ -258,10 +267,12 @@ function (PhpTranslations|JsonTranslations $translations) use ($values) {
258267

259268
public function setTranslation(
260269
string $locale,
261-
string $namespace,
270+
?string $namespace,
262271
string $key,
263272
mixed $value,
264273
): PhpTranslations|JsonTranslations {
274+
$namespace ??= static::JSON_NAMESPACE;
275+
265276
return $this->setTranslations($locale, $namespace, [
266277
$key => $value,
267278
]);
@@ -270,10 +281,11 @@ public function setTranslation(
270281
public function translateTranslations(
271282
string $source,
272283
string $target,
273-
string $namespace,
284+
?string $namespace,
274285
array $keys,
275286
?TranslateServiceInterface $service = null,
276287
): PhpTranslations|JsonTranslations {
288+
$namespace ??= static::JSON_NAMESPACE;
277289
$service = $service ?? $this->translateService;
278290

279291
if (! $service) {
@@ -316,10 +328,12 @@ function (PhpTranslations|JsonTranslations $translations) use ($source, $target,
316328
public function translateTranslation(
317329
string $source,
318330
string $target,
319-
string $namespace,
331+
?string $namespace,
320332
string $key,
321333
?TranslateServiceInterface $service = null,
322334
): PhpTranslations|JsonTranslations {
335+
$namespace ??= static::JSON_NAMESPACE;
336+
323337
return $this->translateTranslations(
324338
$source,
325339
$target,
@@ -331,10 +345,11 @@ public function translateTranslation(
331345

332346
public function proofreadTranslations(
333347
string $locale,
334-
string $namespace,
348+
?string $namespace,
335349
array $keys,
336350
?ProofreadServiceInterface $service = null,
337351
): PhpTranslations|JsonTranslations {
352+
$namespace ??= static::JSON_NAMESPACE;
338353
$service = $service ?? $this->proofreadService;
339354

340355
if (! $service) {
@@ -373,10 +388,12 @@ function (PhpTranslations|JsonTranslations $translations) use ($service, $keys)
373388

374389
public function proofreadTranslation(
375390
string $locale,
376-
string $namespace,
391+
?string $namespace,
377392
string $key,
378393
?ProofreadServiceInterface $service = null,
379394
): PhpTranslations|JsonTranslations {
395+
$namespace ??= static::JSON_NAMESPACE;
396+
380397
return $this->proofreadTranslations(
381398
$locale,
382399
$namespace,
@@ -387,9 +404,11 @@ public function proofreadTranslation(
387404

388405
public function deleteTranslations(
389406
string $locale,
390-
string $namespace,
407+
?string $namespace,
391408
array $keys,
392409
): PhpTranslations|JsonTranslations {
410+
$namespace ??= static::JSON_NAMESPACE;
411+
393412
return $this->transformTranslations(
394413
$locale,
395414
$namespace,
@@ -403,18 +422,24 @@ function (PhpTranslations|JsonTranslations $translations) use ($keys) {
403422

404423
public function deleteTranslation(
405424
string $locale,
406-
string $namespace,
425+
?string $namespace,
407426
string $key,
408427
): PhpTranslations|JsonTranslations {
428+
$namespace ??= static::JSON_NAMESPACE;
429+
409430
return $this->deleteTranslations(
410431
$locale,
411432
$namespace,
412433
[$key]
413434
);
414435
}
415436

416-
public function sortTranslations(string $locale, string $namespace): PhpTranslations|JsonTranslations
417-
{
437+
public function sortTranslations(
438+
string $locale,
439+
?string $namespace
440+
): PhpTranslations|JsonTranslations {
441+
$namespace ??= static::JSON_NAMESPACE;
442+
418443
return $this->transformTranslations(
419444
$locale,
420445
$namespace,
@@ -443,9 +468,11 @@ public function sortAllTranslations(): Collection
443468
*/
444469
public function transformTranslations(
445470
string $locale,
446-
string $namespace,
471+
?string $namespace,
447472
Closure $callback,
448473
): PhpTranslations|JsonTranslations {
474+
$namespace ??= static::JSON_NAMESPACE;
475+
449476
$translations = $this->getTranslations($locale, $namespace);
450477
$translations = $callback($translations);
451478

@@ -458,9 +485,11 @@ public function transformTranslations(
458485

459486
public function saveTranslations(
460487
string $locale,
461-
string $namespace,
488+
?string $namespace,
462489
PhpTranslations|JsonTranslations $translations,
463490
): bool {
491+
$namespace ??= static::JSON_NAMESPACE;
492+
464493
return $this->storage->put(
465494
$this->getTranslationsPath($locale, $namespace),
466495
$translations->toFile()
@@ -469,7 +498,7 @@ public function saveTranslations(
469498

470499
public function getTranslationsPath(
471500
string $locale,
472-
?string $namespace = null
501+
?string $namespace
473502
): string {
474503
$namespace ??= static::JSON_NAMESPACE;
475504

@@ -480,8 +509,10 @@ public function getTranslationsPath(
480509
}
481510

482511
public function getNewTranslationsCollection(
483-
string $namespace
512+
?string $namespace
484513
): PhpTranslations|JsonTranslations {
514+
$namespace ??= static::JSON_NAMESPACE;
515+
485516
return match ($namespace) {
486517
static::JSON_NAMESPACE => new JsonTranslations,
487518
default => new PhpTranslations,

tests/Feature/TranslatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
storage: $this->getStorage(),
2828
);
2929

30-
$translations = $translator->getTranslations('fr');
30+
$translations = $translator->getTranslations('fr', null);
3131

3232
expect($translations)->toHaveLength(2);
3333

0 commit comments

Comments
 (0)