Skip to content

Commit 8b5a372

Browse files
authored
Add support for generating a baseline file (#5)
* Add support for generating a baseline file * wip * wip
1 parent 8e578d5 commit 8b5a372

21 files changed

+282
-9
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ $ php artisan translation:unused
5252
+--------+----------------------+-----------------------------------------------+
5353
```
5454

55+
You can generate a baseline file which will be used to ignore specific keys with the
56+
`--generate-baseline` or `-b` command options:
57+
58+
```sh
59+
$ php artisan translation:unused --generate-baseline
60+
61+
INFO Baseline file written with 5 unused translation keys.
62+
63+
$ php artisan translation:unused
64+
65+
INFO No unused translations found!
66+
```
67+
5568
## Roadmap
5669
- [x] Supports JSON and PHP translation files
5770
- You can enable / disable file types in the config

config/translation-linter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@
8888
],
8989

9090
'unused' => [
91+
/*
92+
|--------------------------------------------------------------------------
93+
| Baseline file
94+
|--------------------------------------------------------------------------
95+
|
96+
| This is the location of the baseline file that is used to ignore specific
97+
| translation keys. You can generate this file by using the `--generate-baseline`
98+
| option when running the command.
99+
|
100+
*/
101+
'baseline' => base_path('translations.unused.baseline.json'),
102+
91103
/*
92104
|--------------------------------------------------------------------------
93105
| Output Fields

pint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"preset": "laravel",
3+
"exclude": [
4+
"workbench"
5+
],
36
"rules": {
47
"single_line_empty_body": true
58
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Fidum\LaravelTranslationLinter\Collections;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class BaselineCollection extends Collection
8+
{
9+
public function shouldReport(string $locale, string $key): bool
10+
{
11+
$ignoredKeys = $this->get($locale) ?: [];
12+
13+
if (in_array($key, $ignoredKeys, true)) {
14+
return false;
15+
}
16+
17+
return true;
18+
}
19+
}

src/Collections/ResultObjectCollection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ public function reset(): void
1616
$this->items = [];
1717
}
1818

19+
public function toBaseLineJson(): string
20+
{
21+
return $this
22+
->groupBy('locale')
23+
->map(fn (ResultObjectCollection $collection) => $collection->pluck('namespaceHintedKey')->values())
24+
->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
25+
}
26+
1927
public function toCommandTableOutputArray(FieldCollectionContract $fields): array
2028
{
2129
$only = $fields->enabled()->toArray();

src/Commands/UnusedCommand.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,39 @@
55
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFieldCollection;
66
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFilterCollection;
77
use Fidum\LaravelTranslationLinter\Contracts\Linters\UnusedTranslationLinter;
8+
use Fidum\LaravelTranslationLinter\Filters\IgnoreKeysFromUnusedBaselineFileFilter;
9+
use Fidum\LaravelTranslationLinter\Writers\UnusedBaselineFileWriter;
810
use Illuminate\Console\Command;
911

1012
class UnusedCommand extends Command
1113
{
12-
public $signature = 'translation:unused';
14+
public $signature = 'translation:unused
15+
{--b|generate-baseline : Generate a baseline file from the unused keys.}';
1316

1417
public $description = 'Finds unused language keys.';
1518

1619
public function handle(
20+
UnusedBaselineFileWriter $writer,
1721
UnusedFieldCollection $fields,
1822
UnusedFilterCollection $filters,
1923
UnusedTranslationLinter $linter,
2024
): int {
21-
$results = $linter->execute()->whereShouldReport($filters);
25+
$baseline = (bool) $this->option('generate-baseline');
26+
$results = $linter->execute();
27+
28+
if ($baseline) {
29+
$results = $results->whereShouldReport($filters);
30+
31+
$writer->execute($results);
32+
33+
$this->components->info("Baseline file written with {$results->count()} unused translation keys.");
34+
35+
return self::SUCCESS;
36+
}
37+
38+
$filters->push(IgnoreKeysFromUnusedBaselineFileFilter::class);
39+
40+
$results = $results->whereShouldReport($filters);
2241

2342
if ($results->isEmpty()) {
2443
$this->components->info('No unused translations found!');

src/Contracts/Collections/FilterCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88

99
interface FilterCollection extends Arrayable, Enumerable
1010
{
11+
public function push(...$values);
12+
1113
public function shouldReport(ResultObject $object): bool;
1214
}

src/Contracts/Collections/ResultObjectCollection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface ResultObjectCollection extends Arrayable, Enumerable
1616
{
1717
public function reset(): void;
1818

19+
public function toBaseLineJson(): string;
20+
1921
public function toCommandTableOutputArray(FieldCollectionContract $fields): array;
2022

2123
public function whereShouldReport(FilterCollectionContract $filters): self;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Fidum\LaravelTranslationLinter\Contracts\Readers;
4+
5+
use Fidum\LaravelTranslationLinter\Collections\BaselineCollection;
6+
7+
interface UnusedBaselineFileReader
8+
{
9+
public function execute(): BaselineCollection;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Fidum\LaravelTranslationLinter\Contracts\Writers;
4+
5+
use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection;
6+
7+
interface UnusedBaselineFileWriter
8+
{
9+
public function execute(ResultObjectCollection $results);
10+
}

0 commit comments

Comments
 (0)