generated from fidum/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating a baseline file (#5)
* Add support for generating a baseline file * wip * wip
- Loading branch information
Showing
21 changed files
with
282 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"preset": "laravel", | ||
"exclude": [ | ||
"workbench" | ||
], | ||
"rules": { | ||
"single_line_empty_body": true | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Collections; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class BaselineCollection extends Collection | ||
{ | ||
public function shouldReport(string $locale, string $key): bool | ||
{ | ||
$ignoredKeys = $this->get($locale) ?: []; | ||
|
||
if (in_array($key, $ignoredKeys, true)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Readers; | ||
|
||
use Fidum\LaravelTranslationLinter\Collections\BaselineCollection; | ||
|
||
interface UnusedBaselineFileReader | ||
{ | ||
public function execute(): BaselineCollection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Contracts\Writers; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection; | ||
|
||
interface UnusedBaselineFileWriter | ||
{ | ||
public function execute(ResultObjectCollection $results); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Filters; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Filters\Filter; | ||
use Fidum\LaravelTranslationLinter\Contracts\Readers\UnusedBaselineFileReader; | ||
use Fidum\LaravelTranslationLinter\Data\ResultObject; | ||
|
||
class IgnoreKeysFromUnusedBaselineFileFilter implements Filter | ||
{ | ||
public function __construct(protected UnusedBaselineFileReader $reader) {} | ||
|
||
public function shouldReport(ResultObject $object): bool | ||
{ | ||
return $this->reader | ||
->execute() | ||
->shouldReport($object->locale, $object->namespaceHintedKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Readers; | ||
|
||
use Fidum\LaravelTranslationLinter\Collections\BaselineCollection; | ||
use Fidum\LaravelTranslationLinter\Contracts\Readers\UnusedBaselineFileReader as UnusedBaselineFileReaderContract; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class UnusedBaselineFileReader implements UnusedBaselineFileReaderContract | ||
{ | ||
protected array $decoded = []; | ||
|
||
public function __construct( | ||
protected Filesystem $filesystem, | ||
protected string $file, | ||
) {} | ||
|
||
public function execute(): BaselineCollection | ||
{ | ||
if ($this->decoded) { | ||
return BaselineCollection::wrap($this->decoded); | ||
} | ||
|
||
if ($this->filesystem->exists($this->file)) { | ||
$contents = $this->filesystem->get($this->file); | ||
|
||
$this->decoded = json_decode($contents, true); | ||
} | ||
|
||
return BaselineCollection::wrap($this->decoded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationLinter\Writers; | ||
|
||
use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection; | ||
use Fidum\LaravelTranslationLinter\Contracts\Writers\UnusedBaselineFileWriter as UnusedBaselineFileWriterContract; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class UnusedBaselineFileWriter implements UnusedBaselineFileWriterContract | ||
{ | ||
public function __construct( | ||
protected Filesystem $filesystem, | ||
protected string $file, | ||
) {} | ||
|
||
public function execute(ResultObjectCollection $results) | ||
{ | ||
$this->filesystem->put( | ||
$this->file, | ||
$results->toBaseLineJson(), | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...UnusedCommandTest/it_generates_baseline_file_then_successfully_ignores_baseline_keys.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
INFO Baseline file written with 36 unused translation keys. | ||
|
42 changes: 42 additions & 0 deletions
42
...sedCommandTest/it_generates_baseline_file_then_successfully_ignores_baseline_keys__2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"en": [ | ||
"Unused Vendor PHP Class", | ||
"Unused Vendor Blade File", | ||
"Unused Vendor Vue Component", | ||
"example::example.unused", | ||
"example::example.blade.choice.unused", | ||
"example::example.blade.lang.unused", | ||
"example::example.vue.unused", | ||
"example::folder/example.unused", | ||
"example::folder/example.blade.choice.unused", | ||
"example::folder/example.blade.lang.unused", | ||
"example::folder/example.vue.unused", | ||
"Unused PHP Class", | ||
"Unused Blade File", | ||
"Unused Vue Component", | ||
"example.unused", | ||
"example.blade.choice.unused", | ||
"example.blade.lang.unused", | ||
"example.vue.unused", | ||
"folder/example.unused", | ||
"folder/example.blade.choice.unused", | ||
"folder/example.blade.lang.unused", | ||
"folder/example.vue.unused" | ||
], | ||
"de": [ | ||
"Unused Vendor PHP Class", | ||
"Unused Vendor Blade File", | ||
"Unused Vendor Vue Component", | ||
"Unused PHP Class", | ||
"Unused Blade File", | ||
"Unused Vue Component", | ||
"example.unused", | ||
"example.blade.choice.unused", | ||
"example.blade.lang.unused", | ||
"example.vue.unused", | ||
"folder/example.unused", | ||
"folder/example.blade.choice.unused", | ||
"folder/example.blade.lang.unused", | ||
"folder/example.vue.unused" | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...sedCommandTest/it_generates_baseline_file_then_successfully_ignores_baseline_keys__3.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
INFO No unused translations found! | ||
|
Oops, something went wrong.