Skip to content

Commit 7ddb2df

Browse files
authored
Ractored. (#24)
1 parent e865ae2 commit 7ddb2df

19 files changed

+219
-83
lines changed

composer.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,33 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^8.2",
29-
"spatie/laravel-package-tools": "^1.16",
30-
"illuminate/contracts": "^10.0||^11.0||^12.0"
28+
"php": "^8.3 || ^8.2",
29+
"illuminate/contracts": "^10.0||^11.0||^12.0",
30+
"spatie/laravel-package-tools": "^1.16"
3131
},
3232
"require-dev": {
33+
"tightenco/duster": "^3.2",
34+
"driftingly/rector-laravel": "^1.2",
35+
"larastan/larastan": "^2.9",
3336
"laravel/pint": "^1.14",
3437
"nunomaduro/collision": "^8.1.1||^7.10.0",
35-
"larastan/larastan": "^2.9",
3638
"orchestra/testbench": "^9.0.0||^8.22.0",
3739
"pestphp/pest": "^2.34",
3840
"pestphp/pest-plugin-arch": "^2.7",
3941
"pestphp/pest-plugin-laravel": "^2.3",
4042
"phpstan/extension-installer": "^1.3",
4143
"phpstan/phpstan-deprecation-rules": "^1.1",
42-
"phpstan/phpstan-phpunit": "^1.3"
44+
"phpstan/phpstan-phpunit": "^1.3",
45+
"rector/rector": "^1.2"
4346
},
4447
"autoload": {
4548
"psr-4": {
46-
"Msamgan\\LaravelEnvKeysChecker\\": "src/",
47-
"Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\": "database/factories/"
49+
"Msamgan\\LaravelEnvKeysChecker\\": "src/"
4850
}
4951
},
5052
"autoload-dev": {
5153
"psr-4": {
52-
"Msamgan\\LaravelEnvKeysChecker\\Tests\\": "tests/",
53-
"Workbench\\App\\": "workbench/app/"
54+
"Msamgan\\LaravelEnvKeysChecker\\Tests\\": "tests/"
5455
}
5556
},
5657
"scripts": {
@@ -69,7 +70,7 @@
6970
"analyse": "vendor/bin/phpstan analyse",
7071
"test": "vendor/bin/pest",
7172
"test-coverage": "vendor/bin/pest --coverage",
72-
"format": "vendor/bin/pint"
73+
"format": "./vendor/bin/rector process && vendor/bin/pint && ./vendor/bin/duster fix"
7374
},
7475
"config": {
7576
"sort-packages": true,

config/env-keys-checker.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46
// List of all the .env files to ignore while checking the env keys
5-
'ignore_files' => explode(',', env('KEYS_CHECKER_IGNORE_FILES', '')),
7+
'ignore_files' => explode(',', (string) env('KEYS_CHECKER_IGNORE_FILES', '')),
68

79
// List of all the env keys to ignore while checking the env keys
8-
'ignore_keys' => explode(',', env('KEYS_CHECKER_IGNORE_KEYS', '')),
10+
'ignore_keys' => explode(',', (string) env('KEYS_CHECKER_IGNORE_KEYS', '')),
911

1012
// strategy to add the missing keys to the .env file
1113
// ask: will ask the user to add the missing keys
@@ -15,7 +17,7 @@
1517

1618
// List of all the .env.* files to be checked if they
1719
// are present in the .gitignore file
18-
'gitignore_files' => explode(',', env('KEYS_CHECKER_GITIGNORE_FILES', '.env')),
20+
'gitignore_files' => explode(',', (string) env('KEYS_CHECKER_GITIGNORE_FILES', '.env')),
1921

2022
// Master .env file to be used for syncing the keys
2123
'master_env' => env('MASTER_ENV', '.env'),

pint.json

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
11
{
22
"preset": "laravel",
3+
"notPath": [
4+
"tests/TestCase.php"
5+
],
36
"rules": {
7+
"array_push": true,
8+
"backtick_to_shell_exec": true,
9+
"date_time_immutable": true,
10+
"declare_strict_types": true,
11+
"lowercase_keywords": true,
12+
"lowercase_static_reference": true,
13+
"final_class": true,
14+
"final_internal_class": true,
15+
"final_public_method_for_abstract_class": true,
16+
"fully_qualified_strict_types": true,
17+
"global_namespace_import": {
18+
"import_classes": true,
19+
"import_constants": true,
20+
"import_functions": true
21+
},
22+
"mb_str_functions": true,
23+
"modernize_types_casting": true,
24+
"new_with_parentheses": false,
25+
"no_superfluous_elseif": true,
26+
"no_useless_else": true,
27+
"no_multiple_statements_per_line": true,
28+
"ordered_class_elements": {
29+
"order": [
30+
"use_trait",
31+
"case",
32+
"constant",
33+
"constant_public",
34+
"constant_protected",
35+
"constant_private",
36+
"property_public",
37+
"property_protected",
38+
"property_private",
39+
"construct",
40+
"destruct",
41+
"magic",
42+
"phpunit",
43+
"method_abstract",
44+
"method_public_static",
45+
"method_public",
46+
"method_protected_static",
47+
"method_protected",
48+
"method_private_static",
49+
"method_private"
50+
],
51+
"sort_algorithm": "none"
52+
},
53+
"ordered_interfaces": true,
54+
"ordered_traits": true,
55+
"protected_to_private": true,
56+
"self_accessor": true,
57+
"self_static_accessor": true,
58+
"strict_comparison": true,
59+
"visibility_required": true,
460
"concat_space": {
561
"spacing": "one"
6-
},
7-
"ordered_imports": {
8-
"sort_algorithm": "alpha"
962
}
1063
}
1164
}

rector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\ValueObject\PhpVersion;
7+
use RectorLaravel\Set\LaravelSetList;
8+
9+
return RectorConfig::configure()
10+
->withPaths([
11+
__DIR__ . '/src',
12+
__DIR__ . '/config',
13+
__DIR__ . '/tests',
14+
])->withPhpSets(php83: true)
15+
->withPhpVersion(PhpVersion::PHP_83)
16+
->withPreparedSets(
17+
deadCode: true,
18+
codeQuality: true,
19+
typeDeclarations: true,
20+
privatization: true,
21+
instanceOf: true,
22+
earlyReturn: true,
23+
carbon: true,
24+
)->withSets([
25+
LaravelSetList::LARAVEL_110,
26+
LaravelSetList::LARAVEL_CODE_QUALITY,
27+
LaravelSetList::LARAVEL_IF_HELPERS,
28+
LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL,
29+
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
30+
LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER,
31+
LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME,
32+
LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL,
33+
LaravelSetList::LARAVEL_COLLECTION,
34+
])->withImportNames(importDocBlockNames: false);

src/Actions/AddKeys.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Msamgan\LaravelEnvKeysChecker\Actions;
46

57
use Illuminate\Support\Collection;
68

7-
class AddKeys
9+
final class AddKeys
810
{
911
public function handle(Collection $missingKeys): void
1012
{
11-
$missingKeys->each(function ($missingKey) {
13+
$missingKeys->each(function (array $missingKey): void {
1214
$filePath = base_path($missingKey['envFile']);
1315
$envContent = file($filePath);
1416

src/Actions/CheckKeys.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Msamgan\LaravelEnvKeysChecker\Actions;
46

57
use Illuminate\Support\Collection;
68

7-
class CheckKeys
9+
final class CheckKeys
810
{
911
public function handle(array $keyData, array $envFiles, Collection $missingKeys): void
1012
{
11-
collect($envFiles)->each(function ($envFile) use ($keyData, $missingKeys) {
13+
collect($envFiles)->each(function ($envFile) use ($keyData, $missingKeys): void {
1214
$envContent = file($envFile);
1315
$keyExists = false;
1416

1517
foreach ($envContent as $line) {
16-
if (str_starts_with($line, $keyData['key'])) {
18+
if (str_starts_with($line, (string) $keyData['key'])) {
1719
$keyExists = true;
1820
break;
1921
}

src/Actions/FilterFiles.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Msamgan\LaravelEnvKeysChecker\Actions;
6+
7+
final class FilterFiles
8+
{
9+
public function handle(array $envFiles, array $ignoredFiles): array
10+
{
11+
return collect(value: $envFiles)
12+
->reject(callback: fn ($file): bool => in_array(needle: basename((string) $file), haystack: $ignoredFiles))
13+
->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted'))
14+
->toArray();
15+
}
16+
}

src/Actions/GetKeys.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Msamgan\LaravelEnvKeysChecker\Actions;
46

57
use Illuminate\Support\Collection;
68

7-
class GetKeys
9+
final class GetKeys
810
{
911
public function handle(array|string $files, ?bool $withComments = false): Collection
1012
{
@@ -16,7 +18,7 @@ public function handle(array|string $files, ?bool $withComments = false): Collec
1618

1719
return $files
1820
->map(function ($file) use ($ignoredKeys, $withComments) {
19-
$collection = collect(file($file))->map(function ($line, $index) use ($file) {
21+
$collection = collect(file($file))->map(function ($line, $index) use ($file): array {
2022
[$key] = explode('=', $line);
2123

2224
return [
@@ -27,14 +29,10 @@ public function handle(array|string $files, ?bool $withComments = false): Collec
2729
});
2830

2931
if (! $withComments) {
30-
$collection = $collection->filter(function ($item) {
31-
return $item['key'] !== "\n" && ! str_starts_with($item['key'], '#');
32-
});
32+
$collection = $collection->filter(fn ($item): bool => $item['key'] !== "\n" && ! str_starts_with($item['key'], '#'));
3333
}
3434

35-
return $collection->filter(function ($keyData) use ($ignoredKeys) {
36-
return ! in_array($keyData['key'], $ignoredKeys);
37-
});
35+
return $collection->reject(fn ($keyData): bool => in_array($keyData['key'], $ignoredKeys));
3836
})
3937
->flatten(1)
4038
->unique('key');

src/Commands/EnvInGitIgnoreCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Msamgan\LaravelEnvKeysChecker\Commands;
46

57
use Illuminate\Console\Command;
68
use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions;
79

8-
class EnvInGitIgnoreCommand extends Command
10+
final class EnvInGitIgnoreCommand extends Command
911
{
1012
use HelperFunctions;
1113

@@ -28,7 +30,7 @@ public function handle(): int
2830
$filesToCheck = config(key: 'env-keys-checker.gitignore_files', default: ['.env']);
2931

3032
$missingFiles = collect();
31-
collect(value: $filesToCheck)->each(callback: function ($file) use ($gitIgnoreContent, $missingFiles) {
33+
collect(value: $filesToCheck)->each(callback: function ($file) use ($gitIgnoreContent, $missingFiles): void {
3234
if (! in_array(needle: $file, haystack: $gitIgnoreContent)) {
3335
$missingFiles->push(values: $file);
3436
}

src/Commands/EnvKeysSyncCommand.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Msamgan\LaravelEnvKeysChecker\Commands;
46

57
use Illuminate\Console\Command;
8+
use Msamgan\LaravelEnvKeysChecker\Actions\FilterFiles;
69
use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions;
710

8-
class EnvKeysSyncCommand extends Command
11+
final class EnvKeysSyncCommand extends Command
912
{
1013
use HelperFunctions;
1114

1215
public $signature = 'env:sync-keys';
1316

1417
public $description = 'Sync keys from master .env file to other .env files.';
1518

16-
public function handle(): int
19+
public function handle(FilterFiles $filterFiles): int
1720
{
1821
$allKeysCheck = $this->call('env:keys-check', [
1922
'--auto-add' => 'none',
@@ -28,30 +31,26 @@ public function handle(): int
2831
return self::FAILURE;
2932
}
3033

31-
$envFiles = glob(pattern: base_path(path: '.env*'));
32-
$ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []);
34+
$envFiles = $this->getEnvs();
35+
$ignoredFiles = $this->getFilesToIgnore();
3336

34-
if (empty($envFiles)) {
37+
if ($envFiles === []) {
3538
$this->showFailureInfo(message: 'No .env files found.');
3639

3740
return self::FAILURE;
3841
}
3942

40-
$envFiles = collect(value: $envFiles)->filter(callback: function ($file) use ($ignoredFiles) {
41-
return ! in_array(needle: basename(path: $file), haystack: $ignoredFiles);
42-
})->toArray();
43+
$envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: $ignoredFiles);
4344

44-
if (empty($envFiles)) {
45+
if ($envFiles === []) {
4546
$this->showFailureInfo(message: 'No .env files found.');
4647

4748
return self::FAILURE;
4849
}
4950

50-
$envFiles = collect(value: $envFiles)->filter(callback: function ($file) {
51-
return basename(path: $file) !== $this->getMasterEnv();
52-
});
51+
$envFiles = collect(value: $envFiles)->filter(callback: fn ($file): bool => basename(path: (string) $file) !== $this->getMasterEnv());
5352

54-
$envFiles->each(callback: function ($envFile) {
53+
$envFiles->each(callback: function ($envFile): void {
5554
$totalKeysFromMaster = count(value: file(filename: $this->getMasterEnv()));
5655
for ($line = 1; $line <= $totalKeysFromMaster; $line++) {
5756
$keyMaster = $this->getKeyFromFileOnLine(file: $this->getMasterEnv(), line: $line);
@@ -93,12 +92,12 @@ public function handle(): int
9392

9493
private function getMasterEnv(): string
9594
{
96-
return config(key: 'env-keys-checker.master_env', default: '.env');
95+
return (string) config(key: 'env-keys-checker.master_env', default: '.env');
9796
}
9897

9998
private function getKeyFromFileOnLine(string $file, int $line): string
10099
{
101-
return file(filename: $file)[$line - 1];
100+
return file(filename: $file)[$line - 1] ?? '';
102101
}
103102

104103
private function checkIfComment(string $line): bool
@@ -122,11 +121,9 @@ private function checkIfEmptyLine(string $line): bool
122121
private function moveKeyToLine(string $file, string $key, int $toLine): void
123122
{
124123
$lines = file(filename: $file);
125-
$keyLine = array_filter(array: $lines, callback: function ($line) use ($key) {
126-
return str_starts_with($line, $key);
127-
});
124+
$keyLine = array_filter(array: $lines, callback: fn ($line): bool => str_starts_with($line, $key));
128125

129-
if (empty($keyLine)) {
126+
if ($keyLine === []) {
130127
return;
131128
}
132129

0 commit comments

Comments
 (0)