Skip to content

Commit

Permalink
crude key exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsWiegers committed Oct 2, 2023
1 parent fdffde0 commit e049315
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\File as FileFacade;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\Features\DirectoryExclusion;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\Features\GetLanguages;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\Features\KeyExclusion;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\Features\LanguagesWithMissingFiles;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\Features\LanguagesWithMissingKeys;
use Larswiegers\LaravelTranslationsChecker\Console\Domain\File;
Expand All @@ -20,7 +21,7 @@ class CheckIfTranslationsAreAllThereCommand extends Command
*
* @var string
*/
protected $signature = 'translations:check {--directory=} {--excludedDirectories=config}';
protected $signature = 'translations:check {--directory=} {--excludedDirectories=config} {--excludedKeys=config}';

/**
* The console command description.
Expand Down Expand Up @@ -66,6 +67,7 @@ public function handle(): int
}

DirectoryExclusion::getExcludedDirectories($this->options());
KeyExclusion::getExcludedKeys($this->options());

$languages = $this->getLanguages->getLanguages($topDirectory);

Expand Down
35 changes: 35 additions & 0 deletions src/Console/Domain/Features/KeyExclusion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Larswiegers\LaravelTranslationsChecker\Console\Domain\Features;

use Larswiegers\LaravelTranslationsChecker\Console\Domain\Line;

class KeyExclusion
{

/**
* @var array|false|string[]
*/
private static array $excludedKeys = [];

public static function shouldExclude(Line $line): bool
{
return in_array($line->getKey(), self::$excludedKeys);
}

/**
* @param array<string> $options
*/
public static function getExcludedKeys(array $options): void
{
if ($options['excludedKeys'] === 'config') {
self::$excludedKeys = (array) config('translations-checker.excluded_keys', []);
} elseif ($options['excludedKeys']) {
self::$excludedKeys = explode(',', $options['excludedKeys']);
} elseif (empty((array) config('translations-checker.excluded_keys', []))) {
self::$excludedKeys = (array) config('translations-checker.excluded_keys', []);
} else {
self::$excludedKeys = [];
}
}
}
4 changes: 4 additions & 0 deletions src/Console/Domain/Features/LanguagesWithMissingKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public function getMissingKeysTexts(array $realLines, array $languages): void
continue;
}

if(KeyExclusion::shouldExclude($line)) {
continue;
}

$file = new File($fileKey);
$fileName = $file->withoutExtensionAndLanguages($languages);

Expand Down
38 changes: 38 additions & 0 deletions tests/Tests/Unit/Console/Commands/CheckExcludeKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Console\Commands;

use Tests\TestCase;

class CheckExcludeKeysTest extends TestCase
{
private string $exclusionDir = 'tests/resources/lang/excluded_keys';

public function setUp(): void
{
parent::setUp();
}

public function testItWorksFineIfKeyExistButIsExcluded()
{
$command = $this->artisan('translations:check', [
'--directory' => $this->exclusionDir .'/excluded_but_existing',
'--excludedKeys' => 'test.existing_key',
]);

$command->assertExitCode(0);
}

public function testItWorksFineIfKeyIsMissingButIsExcluded()
{
config()->set('translations-checker.excluded_keys', null);


$command = $this->artisan('translations:check', [
'--directory' => $this->exclusionDir .'/excluded_but_missing',
'--excludedKeys' => 'existing_key',
]);

$command->assertExitCode(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'existing_key' => 'existing_key',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'existing_key' => 'existing_key',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'existing_key' => 'existing_key',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

return [
];

0 comments on commit e049315

Please sign in to comment.