Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Oct 5, 2023
1 parent f7ba68d commit 440356c
Show file tree
Hide file tree
Showing 50 changed files with 876 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
/vendor
node_modules
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
[![Total Downloads](https://img.shields.io/packagist/dt/fidum/laravel-translation-linter.svg?style=for-the-badge)](https://packagist.org/packages/fidum/laravel-translation-linter)
[![Twitter Follow](https://img.shields.io/badge/follow-%40danmasonmp-1DA1F2?logo=twitter&style=for-the-badge)](https://twitter.com/danmasonmp)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

## Installation

You can install the package via composer:

```bash
composer require fidum/laravel-translation-linter
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag="laravel-translation-linter-migrations"
php artisan migrate
composer require --dev fidum/laravel-translation-linter
```

You can publish the config file with:
Expand All @@ -36,12 +27,6 @@ return [
];
```

Optionally, you can publish the views using

```bash
php artisan vendor:publish --tag="laravel-translation-linter-views"
```

## Usage

```php
Expand Down
23 changes: 7 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
},
"autoload": {
"psr-4": {
"Fidum\\LaravelTranslationLinter\\": "src/",
"Fidum\\LaravelTranslationLinter\\Database\\Factories\\": "database/factories/"
"Fidum\\LaravelTranslationLinter\\": "src/"
}
},
"autoload-dev": {
Expand All @@ -45,22 +44,14 @@
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"clear": "@php vendor/bin/testbench package:purge-laravel-translation-linter --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": [
"@composer run prepare",
"@php vendor/bin/testbench workbench:build --ansi"
],
"start": [
"Composer\\Config::disableProcessTimeout",
"@composer run build",
"@php vendor/bin/testbench serve"
],
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
]
},
"config": {
"sort-packages": true,
Expand All @@ -81,4 +72,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
117 changes: 116 additions & 1 deletion config/translation-linter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,121 @@
<?php

// config for Fidum/LaravelTranslationLinter
return [
'application' => [
/*
|--------------------------------------------------------------------------
| Code Directories
|--------------------------------------------------------------------------
|
| The following array lists the "directories" that will be scanned
| for translations. The defaults below should cover most uses
| but if you need to add more make sure they are absolute paths.
|
*/
'directories' => [
app_path(),
resource_path(),
],

/*
|--------------------------------------------------------------------------
| Code Extensions
|--------------------------------------------------------------------------
|
| The following array lists the file "extensions" that will be scanned for
| translations. Make sure that all files where you use translations are
| included here.
|
*/
'extensions' => [
'php',
'js',
'vue',
],
],

'lang' => [
/*
|--------------------------------------------------------------------------
| Language Functions
|--------------------------------------------------------------------------
|
| The following array lists the translation "functions" that will be used
| to find translation usage throughout your code. This is used in the
| regex pattern below to detect translations.
|
*/
'functions' => [
'__',
'_t',
'@lang',
'@choice',
'trans',
'trans_choice',
'Lang::choice',
'Lang::get',
'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
|--------------------------------------------------------------------------
|
| The following array contains the language 'locales' to use.
|
*/
'locales' => [env('LOCALE_DEFAULT', 'en')],
],

'unused' => [
/*
|--------------------------------------------------------------------------
| Output Fields
|--------------------------------------------------------------------------
|
| The following array lists the "fields" that are displayed by the command
| when unused translations are found. Set any of these to `false` to hide
| them from the output or change all to `false` to not show anything.
|
*/
'fields' => [
'lang' => true,
'namespace' => true,
'key' => true,
'value' => true,
],

/*
|--------------------------------------------------------------------------
| Unused Language Filters
|--------------------------------------------------------------------------
|
| The following array lists the "filters" that will be used to filter out
| erroneously detected unused translations. For example, you may want to
| ignore laravel or vendor translations.
|
| All filters must implement the filter interface or they will be skipped:
| \Fidum\LaravelTranslationLinter\Contracts\Filter
|
| We have included some filters with this package which may be of use.
|
*/
'filters' => [
\Fidum\LaravelTranslationLinter\Filters\DefaultLanguageFilesFilter::class,
\Fidum\LaravelTranslationLinter\Filters\IgnoreNamespacedKeysFilter::class,
],
],
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_translation_linter_table.php.stub

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ parameters:
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
Expand Down
Empty file removed resources/views/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions src/Collections/UnusedFieldCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Fidum\LaravelTranslationLinter\Collections;

use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFieldCollection as UnusedFieldCollectionContract;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class UnusedFieldCollection extends Collection implements UnusedFieldCollectionContract
{
public function enabled(): static
{
return $this->filter()->keys();

Check failure on line 13 in src/Collections/UnusedFieldCollection.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Fidum\LaravelTranslationLinter\Collections\UnusedFieldCollection::enabled() should return static(Fidum\LaravelTranslationLinter\Collections\UnusedFieldCollection) but returns Fidum\LaravelTranslationLinter\Collections\UnusedFieldCollection.
}

public function headers(): array
{
return $this->enabled()
->map(fn ($v) => Str::headline($v))
->toArray();
}
}
27 changes: 27 additions & 0 deletions src/Collections/UnusedFilterCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Fidum\LaravelTranslationLinter\Collections;

use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFilterCollection as UnusedFilterCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Filters\Filter;
use http\Exception\InvalidArgumentException;
use Illuminate\Support\Collection;

class UnusedFilterCollection extends Collection implements UnusedFilterCollectionContract
{
public function shouldReport(string $lang, string $namespace, string $key, ?string $value): bool
{
return $this->every(function (string $filterClass) use ($lang, $namespace, $key, $value) {
$interface = Filter::class;

if (is_subclass_of($filterClass, $interface)) {
/** @var Filter $filter */
$filter = app($filterClass);

return $filter->shouldReport($lang, $namespace, $key, $value);
}

throw new InvalidArgumentException("Filter [$filterClass] needs to implement [$interface].");
});
}
}
26 changes: 26 additions & 0 deletions src/Collections/UnusedResultCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Fidum\LaravelTranslationLinter\Collections;

use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFieldCollection as UnusedFieldCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedResultCollection as UnusedResultCollectionContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class UnusedResultCollection extends Collection implements UnusedResultCollectionContract
{
public function addUnusedLanguageKey(string $lang, string $namespace, string $key, ?string $value): UnusedResultCollectionContract
{
return $this->push([
'lang' => $lang,
'namespace' => $namespace,
'key' => $key,
'value' => $value,
]);
}

public function toCommandTableOutputArray(UnusedFieldCollectionContract $fields): array
{
return Arr::only($this->items, $fields->enabled()->toArray());
}
}
19 changes: 0 additions & 19 deletions src/Commands/LaravelTranslationLinterCommand.php

This file was deleted.

Loading

0 comments on commit 440356c

Please sign in to comment.