Skip to content

Commit c1a986e

Browse files
committed
deadcode analysis
1 parent be2b185 commit c1a986e

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Translator.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ public function getDeadTranslations(
124124
return $definedTranslationsKeys->filter(fn (string $key) => ! in_array("{$namespace}.{$key}", $usedTranslationsKeys))->toArray();
125125
}
126126

127+
/**
128+
* @return array<string, array<string, string[]>>
129+
*/
130+
public function getAllDeadTranslations(): array
131+
{
132+
return collect($this->getLocales())
133+
->mapWithKeys(function (string $locale) {
134+
$namespaces = collect($this->getNamespaces($locale));
135+
136+
return [
137+
$locale => $namespaces
138+
->mapWithKeys(fn (string $namespace) => [
139+
$namespace => $this->getDeadTranslations($locale, $namespace),
140+
])
141+
->filter(),
142+
];
143+
})
144+
->filter()
145+
->toArray();
146+
}
147+
127148
/**
128149
* @param array<string|int, string|int|float|array|null> $values
129150
*/

tests/Feature/TranslatorTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
]);
8686
});
8787

88-
it('finds dead translations', function () {
88+
it('finds dead translations in a namespace', function () {
8989
$translator = new Translator(
9090
storage: $this->getStorage(),
9191
searchcodeService: new PhpParserService([
@@ -110,6 +110,41 @@
110110
]);
111111
});
112112

113+
it('finds all dead translations', function () {
114+
$translator = new Translator(
115+
storage: $this->getStorage(),
116+
searchcodeService: new PhpParserService([
117+
$this->getAppPath(),
118+
$this->getResourcesPath(),
119+
])
120+
);
121+
122+
$deadTranslations = $translator->getAllDeadTranslations();
123+
124+
expect($deadTranslations)->toBe([
125+
'en' => [
126+
'messages' => [
127+
'hello',
128+
'add',
129+
'home.title',
130+
'home.end',
131+
'empty',
132+
],
133+
],
134+
'fr' => [
135+
'messages' => [
136+
'hello',
137+
'add',
138+
'home.title',
139+
'home.end',
140+
'home.missing',
141+
'empty',
142+
'missing',
143+
],
144+
],
145+
]);
146+
});
147+
113148
it('sets translations', function () {
114149
$translator = new Translator(
115150
storage: $this->getStorage(),

0 commit comments

Comments
 (0)