From 5c3ec4634fe5837313ba52d8ac2f3d4ff432093c Mon Sep 17 00:00:00 2001 From: dmason30 Date: Fri, 6 Oct 2023 01:02:36 +0100 Subject: [PATCH] wip --- config/translation-linter.php | 12 ---- src/Contracts/Extractors/Extractor.php | 10 --- .../Readers/ApplicationFileReader.php | 10 +++ src/Contracts/Readers/LanguageFileReader.php | 10 +++ src/Finders/ApplicationFileFinder.php | 2 +- src/Finders/LanguageFileFinder.php | 2 +- src/Finders/LanguageNamespaceFinder.php | 2 +- ...aravelTranslationLinterServiceProvider.php | 48 +++++++------- src/Linters/UnusedTranslationLinter.php | 62 +++++++------------ src/Parsers/Parser.php | 10 ++- .../ApplicationFileReader.php} | 6 +- src/Readers/LanguageFileReader.php | 25 ++++++++ 12 files changed, 105 insertions(+), 94 deletions(-) delete mode 100644 src/Contracts/Extractors/Extractor.php create mode 100644 src/Contracts/Readers/ApplicationFileReader.php create mode 100644 src/Contracts/Readers/LanguageFileReader.php rename src/{Extractors/Extractor.php => Readers/ApplicationFileReader.php} (74%) create mode 100644 src/Readers/LanguageFileReader.php diff --git a/config/translation-linter.php b/config/translation-linter.php index d65e31c..fa4aeda 100644 --- a/config/translation-linter.php +++ b/config/translation-linter.php @@ -57,18 +57,6 @@ 'Lang::has', ], - /* - |-------------------------------------------------------------------------- - | Language Function Regex Pattern - |-------------------------------------------------------------------------- - | - | The following contains the regex pattern used to find the functions - | configured above. The '[FUNCTIONS]' part will be replaced with a - | pipe delimited list of the functions defined above. - | - */ - 'regex' => '/([FUNCTIONS])\([\t\r\n\s]*[\'"](.+)[\'"][\),\t\r\n\s]/U', - /* |-------------------------------------------------------------------------- | Language Locales diff --git a/src/Contracts/Extractors/Extractor.php b/src/Contracts/Extractors/Extractor.php deleted file mode 100644 index 60a3eab..0000000 --- a/src/Contracts/Extractors/Extractor.php +++ /dev/null @@ -1,10 +0,0 @@ -app->bind(ApplicationFileFinderContract::class, function (Application $app) { - return new ApplicationFileFinder( - $app->make('files'), - $app->make('config')->get('translation-linter.application.directories'), - $app->make('config')->get('translation-linter.application.extensions'), - ); - }); + $this->app->bind(ApplicationFileFinderContract::class, ApplicationFileFinder::class); + + $this->app->when(ApplicationFileFinder::class) + ->needs('$directories') + ->giveConfig('translation-linter.application.directories'); - $this->app->bind(ExtractorContract::class, Extractor::class); + $this->app->when(ApplicationFileFinder::class) + ->needs('$extensions') + ->giveConfig('translation-linter.application.extensions'); + + $this->app->bind(ApplicationFileReaderContract::class, ApplicationFileReader::class); $this->app->bind(LanguageFileFinderContract::class, LanguageFileFinder::class); + $this->app->bind(LanguageFileReaderContract::class, LanguageFileReader::class); $this->app->bind(LanguageNamespaceFinderContract::class, LanguageNamespaceFinder::class); - $this->app->bind(ParserContract::class, function (Application $app) { - $regex = $app->make('config')->get('translation-linter.lang.regex'); - $functions = $app->make('config')->get('translation-linter.lang.functions'); - - return new Parser(str_replace('[FUNCTIONS]', implode('|', $functions), $regex)); - }); + $this->app->bind(ParserContract::class, Parser::class); + $this->app->when(Parser::class) + ->needs('$functions') + ->giveConfig('translation-linter.lang.functions'); $this->app->bind(UnusedFieldCollectionContract::class, function (Application $app) { return UnusedFieldCollection::wrap($app->make('config')->get('translation-linter.unused.fields')); @@ -66,20 +69,19 @@ public function registeringPackage() }); $this->app->bind(UnusedResultCollectionContract::class, UnusedResultCollection::class); - $this->app->bind(UnusedTranslationLinterContract::class, function (Application $app) { - $linter = $app->make(UnusedTranslationLinter::class); - $languages = $app->make('config')->get('translation-linter.lang.locales'); - - return $linter->withLanguages($languages); - }); + $this->app->bind(UnusedTranslationLinterContract::class, UnusedTranslationLinter::class); + $this->app->when(UnusedTranslationLinter::class) + ->needs('$languages') + ->giveConfig('translation-linter.lang.locales'); } public function provides() { return [ ApplicationFileFinderContract::class, - ExtractorContract::class, + ApplicationFileReaderContract::class, LanguageFileFinderContract::class, + LanguageFileReaderContract::class, LanguageNamespaceFinderContract::class, ParserContract::class, UnusedFieldCollectionContract::class, diff --git a/src/Linters/UnusedTranslationLinter.php b/src/Linters/UnusedTranslationLinter.php index 510c864..fd2b51f 100644 --- a/src/Linters/UnusedTranslationLinter.php +++ b/src/Linters/UnusedTranslationLinter.php @@ -4,47 +4,49 @@ use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageFileFinder; use Fidum\LaravelTranslationLinter\Contracts\Linters\UnusedTranslationLinter as UnusedTranslationLinterContract; -use Fidum\LaravelTranslationLinter\Extractors\Extractor; use Fidum\LaravelTranslationLinter\Finders\LanguageNamespaceFinder; -use http\Exception\InvalidArgumentException; +use Fidum\LaravelTranslationLinter\Readers\ApplicationFileReader; +use Fidum\LaravelTranslationLinter\Readers\LanguageFileReader; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Stringable; use Symfony\Component\Finder\SplFileInfo; -class UnusedTranslationLinter implements UnusedTranslationLinterContract +readonly class UnusedTranslationLinter implements UnusedTranslationLinterContract { public function __construct( - protected Extractor $extractor, - protected LanguageFileFinder $finder, + protected ApplicationFileReader $used, + protected LanguageFileFinder $files, + protected LanguageFileReader $translations, protected LanguageNamespaceFinder $namespaces, - protected array $languages = ['en'], + protected array $languages, ) { } public function execute(): Collection { - $unusedStrings = []; - $usedStrings = $this->extractor->execute()->toArray(); - $registeredNamespaces = $this->namespaces->execute(); + $unused = []; + $used = $this->used->execute(); + $namespaces = $this->namespaces->execute(); foreach ($this->languages as $language) { - $unusedStrings[$language] = []; + $unused[$language] = []; - foreach ($registeredNamespaces as $namespace => $path) { - $unusedStrings[$language][$namespace] = []; + foreach ($namespaces as $namespace => $path) { + $unused[$language][$namespace] = []; // TODO: Support json files - $files = $this->finder->execute($path, ['php']); + $files = $this->files->execute($path, ['php']); /** @var SplFileInfo $file */ foreach ($files as $file) { - $translations = $this->getTranslationsFromFile($file); + $translations = $this->translations->execute($file); - foreach ($translations as $field => $value) { + foreach ($translations as $field => $children) { $group = $this->getLanguageKey($file, $language, $field); - foreach (Arr::dot(Arr::wrap($value)) as $key => $val) { + + foreach (Arr::dot(Arr::wrap($children)) as $key => $value) { $groupedKey = Str::of($group) ->when(is_string($key), fn (Stringable $str) => $str->append(".$key")) ->toString(); @@ -54,8 +56,8 @@ public function execute(): Collection ->append($groupedKey) ->toString(); - if (! in_array($namespacedKey, $usedStrings)) { - $unusedStrings[$language][$namespace][$groupedKey] = $val; + if ($used->doesntContain($namespacedKey)) { + $unused[$language][$namespace][$groupedKey] = $value; } } } @@ -63,14 +65,7 @@ public function execute(): Collection } } - return new Collection($unusedStrings); - } - - public function withLanguages(array $languages): self - { - $this->languages = $languages; - - return $this; + return new Collection($unused); } protected function getLanguageKey(SplFileInfo $file, string $language, string $key): string @@ -87,19 +82,4 @@ protected function getLanguageKey(SplFileInfo $file, string $language, string $k ->append($key) ->toString(); } - - protected function getTranslationsFromFile(SplFileInfo $file): array - { - $translations = include $file->getPathname(); - - if ($file->getExtension() === 'json') { - $translations = json_decode($translations, true); - } - - if (! is_array($translations)) { - throw new InvalidArgumentException("Unable to extract an array from {$file->getPathname()}!"); - } - - return $translations; - } } diff --git a/src/Parsers/Parser.php b/src/Parsers/Parser.php index d252735..caa0e8b 100644 --- a/src/Parsers/Parser.php +++ b/src/Parsers/Parser.php @@ -6,10 +6,16 @@ use Illuminate\Support\Collection; use Symfony\Component\Finder\SplFileInfo; -class Parser implements ParserContract +readonly class Parser implements ParserContract { - public function __construct(private string $pattern) + protected string $regex; + + protected string $pattern; + + public function __construct(array $functions) { + $this->regex = '/([FUNCTIONS])\([\t\r\n\s]*[\'"](.+)[\'"][\),\t\r\n\s]/U'; + $this->pattern = str_replace('[FUNCTIONS]', implode('|', $functions), $this->regex); } public function execute(SplFileInfo $file): Collection diff --git a/src/Extractors/Extractor.php b/src/Readers/ApplicationFileReader.php similarity index 74% rename from src/Extractors/Extractor.php rename to src/Readers/ApplicationFileReader.php index 458da71..6d863db 100644 --- a/src/Extractors/Extractor.php +++ b/src/Readers/ApplicationFileReader.php @@ -1,13 +1,13 @@ getPathname(); + + if ($file->getExtension() === 'json') { + $translations = json_decode($translations, true); + } + + if (! is_array($translations)) { + throw new InvalidArgumentException("Unable to extract an array from {$file->getPathname()}!"); + } + + return $translations; + } +}