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 2ca5f16 commit 7d01f30
Show file tree
Hide file tree
Showing 21 changed files with 282 additions and 37 deletions.
23 changes: 21 additions & 2 deletions config/translation-linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'application' => [
/*
|--------------------------------------------------------------------------
| Code Directories
| Application Directories
|--------------------------------------------------------------------------
|
| The following array lists the "directories" that will be scanned
Expand All @@ -19,7 +19,7 @@

/*
|--------------------------------------------------------------------------
| Code Extensions
| Application File Extensions
|--------------------------------------------------------------------------
|
| The following array lists the file "extensions" that will be scanned for
Expand Down Expand Up @@ -66,6 +66,25 @@
|
*/
'locales' => [env('LOCALE_DEFAULT', 'en')],

/*
|--------------------------------------------------------------------------
| Language File Readers
|--------------------------------------------------------------------------
|
| The following array lists the language file "readers" that will be
| parsed for translation keys. This should be mapped as a key value
| array. The key should be the "extension" and the value should be
| the "Reader" class that implements the require interface.
|
| If you want to disable reading a specific file type then you can
| remove it from the array below.
|
*/
'readers' => [
'json' => \Fidum\LaravelTranslationLinter\Readers\JsonFileReader::class,
'php' => \Fidum\LaravelTranslationLinter\Readers\PhpFileReader::class,
],
],

'unused' => [
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/UnusedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public function handle(
}

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

return self::SUCCESS;
}

$this->error(sprintf('%d unused translations found', $results->count()));
$this->components->error(sprintf('%d unused translations found', $results->count()));
$this->table($fields->headers(), $results->toCommandTableOutputArray($fields));

return self::FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Finders/LanguageFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface LanguageFileFinder
{
public function execute(string $path, array $extensions): Collection;
public function execute(string $path, string $locale): Collection;
}
31 changes: 27 additions & 4 deletions src/Finders/LanguageFileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,40 @@
namespace Fidum\LaravelTranslationLinter\Finders;

use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageFileFinder as LanguageFileFinderContract;
use Fidum\LaravelTranslationLinter\Managers\LanguageFileReaderManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\SplFileInfo;

readonly class LanguageFileFinder implements LanguageFileFinderContract
{
public function __construct(protected Filesystem $filesystem) {}
public function __construct(
protected Filesystem $filesystem,
protected LanguageFileReaderManager $manager,
) {}

public function execute(string $path, array $extensions): Collection
public function execute(string $path, string $locale): Collection
{
$files = new Collection($this->filesystem->allFiles($path));
if ($this->filesystem->exists($path)) {
$files = new Collection($this->filesystem->allFiles($path));
$extensions = $this->manager->getEnabledDrivers();

return $files->filter(fn (\SplFileInfo $file) => in_array($file->getExtension(), $extensions));
return $files->filter(function (SplFileInfo $file) use ($extensions, $locale) {
if (in_array($file->getExtension(), $extensions)) {
if ($file->getFilenameWithoutExtension() === $locale) {
return true;
}

return str_contains(
$file->getPathname(),
DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR
);
}

return false;
});
}

return new Collection();
}
}
7 changes: 7 additions & 0 deletions src/LaravelTranslationLinterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Fidum\LaravelTranslationLinter\Finders\LanguageFileFinder;
use Fidum\LaravelTranslationLinter\Finders\LanguageNamespaceFinder;
use Fidum\LaravelTranslationLinter\Linters\UnusedTranslationLinter;
use Fidum\LaravelTranslationLinter\Managers\LanguageFileReaderManager;
use Fidum\LaravelTranslationLinter\Parsers\ApplicationFileParser;
use Fidum\LaravelTranslationLinter\Readers\ApplicationFileReader;
use Fidum\LaravelTranslationLinter\Readers\LanguageFileReader;
Expand Down Expand Up @@ -62,6 +63,11 @@ public function registeringPackage()

$this->app->bind(LanguageFileReaderContract::class, LanguageFileReader::class);

$this->app->scoped(LanguageFileReaderManager::class, LanguageFileReaderManager::class);
$this->app->when(LanguageFileReaderManager::class)
->needs('$driverConfig')
->giveConfig('translation-linter.lang.readers');

$this->app->bind(LanguageNamespaceFinderContract::class, LanguageNamespaceFinder::class);

$this->app->bind(UnusedFieldCollectionContract::class, function (Application $app) {
Expand Down Expand Up @@ -89,6 +95,7 @@ public function provides()
ApplicationFileReaderContract::class,
LanguageFileFinderContract::class,
LanguageFileReaderContract::class,
LanguageFileReaderManager::class,
LanguageNamespaceFinderContract::class,
UnusedFieldCollectionContract::class,
UnusedFilterCollectionContract::class,
Expand Down
6 changes: 3 additions & 3 deletions src/Linters/UnusedTranslationLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function execute(): Collection
foreach ($namespaces as $namespace => $path) {
$unused[$locale][$namespace] = [];

$files = $this->files->execute($path, ['php', 'json']);
$files = $this->files->execute($path, $locale);

/** @var SplFileInfo $file */
foreach ($files as $file) {
Expand Down Expand Up @@ -73,8 +73,8 @@ protected function getLanguageKey(SplFileInfo $file, string $language, string $k
}

return Str::of($file->getPath())
->finish('/')
->after("/$language/")
->finish(DIRECTORY_SEPARATOR)
->after(DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR)
->append($file->getFilenameWithoutExtension())
->append('.')
->append($key)
Expand Down
39 changes: 39 additions & 0 deletions src/Managers/LanguageFileReaderManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Fidum\LaravelTranslationLinter\Managers;

use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Manager;

/**
* @method LanguageFileReader driver(string $driver)
*/
class LanguageFileReaderManager extends Manager
{
public function __construct(
Container $container,
protected array $driverConfig,
) {
parent::__construct($container);

foreach ($this->driverConfig as $driver => $readerClass) {
$this->extend($driver, fn (Container $app) => $app->get($readerClass));
}
}

public function getDefaultDriver()
{
return array_key_first($this->customCreators);
}

public function isEnabled(string $driver)
{
return array_key_exists($driver, $this->customCreators);
}

public function getEnabledDrivers()
{
return array_keys($this->customCreators);
}
}
21 changes: 21 additions & 0 deletions src/Readers/JsonFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Fidum\LaravelTranslationLinter\Readers;

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

class JsonFileReader implements LanguageFileReaderContract
{
public function execute(SplFileInfo $file): array
{
$translations = json_decode($file->getContents(), true);

if (! is_array($translations)) {
throw new InvalidArgumentException("Unable to extract an array from {$file->getPathname()}!");
}

return $translations;
}
}
17 changes: 11 additions & 6 deletions src/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
namespace Fidum\LaravelTranslationLinter\Readers;

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

class LanguageFileReader implements LanguageFileReaderContract
{
public function __construct(protected LanguageFileReaderManager $manager) {}

public function execute(SplFileInfo $file): array
{
$translations = match ($file->getExtension()) {
'json' => json_decode($file->getContents(), true),
default => include $file->getPathname(),
};
$extension = $file->getExtension();
$translations = [];

if ($this->manager->isEnabled($extension)) {
$translations = $this->manager->driver($extension)->execute($file);

if (! is_array($translations)) {
throw new InvalidArgumentException("Unable to extract an array from {$file->getPathname()}!");
if (! $translations) {
throw new InvalidArgumentException("Unable to extract any data from {$file->getPathname()}!");
}
}

return $translations;
Expand Down
21 changes: 21 additions & 0 deletions src/Readers/PhpFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Fidum\LaravelTranslationLinter\Readers;

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

class PhpFileReader implements LanguageFileReaderContract
{
public function execute(SplFileInfo $file): array
{
$translations = include $file->getPathname();

if (! is_array($translations)) {
throw new InvalidArgumentException("Unable to extract an array from {$file->getPathname()}!");
}

return $translations;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
11 unused translations found

ERROR 11 unused translations found.

+--------+-----------+------------------------------------+------------------------------+
| Locale | Namespace | Key | Value |
+--------+-----------+------------------------------------+------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

ERROR 11 unused translations found.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
19 unused translations found

ERROR 19 unused translations found.

+--------+-----------+------------------------------------+------------------------------+
| Locale | Namespace | Key | Value |
+--------+-----------+------------------------------------+------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
11 unused translations found

ERROR 11 unused translations found.

+--------+------------------------------------+
| Locale | Key |
+--------+------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

ERROR 22 unused translations found.

+--------+-----------+------------------------------------+----------------------------------------------------+
| Locale | Namespace | Key | Value |
+--------+-----------+------------------------------------+----------------------------------------------------+
| en | | Unused PHP Class | I am unused in php class |
| en | | Unused Blade File | I am unused in blade |
| en | | Unused Vue Component | I am unused in vue component |
| en | | example.unused | I am unused in php class |
| en | | example.blade.choice.unused | I am unused in blade |
| en | | example.blade.lang.unused | I am unused in blade |
| en | | example.vue.unused | I am unused in vue component |
| en | | folder/example.unused | I am unused in php class |
| en | | folder/example.blade.choice.unused | I am unused in blade |
| en | | folder/example.blade.lang.unused | I am unused in blade |
| en | | folder/example.vue.unused | I am unused in vue component |
| de | | Unused PHP Class | Ich werde in einer PHP-Klasse nicht verwendet |
| de | | Unused Blade File | Ich werde in Blade nicht verwendet |
| de | | Unused Vue Component | Ich werde in einem Vue-Komponenten nicht verwendet |
| de | | example.unused | Ich werde in einer PHP-Klasse nicht verwendet |
| de | | example.blade.choice.unused | Ich werde in Blade nicht verwendet |
| de | | example.blade.lang.unused | Ich werde in Blade nicht verwendet |
| de | | example.vue.unused | Ich werde in einem Vue-Komponenten nicht verwendet |
| de | | folder/example.unused | Ich werde in einer PHP-Klasse nicht verwendet |
| de | | folder/example.blade.choice.unused | Ich werde in Blade nicht verwendet |
| de | | folder/example.blade.lang.unused | Ich werde in Blade nicht verwendet |
| de | | folder/example.vue.unused | Ich werde in einem Vue-Komponenten nicht verwendet |
+--------+-----------+------------------------------------+----------------------------------------------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

INFO No unused translations found!

Loading

0 comments on commit 7d01f30

Please sign in to comment.