Skip to content

Commit 9eb8c95

Browse files
committed
Add new linter: SameParametersLinter
1 parent 12fb35e commit 9eb8c95

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

src/Linters/SameParametersLinter.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Lukasss93\Larex\Linters;
4+
5+
use Illuminate\Support\Collection;
6+
use Lukasss93\Larex\Contracts\Linter;
7+
use Lukasss93\Larex\Exceptions\LintException;
8+
use Lukasss93\Larex\Support\CsvReader;
9+
10+
class SameParametersLinter implements Linter
11+
{
12+
/**
13+
* @inheritDoc
14+
*/
15+
public static function description(): string
16+
{
17+
return 'Checking same parameters...';
18+
}
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function handle(CsvReader $reader): void
24+
{
25+
$errors = collect([]);
26+
27+
//get headers
28+
$headers = $reader->getHeader()->flip();
29+
30+
//check if all parameters are the same
31+
$reader->getRows()->each(function (Collection $columns, int $line) use ($headers, &$errors) {
32+
$line += 2;
33+
34+
$group = $columns->get('group');
35+
$key = $columns->get('key');
36+
37+
//get parameters for every language
38+
$parameters = $columns
39+
->skip(2)
40+
->map(function (string $item) {
41+
preg_match_all('/:\w+/', $item, $matches);
42+
43+
return collect($matches[0] ?? []);
44+
});
45+
46+
//get first item with max parameters count
47+
/** @var Collection $max */
48+
$max = $parameters->sortByDesc(fn ($item) => count($item))->first();
49+
50+
//check if all parameters are the same
51+
foreach ($parameters as $lang => $items) {
52+
$column = $headers->get($lang) + 1;
53+
$value = $columns->get($lang);
54+
55+
if (blank($value) && config('larex.ignore_empty_values', false)) {
56+
continue;
57+
}
58+
59+
foreach ($max->diff($items) as $param) {
60+
$errors->push("line $line ($group.$key), column $column ($lang): missing $param parameter");
61+
}
62+
}
63+
});
64+
65+
if ($errors->isNotEmpty()) {
66+
throw new LintException('Missing parameters found:', $errors->toArray());
67+
}
68+
}
69+
}

src/config/larex.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@
5858
// Lukasss93\Larex\Linters\UntranslatedStringsLinter::class,
5959
// Lukasss93\Larex\Linters\UnusedStringsLinter::class,
6060
// Lukasss93\Larex\Linters\ValidHtmlValueLinter::class,
61+
// Lukasss93\Larex\Linters\SameParametersLinter::class,
6162
],
6263

64+
/**
65+
* Used by SameParametersLinter
66+
*/
67+
'ignore_empty_values' => false,
68+
6369
/**
6470
* Search criteria for file used by:
6571
* - UntranslatedStringsLinter
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
use Lukasss93\Larex\Console\LarexLintCommand;
4+
use Lukasss93\Larex\Linters\SameParametersLinter;
5+
6+
it('passes', function () {
7+
config([
8+
'larex.linters' => [SameParametersLinter::class],
9+
]);
10+
11+
initFromStub('linters.same-parameters.success');
12+
13+
$this->artisan(LarexLintCommand::class)
14+
->expectsOutput('OK (1 linter)')
15+
->assertExitCode(0);
16+
});
17+
18+
it('fails', function () {
19+
config([
20+
'larex.linters' => [SameParametersLinter::class],
21+
]);
22+
23+
initFromStub('linters.same-parameters.failure');
24+
25+
$this->artisan(LarexLintCommand::class)
26+
->expectsOutput(' FAIL Missing parameters found:')
27+
->expectsOutput('├ line 2 (app.hello), column 4 (it): missing :age parameter')
28+
->expectsOutput('├ line 2 (app.hello), column 5 (es): missing :name parameter')
29+
->expectsOutput('├ line 3 (app.cat), column 3 (en): missing :thing parameter')
30+
->expectsOutput('├ line 3 (app.cat), column 5 (es): missing :animal parameter')
31+
->expectsOutput('├ line 3 (app.cat), column 5 (es): missing :thing parameter')
32+
->expectsOutput('└ line 6 (app.bye), column 5 (es): missing :name parameter')
33+
->expectsOutput('FAILURES!')
34+
->expectsOutput('Linters: 1, Failures: 1')
35+
->assertExitCode(1);
36+
});
37+
38+
it('fails ignoring empty values', function () {
39+
config([
40+
'larex.linters' => [SameParametersLinter::class],
41+
'larex.ignore_empty_values' => true,
42+
]);
43+
44+
initFromStub('linters.same-parameters.failure');
45+
46+
$this->artisan(LarexLintCommand::class)
47+
->expectsOutput(' FAIL Missing parameters found:')
48+
->expectsOutput('├ line 2 (app.hello), column 4 (it): missing :age parameter')
49+
->expectsOutput('├ line 2 (app.hello), column 5 (es): missing :name parameter')
50+
->expectsOutput('├ line 3 (app.cat), column 3 (en): missing :thing parameter')
51+
->expectsOutput('├ line 3 (app.cat), column 5 (es): missing :animal parameter')
52+
->expectsOutput('└ line 3 (app.cat), column 5 (es): missing :thing parameter')
53+
->expectsOutput('FAILURES!')
54+
->expectsOutput('Linters: 1, Failures: 1')
55+
->assertExitCode(1);
56+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
group,key,en,it,es
2+
app,hello,My name is :name and I'm :age.,Il nome è :name.,Tengo :age años.
3+
app,cat,The :animal is on the table.,Il :animal è sul :thing.,El animal está sobre la mesa.
4+
app,yep,Yes :who can,Sì :who può,Sí :who puede
5+
app,nope,Nope,No,No
6+
app,bye,Bye :name,Ciao :name,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
group,key,en,it
2+
app,hello,My name is :name and I'm :age.,Il nome è :name e ho :age anni.

0 commit comments

Comments
 (0)