Skip to content

Commit 752a69f

Browse files
committed
support @lang and Lang
1 parent 729f547 commit 752a69f

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

src/Collections/JsonTranslations.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@ public function sortNatural(): static
1919
return $this->sortKeys(SORT_NATURAL);
2020
}
2121

22-
public function toDotTranslations(): Collection
22+
public function toDotTranslations(bool $filter = false): Collection
2323
{
2424
return $this
2525
->toBase()
26-
->filter(fn ($value) => ! blank($value));
26+
->when(
27+
$filter,
28+
fn ($c) => $c->filter(fn ($value) => ! blank($value))
29+
);
2730
}
2831

29-
public function toTranslationsKeys(): Collection
32+
public function toTranslationsKeys(bool $filter = false): Collection
3033
{
31-
return $this->toDotTranslations()->keys();
34+
return $this->toDotTranslations($filter)->keys();
3235
}
3336

34-
public function toTranslationsValues(): Collection
37+
public function toTranslationsValues(bool $filter = false): Collection
3538
{
36-
return $this->toDotTranslations()->values();
39+
return $this->toDotTranslations($filter)->values();
3740
}
3841

3942
public function diffTranslationsKeys(Collection $translations): Collection

src/Collections/PhpTranslations.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,28 @@ protected function recursiveSortNatural(array $items): array
6565
return $items;
6666
}
6767

68-
public function toDotTranslations(): Collection
68+
public function toDotTranslations(bool $filter = false): Collection
6969
{
7070
/**
7171
* Filtering the array prevent incoherent values such as such as 'key' => []
7272
*/
7373
return $this
7474
->dot()
7575
->toBase()
76-
->filter(fn ($value) => ! blank($value));
76+
->when(
77+
$filter,
78+
fn ($c) => $c->filter(fn ($value) => ! blank($value))
79+
);
7780
}
7881

79-
public function toTranslationsKeys(): Collection
82+
public function toTranslationsKeys(bool $filter = false): Collection
8083
{
81-
return $this->toDotTranslations()->keys();
84+
return $this->toDotTranslations($filter)->keys();
8285
}
8386

84-
public function toTranslationsValues(): Collection
87+
public function toTranslationsValues(bool $filter = false): Collection
8588
{
86-
return $this->toDotTranslations()->values();
89+
return $this->toDotTranslations($filter)->values();
8790
}
8891

8992
public function diffTranslationsKeys(Collection $translations): Collection

src/Collections/TranslationsInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public function except($keys);
1818

1919
public function sortNatural(): static;
2020

21-
public function toDotTranslations(): Collection;
21+
public function toDotTranslations(bool $filter = false): Collection;
2222

23-
public function toTranslationsKeys(): Collection;
23+
public function toTranslationsKeys(bool $filter = false): Collection;
2424

25-
public function toTranslationsValues(): Collection;
25+
public function toTranslationsValues(bool $filter = false): Collection;
2626

2727
public function diffTranslationsKeys(Collection $translations): Collection;
2828

src/Services/SearchCode/PhpParserService.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
use Elegantly\Translator\Caches\SearchCodeCache;
77
use Illuminate\Contracts\Filesystem\Filesystem;
88
use Illuminate\Support\Facades\Blade;
9+
use Illuminate\Support\Facades\Lang;
910
use PhpParser\Node;
1011
use PhpParser\Node\Expr\FuncCall;
12+
use PhpParser\Node\Expr\MethodCall;
13+
use PhpParser\Node\Expr\StaticCall;
1114
use PhpParser\Node\Name;
1215
use PhpParser\Node\Scalar\String_;
1316
use PhpParser\NodeFinder;
@@ -50,6 +53,32 @@ public function finder(): Finder
5053
->files();
5154
}
5255

56+
public static function isFunCallTo(
57+
FuncCall $node,
58+
string $function,
59+
string $argName,
60+
int $argPosition,
61+
string $argValue
62+
) {
63+
if ($node->name instanceof Name && $node->name->name !== $function) {
64+
return false;
65+
}
66+
67+
$args = collect($node->getArgs());
68+
69+
if ($args->isEmpty()) {
70+
return false;
71+
}
72+
73+
$arg = $args->firstWhere('name.name', $argName) ?? $args->get($argPosition);
74+
75+
if ($arg->value instanceof String_) {
76+
return $arg->value->value === $argValue;
77+
}
78+
79+
return false;
80+
}
81+
5382
/**
5483
* @return string[] All translations keys used in the code
5584
*/
@@ -64,6 +93,19 @@ public static function scanCode(string $code): array
6493

6594
/** @var FuncCall[] $results */
6695
$results = $nodeFinder->find($ast, function (Node $node) {
96+
97+
if (
98+
$node instanceof MethodCall &&
99+
$node->var instanceof FuncCall &&
100+
static::isFunCallTo($node->var, 'app', 'abstract', 0, 'translator')
101+
) {
102+
return in_array($node->name->name, ['get']);
103+
}
104+
105+
if ($node instanceof StaticCall && $node->class->name === Lang::class) {
106+
return in_array($node->name->name, ['get', 'has', 'hasForLocale', 'choice']);
107+
}
108+
67109
if ($node instanceof FuncCall && $node->name instanceof Name) {
68110
return in_array($node->name->name, ['__', 'trans', 'trans_choice']);
69111
}
@@ -72,8 +114,8 @@ public static function scanCode(string $code): array
72114
});
73115

74116
return collect($results)
75-
->map(function (FuncCall $funcCall) {
76-
$args = collect($funcCall->getArgs());
117+
->map(function (FuncCall|StaticCall|MethodCall $node) {
118+
$args = collect($node->getArgs());
77119
$argKey = $args->firstWhere('name.name', 'key') ?? $args->first();
78120
$value = $argKey->value;
79121

tests/Unit/PhpParserServiceTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

33
use Elegantly\Translator\Services\SearchCode\PhpParserService;
4+
use Illuminate\Support\Facades\Blade;
45
use Illuminate\Support\Facades\Storage;
56

6-
it('finds all occurences of __ in code', function (string $code) {
7+
it('finds all occurences of __ in php code', function (string $code) {
78
$results = PhpParserService::scanCode($code);
89

910
expect($results)->toHaveLength(1);
@@ -15,6 +16,21 @@
1516
"<?php __('messages.dummy.class', [], 'en');",
1617
"<?php __(key: 'messages.dummy.class');",
1718
"<?php __(key: 'messages.dummy.class', replace: [], locale: 'en');",
19+
"<?php \Illuminate\Support\Facades\Lang::get(key: 'messages.dummy.class');",
20+
"<?php \Illuminate\Support\Facades\Lang::has(key: 'messages.dummy.class');",
21+
"<?php \Illuminate\Support\Facades\Lang::hasForLocale(key: 'messages.dummy.class');",
22+
"<?php \Illuminate\Support\Facades\Lang::choice(key: 'messages.dummy.class');",
23+
]);
24+
25+
it('finds all occurences of __ in blade code', function (string $code) {
26+
$results = PhpParserService::scanCode(Blade::compileString($code));
27+
28+
expect($results)->toHaveLength(1);
29+
})->with([
30+
"{{ __('messages.dummy.class') }}",
31+
"{{ trans('messages.dummy.class') }}",
32+
"{{ \Illuminate\Support\Facades\Lang::get('messages.dummy.class') }}",
33+
"@lang('messages.dummy.class')",
1834
]);
1935

2036
it('gets all the translations keys grouped by files', function () {

0 commit comments

Comments
 (0)