Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Oct 6, 2023
1 parent 814b08a commit 5c3ec46
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 94 deletions.
12 changes: 0 additions & 12 deletions config/translation-linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions src/Contracts/Extractors/Extractor.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Contracts/Readers/ApplicationFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fidum\LaravelTranslationLinter\Contracts\Readers;

use Illuminate\Support\Collection;

interface ApplicationFileReader
{
public function execute(): Collection;
}
10 changes: 10 additions & 0 deletions src/Contracts/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fidum\LaravelTranslationLinter\Contracts\Readers;

use Symfony\Component\Finder\SplFileInfo;

interface LanguageFileReader
{
public function execute(SplFileInfo $file): array;
}
2 changes: 1 addition & 1 deletion src/Finders/ApplicationFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

class ApplicationFileFinder implements ApplicationFileFinderContract
readonly class ApplicationFileFinder implements ApplicationFileFinderContract
{
public function __construct(
protected Filesystem $filesystem,
Expand Down
2 changes: 1 addition & 1 deletion src/Finders/LanguageFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

class LanguageFileFinder implements LanguageFileFinderContract
readonly class LanguageFileFinder implements LanguageFileFinderContract
{
public function __construct(protected Filesystem $filesystem)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Finders/LanguageNamespaceFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Support\Collection;

class LanguageNamespaceFinder implements LanguageNamespaceFinderContract
readonly class LanguageNamespaceFinder implements LanguageNamespaceFinderContract
{
public function __construct(protected Translator $translator)
{
Expand Down
48 changes: 25 additions & 23 deletions src/LaravelTranslationLinterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFieldCollection as UnusedFieldCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFilterCollection as UnusedFilterCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedResultCollection as UnusedResultCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Extractors\Extractor as ExtractorContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\ApplicationFileFinder as ApplicationFileFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageFileFinder as LanguageFileFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageNamespaceFinder as LanguageNamespaceFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Linters\UnusedTranslationLinter as UnusedTranslationLinterContract;
use Fidum\LaravelTranslationLinter\Contracts\Parsers\Parser as ParserContract;
use Fidum\LaravelTranslationLinter\Extractors\Extractor;
use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader as ApplicationFileReaderContract;
use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader as LanguageFileReaderContract;
use Fidum\LaravelTranslationLinter\Finders\ApplicationFileFinder;
use Fidum\LaravelTranslationLinter\Finders\LanguageFileFinder;
use Fidum\LaravelTranslationLinter\Finders\LanguageNamespaceFinder;
use Fidum\LaravelTranslationLinter\Linters\UnusedTranslationLinter;
use Fidum\LaravelTranslationLinter\Parsers\Parser;
use Fidum\LaravelTranslationLinter\Readers\ApplicationFileReader;
use Fidum\LaravelTranslationLinter\Readers\LanguageFileReader;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Foundation\Application;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -38,24 +40,25 @@ public function configurePackage(Package $package): void

public function registeringPackage()
{
$this->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'));
Expand All @@ -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,
Expand Down
62 changes: 21 additions & 41 deletions src/Linters/UnusedTranslationLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -54,23 +56,16 @@ 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;
}
}
}
}
}
}

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
Expand All @@ -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;
}
}
10 changes: 8 additions & 2 deletions src/Parsers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Fidum\LaravelTranslationLinter\Extractors;
namespace Fidum\LaravelTranslationLinter\Readers;

use Fidum\LaravelTranslationLinter\Contracts\Extractors\Extractor as ExtractorContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\ApplicationFileFinder;
use Fidum\LaravelTranslationLinter\Contracts\Parsers\Parser;
use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader as ApplicationFileReaderContract;
use Illuminate\Support\Collection;

class Extractor implements ExtractorContract
class ApplicationFileReader implements ApplicationFileReaderContract
{
public function __construct(
protected ApplicationFileFinder $finder,
Expand Down
25 changes: 25 additions & 0 deletions src/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Fidum\LaravelTranslationLinter\Readers;

use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader as LanguageFileReaderContract;
use InvalidArgumentException;
use Symfony\Component\Finder\SplFileInfo;

class LanguageFileReader implements LanguageFileReaderContract
{
public function execute(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;
}
}

0 comments on commit 5c3ec46

Please sign in to comment.