Skip to content

Commit 0325045

Browse files
wip cq
1 parent 3f41da9 commit 0325045

File tree

9 files changed

+84
-62
lines changed

9 files changed

+84
-62
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require-dev": {
88
"pestphp/pest": "^3.8",
99
"orchestra/testbench": "^10.2",
10-
"carthage-software/mago": "^0.23.0"
10+
"carthage-software/mago": "^0.26.1"
1111
},
1212
"autoload": {
1313
"psr-4": {
@@ -43,6 +43,11 @@
4343
"test": [
4444
"@clear",
4545
"@php vendor/bin/pest"
46+
],
47+
"qa": [
48+
"@php vendor/bin/mago fmt",
49+
"@php vendor/bin/mago lint",
50+
"@test"
4651
]
4752
},
4853
"config": {

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mago.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
php_version = "8.4.0"
44

55
[source]
6-
paths = ["src/", "tests/", "workbench/app/"]
6+
paths = ["src/", "workbench/app/"]
77
includes = ["vendor"]
88
excludes = []
99

@@ -18,4 +18,8 @@ plugins = ["laravel"]
1818

1919
[[linter.rules]]
2020
name = "naming/class"
21-
level = "off"
21+
level = "off"
22+
23+
[[linter.rules]]
24+
name = "analysis/undefined-function-or-method"
25+
level = "off"

src/Console/Commands/GenerateTranslations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class GenerateTranslations extends Command
1717

1818
public function handle(Polywarp $polywarp): int
1919
{
20-
$outFile = Config::get('polywarp.output_path');
20+
$outFile = Config::get(key: 'polywarp.output_path');
2121

2222
File::ensureDirectoryExists(pathinfo($outFile, PATHINFO_DIRNAME));
2323

@@ -26,7 +26,7 @@ public function handle(Polywarp $polywarp): int
2626
keysToKeep: $polywarp->discoverUsedTranslationKeys(),
2727
));
2828

29-
$this->info('Translations generated successfully.');
29+
$this->info(string: 'Translations generated successfully.');
3030

3131
return Command::SUCCESS;
3232
}

src/Polywarp.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ public function discoverTranslations(): Collection
1717
->flatMap(static fn(string $path): Collection => collect(File::allFiles($path))
1818
->map(static fn(SplFileInfo $file): array => [
1919
'path' => $file->getPathname(),
20-
'lang' => str(substr(pathinfo($file->getPathname(), PATHINFO_DIRNAME), strlen($path) + 1))
21-
->replace('/', '.')
22-
->toString(),
20+
'lang' => str(substr(pathinfo($file->getPathname(), PATHINFO_DIRNAME), strlen($path) + 1))->replace(
21+
search: '/',
22+
replace: '.',
23+
)->toString(),
2324
]))
2425
->reduceWithKeys(static function (Collection $acc, array $file): Collection {
25-
$key = implode('.', [$file['lang'], pathinfo($file['path'], PATHINFO_FILENAME)]);
26+
$key = implode(
27+
separator: '.',
28+
array: [$file['lang'], pathinfo($file['path'], PATHINFO_FILENAME)],
29+
);
2630

2731
$acc[$key] = match (pathinfo($file['path'], PATHINFO_EXTENSION)) {
28-
'json' => json_decode($file['path'], true, JSON_THROW_ON_ERROR),
32+
'json' => json_decode($file['path'], associative: true, flags: JSON_THROW_ON_ERROR),
2933
'php' => require $file['path'],
3034
default => [],
3135
};
@@ -34,15 +38,15 @@ public function discoverTranslations(): Collection
3438
}, collect())
3539
->undot()
3640
->map(static function (array $value): array {
37-
return collect($value)->dot()->filter(fn($trans) => is_string($trans))->toArray();
41+
return collect($value)->dot()->filter(fn(mixed $trans): bool => is_string($trans))->toArray();
3842
});
3943
}
4044

4145
public function discoverUsedTranslationKeys(): Collection
4246
{
43-
$outputPath = Config::get('polywarp.output_path');
47+
$outputPath = Config::string(key: 'polywarp.output_path');
4448

45-
return collect(Config::get('polywarp.script_paths'))
49+
return collect(Config::array(key: 'polywarp.script_paths'))
4650
->flatMap(File::allFiles(...))
4751
->flatMap(static function (SplFileInfo $file) use ($outputPath): Collection {
4852
if ($file->getExtension() !== 'ts' || $file->getPathname() === $outputPath) {
@@ -51,7 +55,11 @@ public function discoverUsedTranslationKeys(): Collection
5155

5256
$content = $file->getContents();
5357

54-
preg_match_all('/t\(\s*([\'"])(.*?)\1/', $content, $matches);
58+
preg_match_all(
59+
pattern: '/t\(\s*([\'"])(.*?)\1/',
60+
subject: $content,
61+
matches: $matches,
62+
);
5563

5664
return collect($matches[2]);
5765
});
@@ -60,34 +68,41 @@ public function discoverUsedTranslationKeys(): Collection
6068
private function compileTypeOverloads(Collection $translations): string
6169
{
6270
return $translations
63-
->flatMap(fn($e) => $e)
71+
->collapse()
6472
->map(static function (string $value, string $key): string {
6573
$params = [];
66-
preg_match_all('/:(\w+)/', $value, $matches);
74+
preg_match_all(
75+
pattern: '/:(\w+)/',
76+
subject: $value,
77+
matches: $matches,
78+
);
6779
if (isset($matches[1])) {
6880
$params = $matches[1];
6981
}
7082

7183
if ($params) {
72-
$paramStr = implode(', ', array_map(fn(string $p): string => "{$p}: string | number", $params));
84+
$paramStr = implode(
85+
separator: ', ',
86+
array: array_map(fn(string $p): string => "{$p}: string | number", $params),
87+
);
7388
return "(key: \"{$key}\", params: { {$paramStr} }): string;";
7489
}
7590

7691
return "(key: \"{$key}\"): string;";
7792
})
78-
->implode('');
93+
->implode(value: '');
7994
}
8095

8196
public function compile(Collection $availableTranlsations, Collection $keysToKeep): string
8297
{
83-
$translationsToIncludeInBundle = json_encode($availableTranlsations->mapWithKeys(static fn(
84-
array $value,
85-
string $lang,
86-
): array => [$lang => array_filter(
87-
$value,
88-
$keysToKeep->contains(...),
89-
mode: ARRAY_FILTER_USE_KEY,
90-
)]), JSON_THROW_ON_ERROR);
98+
$translationsToIncludeInBundle = json_encode(
99+
$availableTranlsations->mapWithKeys(static fn(array $value, string $lang): array => [$lang => array_filter(
100+
$value,
101+
$keysToKeep->contains(...),
102+
mode: ARRAY_FILTER_USE_KEY,
103+
)]),
104+
JSON_THROW_ON_ERROR,
105+
);
91106

92107
return <<<ts
93108
// This file is auto-generated. Do not edit it manually.

src/PolywarpServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ final class PolywarpServiceProvider extends ServiceProvider
1313
#[Override]
1414
public function register(): void
1515
{
16-
$this->mergeConfigFrom(__DIR__ . '/../config/polywarp.php', 'polywarp');
16+
$this->mergeConfigFrom(
17+
path: __DIR__ . '/../config/polywarp.php',
18+
key: 'polywarp',
19+
);
1720
}
1821

1922
public function boot(): void

tests/Feature/PolywarpCommandTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use Illuminate\Support\Facades\Config;
6-
75
it('returns a successful response', function (): void {
86
@unlink(config('polywarp.output_path'));
97

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function getEnvironmentSetUp($app): void
2323
// config()->set('database.default', 'testing');
2424
}
2525

26-
public static function applicationBasePath()
26+
public static function applicationBasePath(): string
2727
{
28-
return package_path('workbench');
28+
return package_path(path: 'workbench');
2929
}
3030
}

tests/Unit/PolywarpTest.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,43 @@
1111
$translations = app(Polywarp::class)->discoverTranslations();
1212

1313
foreach (['en', 'sv'] as $lang) {
14-
expect($translations[$lang])
15-
->toHaveKeys([
16-
'test.welcome',
17-
'test.goodbye',
18-
'test.greeting.morning',
19-
'test.greeting.afternoon',
20-
'test.greeting.evening',
21-
'test.farewell.formal',
22-
'test.farewell.informal',
23-
]);
14+
expect($translations[$lang])->toHaveKeys([
15+
'test.welcome',
16+
'test.goodbye',
17+
'test.greeting.morning',
18+
'test.greeting.afternoon',
19+
'test.greeting.evening',
20+
'test.farewell.formal',
21+
'test.farewell.informal',
22+
]);
2423
}
2524
});
2625

2726
it('discovers all used translation keys', function (): void {
28-
Config::set('polywarp.script_paths', [
29-
workbench_path('resources/js'),
27+
Config::set(key: 'polywarp.script_paths', value: [
28+
workbench_path(path: 'resources/js'),
3029
]);
3130

3231
$usedKeys = app(Polywarp::class)->discoverUsedTranslationKeys();
3332

34-
expect($usedKeys->toArray())
35-
->toEqualCanonicalizing([
36-
'test.welcome',
37-
'test-with-attributes',
38-
]);
33+
expect($usedKeys->toArray())->toEqualCanonicalizing([
34+
'test.welcome',
35+
'test-with-attributes',
36+
]);
3937
});
4038

4139
it('discovers used keys with attributes', function (): void {
42-
Config::set('polywarp.script_paths', [
43-
workbench_path('resources/js'),
40+
Config::set(key: 'polywarp.script_paths', value: [
41+
workbench_path(path: 'resources/js'),
4442
]);
4543

4644
$usedKeys = app(Polywarp::class)->discoverUsedTranslationKeys();
4745
$usedKeys = $usedKeys->toArray();
4846

49-
expect($usedKeys)
50-
->toEqualCanonicalizing([
51-
'test.welcome',
52-
'test-with-attributes',
53-
]);
47+
expect($usedKeys)->toEqualCanonicalizing([
48+
'test.welcome',
49+
'test-with-attributes',
50+
]);
5451
});
5552

5653
it('can compile', function (): void {

0 commit comments

Comments
 (0)