From ad5e506d0aaaad91f2fdd0a5021bebbdb86a138b Mon Sep 17 00:00:00 2001 From: msamgan Date: Thu, 29 May 2025 20:18:25 -0400 Subject: [PATCH 1/8] Ractored. --- composer.json | 10 +++--- config/env-keys-checker.php | 6 ++-- rector.php | 34 ++++++++++++++++++++ src/Actions/AddKeys.php | 2 +- src/Actions/CheckKeys.php | 4 +-- src/Actions/GetKeys.php | 10 ++---- src/Commands/EnvInGitIgnoreCommand.php | 2 +- src/Commands/EnvKeysSyncCommand.php | 18 ++++------- src/Commands/KeysCheckerCommand.php | 18 ++++------- src/LaravelEnvKeysCheckerServiceProvider.php | 2 ++ tests/ExampleTest.php | 2 +- tests/LaravelEnvKeysCheckerCommandTest.php | 2 +- tests/TestCase.php | 8 +++-- 13 files changed, 73 insertions(+), 45 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 69e3b72..f68efe8 100644 --- a/composer.json +++ b/composer.json @@ -25,21 +25,23 @@ } ], "require": { - "php": "^8.2", + "php": "^8.3", "spatie/laravel-package-tools": "^1.16", "illuminate/contracts": "^10.0||^11.0||^12.0" }, "require-dev": { + "driftingly/rector-laravel": "^1.2", + "larastan/larastan": "^2.9", "laravel/pint": "^1.14", "nunomaduro/collision": "^8.1.1||^7.10.0", - "larastan/larastan": "^2.9", "orchestra/testbench": "^9.0.0||^8.22.0", "pestphp/pest": "^2.34", "pestphp/pest-plugin-arch": "^2.7", "pestphp/pest-plugin-laravel": "^2.3", "phpstan/extension-installer": "^1.3", "phpstan/phpstan-deprecation-rules": "^1.1", - "phpstan/phpstan-phpunit": "^1.3" + "phpstan/phpstan-phpunit": "^1.3", + "rector/rector": "^1.2" }, "autoload": { "psr-4": { @@ -69,7 +71,7 @@ "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/pest", "test-coverage": "vendor/bin/pest --coverage", - "format": "vendor/bin/pint" + "format": "./vendor/bin/rector process && vendor/bin/pint" }, "config": { "sort-packages": true, diff --git a/config/env-keys-checker.php b/config/env-keys-checker.php index a25d4d2..4a8f929 100644 --- a/config/env-keys-checker.php +++ b/config/env-keys-checker.php @@ -2,10 +2,10 @@ return [ // List of all the .env files to ignore while checking the env keys - 'ignore_files' => explode(',', env('KEYS_CHECKER_IGNORE_FILES', '')), + 'ignore_files' => explode(',', (string) env('KEYS_CHECKER_IGNORE_FILES', '')), // List of all the env keys to ignore while checking the env keys - 'ignore_keys' => explode(',', env('KEYS_CHECKER_IGNORE_KEYS', '')), + 'ignore_keys' => explode(',', (string) env('KEYS_CHECKER_IGNORE_KEYS', '')), // strategy to add the missing keys to the .env file // ask: will ask the user to add the missing keys @@ -15,7 +15,7 @@ // List of all the .env.* files to be checked if they // are present in the .gitignore file - 'gitignore_files' => explode(',', env('KEYS_CHECKER_GITIGNORE_FILES', '.env')), + 'gitignore_files' => explode(',', (string) env('KEYS_CHECKER_GITIGNORE_FILES', '.env')), // Master .env file to be used for syncing the keys 'master_env' => env('MASTER_ENV', '.env'), diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..18a873c --- /dev/null +++ b/rector.php @@ -0,0 +1,34 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/config', + __DIR__ . '/tests', + ])->withPhpSets(php83: true) + ->withPhpVersion(PhpVersion::PHP_83) + ->withPreparedSets( + deadCode: true, + codeQuality: true, + typeDeclarations: true, + privatization: true, + instanceOf: true, + earlyReturn: true, + carbon: true, + )->withSets([ + LaravelSetList::LARAVEL_110, + LaravelSetList::LARAVEL_CODE_QUALITY, + LaravelSetList::LARAVEL_IF_HELPERS, + LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL, + LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES, + LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER, + LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME, + LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL, + LaravelSetList::LARAVEL_COLLECTION, + ])->withImportNames(importDocBlockNames: false); diff --git a/src/Actions/AddKeys.php b/src/Actions/AddKeys.php index c9b134d..9f72aaf 100644 --- a/src/Actions/AddKeys.php +++ b/src/Actions/AddKeys.php @@ -8,7 +8,7 @@ class AddKeys { public function handle(Collection $missingKeys): void { - $missingKeys->each(function ($missingKey) { + $missingKeys->each(function (array $missingKey): void { $filePath = base_path($missingKey['envFile']); $envContent = file($filePath); diff --git a/src/Actions/CheckKeys.php b/src/Actions/CheckKeys.php index bce0d7a..e8de102 100644 --- a/src/Actions/CheckKeys.php +++ b/src/Actions/CheckKeys.php @@ -8,12 +8,12 @@ class CheckKeys { public function handle(array $keyData, array $envFiles, Collection $missingKeys): void { - collect($envFiles)->each(function ($envFile) use ($keyData, $missingKeys) { + collect($envFiles)->each(function ($envFile) use ($keyData, $missingKeys): void { $envContent = file($envFile); $keyExists = false; foreach ($envContent as $line) { - if (str_starts_with($line, $keyData['key'])) { + if (str_starts_with($line, (string) $keyData['key'])) { $keyExists = true; break; } diff --git a/src/Actions/GetKeys.php b/src/Actions/GetKeys.php index 821ca54..60dd1a0 100644 --- a/src/Actions/GetKeys.php +++ b/src/Actions/GetKeys.php @@ -16,7 +16,7 @@ public function handle(array|string $files, ?bool $withComments = false): Collec return $files ->map(function ($file) use ($ignoredKeys, $withComments) { - $collection = collect(file($file))->map(function ($line, $index) use ($file) { + $collection = collect(file($file))->map(function ($line, $index) use ($file): array { [$key] = explode('=', $line); return [ @@ -27,14 +27,10 @@ public function handle(array|string $files, ?bool $withComments = false): Collec }); if (! $withComments) { - $collection = $collection->filter(function ($item) { - return $item['key'] !== "\n" && ! str_starts_with($item['key'], '#'); - }); + $collection = $collection->filter(fn ($item): bool => $item['key'] !== "\n" && ! str_starts_with((string) $item['key'], '#')); } - return $collection->filter(function ($keyData) use ($ignoredKeys) { - return ! in_array($keyData['key'], $ignoredKeys); - }); + return $collection->reject(fn ($keyData): bool => in_array($keyData['key'], $ignoredKeys)); }) ->flatten(1) ->unique('key'); diff --git a/src/Commands/EnvInGitIgnoreCommand.php b/src/Commands/EnvInGitIgnoreCommand.php index d65d2e3..090d9bf 100644 --- a/src/Commands/EnvInGitIgnoreCommand.php +++ b/src/Commands/EnvInGitIgnoreCommand.php @@ -28,7 +28,7 @@ public function handle(): int $filesToCheck = config(key: 'env-keys-checker.gitignore_files', default: ['.env']); $missingFiles = collect(); - collect(value: $filesToCheck)->each(callback: function ($file) use ($gitIgnoreContent, $missingFiles) { + collect(value: $filesToCheck)->each(callback: function ($file) use ($gitIgnoreContent, $missingFiles): void { if (! in_array(needle: $file, haystack: $gitIgnoreContent)) { $missingFiles->push(values: $file); } diff --git a/src/Commands/EnvKeysSyncCommand.php b/src/Commands/EnvKeysSyncCommand.php index 27d1e5d..591e88f 100644 --- a/src/Commands/EnvKeysSyncCommand.php +++ b/src/Commands/EnvKeysSyncCommand.php @@ -31,15 +31,13 @@ public function handle(): int $envFiles = glob(pattern: base_path(path: '.env*')); $ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []); - if (empty($envFiles)) { + if ($envFiles === [] || $envFiles === false) { $this->showFailureInfo(message: 'No .env files found.'); return self::FAILURE; } - $envFiles = collect(value: $envFiles)->filter(callback: function ($file) use ($ignoredFiles) { - return ! in_array(needle: basename(path: $file), haystack: $ignoredFiles); - })->toArray(); + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename(path: $file), haystack: $ignoredFiles))->toArray(); if (empty($envFiles)) { $this->showFailureInfo(message: 'No .env files found.'); @@ -47,11 +45,9 @@ public function handle(): int return self::FAILURE; } - $envFiles = collect(value: $envFiles)->filter(callback: function ($file) { - return basename(path: $file) !== $this->getMasterEnv(); - }); + $envFiles = collect(value: $envFiles)->filter(callback: fn ($file): bool => basename(path: (string) $file) !== $this->getMasterEnv()); - $envFiles->each(callback: function ($envFile) { + $envFiles->each(callback: function ($envFile): void { $totalKeysFromMaster = count(value: file(filename: $this->getMasterEnv())); for ($line = 1; $line <= $totalKeysFromMaster; $line++) { $keyMaster = $this->getKeyFromFileOnLine(file: $this->getMasterEnv(), line: $line); @@ -122,11 +118,9 @@ private function checkIfEmptyLine(string $line): bool private function moveKeyToLine(string $file, string $key, int $toLine): void { $lines = file(filename: $file); - $keyLine = array_filter(array: $lines, callback: function ($line) use ($key) { - return str_starts_with($line, $key); - }); + $keyLine = array_filter(array: $lines, callback: fn ($line): bool => str_starts_with((string) $line, $key)); - if (empty($keyLine)) { + if ($keyLine === []) { return; } diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index 1a399cc..238df41 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -40,7 +40,7 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) return self::FAILURE; } - if (empty($envFiles)) { + if ($envFiles === [] || $envFiles === false) { if (! $this->option(key: 'no-display')) { $this->showFailureInfo(message: 'No .env files found.'); } @@ -48,9 +48,7 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) return self::FAILURE; } - $envFiles = collect(value: $envFiles)->filter(callback: function ($file) use ($ignoredFiles) { - return ! in_array(needle: basename($file), haystack: $ignoredFiles); - })->toArray(); + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename($file), haystack: $ignoredFiles))->toArray(); if (empty($envFiles)) { if (! $this->option(key: 'no-display')) { @@ -117,13 +115,11 @@ private function showMissingKeysTable(Collection $missingKeys): void { table( headers: ['Line', 'Key', 'Is missing in'], - rows: $missingKeys->map(callback: function ($missingKey) { - return [ - $missingKey['line'], - $missingKey['key'], - $missingKey['envFile'], - ]; - })->toArray() + rows: $missingKeys->map(callback: fn ($missingKey): array => [ + $missingKey['line'], + $missingKey['key'], + $missingKey['envFile'], + ])->toArray() ); } } diff --git a/src/LaravelEnvKeysCheckerServiceProvider.php b/src/LaravelEnvKeysCheckerServiceProvider.php index 10de06e..c50446e 100644 --- a/src/LaravelEnvKeysCheckerServiceProvider.php +++ b/src/LaravelEnvKeysCheckerServiceProvider.php @@ -5,11 +5,13 @@ use Msamgan\LaravelEnvKeysChecker\Commands\EnvInGitIgnoreCommand; use Msamgan\LaravelEnvKeysChecker\Commands\EnvKeysSyncCommand; use Msamgan\LaravelEnvKeysChecker\Commands\KeysCheckerCommand; +use Override; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; class LaravelEnvKeysCheckerServiceProvider extends PackageServiceProvider { + #[Override] public function configurePackage(Package $package): void { /* diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 5d36321..29b56ec 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,5 +1,5 @@ toBeTrue(); }); diff --git a/tests/LaravelEnvKeysCheckerCommandTest.php b/tests/LaravelEnvKeysCheckerCommandTest.php index 009899a..126b25e 100644 --- a/tests/LaravelEnvKeysCheckerCommandTest.php +++ b/tests/LaravelEnvKeysCheckerCommandTest.php @@ -1,6 +1,6 @@ artisan('env:keys-check --auto-add=none') ->assertExitCode(0); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index 2477fc4..2b2eb72 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,18 +5,21 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Msamgan\LaravelEnvKeysChecker\LaravelEnvKeysCheckerServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; +use Override; class TestCase extends Orchestra { + #[Override] protected function setUp(): void { parent::setUp(); Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\' . class_basename($modelName) . 'Factory' + fn (string $modelName): string => 'Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\' . class_basename($modelName) . 'Factory' ); } + #[Override] protected function getPackageProviders($app) { return [ @@ -24,7 +27,8 @@ protected function getPackageProviders($app) ]; } - public function getEnvironmentSetUp($app) + #[Override] + public function getEnvironmentSetUp($app): void { config()->set('database.default', 'testing'); From d83116a953670e6f36bb379f811e971a9b0e2e74 Mon Sep 17 00:00:00 2001 From: msamgan Date: Thu, 29 May 2025 20:25:38 -0400 Subject: [PATCH 2/8] PHP V --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f68efe8..7e1ddf2 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } ], "require": { - "php": "^8.3", + "php": "^8.3 || ^8.2", "spatie/laravel-package-tools": "^1.16", "illuminate/contracts": "^10.0||^11.0||^12.0" }, From fc5d10d7603c55a77e55c56fce5a8e48d937bd09 Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 10:11:26 -0400 Subject: [PATCH 3/8] pint and duster --- composer.json | 11 ++-- config/env-keys-checker.php | 2 + pint.json | 59 +++++++++++++++++++- src/Actions/AddKeys.php | 4 +- src/Actions/CheckKeys.php | 4 +- src/Actions/GetKeys.php | 6 +- src/Commands/EnvInGitIgnoreCommand.php | 4 +- src/Commands/EnvKeysSyncCommand.php | 6 +- src/Commands/KeysCheckerCommand.php | 13 +++-- src/Concerns/HelperFunctions.php | 2 + src/LaravelEnvKeysCheckerServiceProvider.php | 4 +- tests/ArchTest.php | 2 + tests/ExampleTest.php | 2 + tests/LaravelEnvKeysCheckerCommandTest.php | 2 + tests/Pest.php | 2 + tests/TestCase.php | 16 +++--- tlint.json | 3 + 17 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 tlint.json diff --git a/composer.json b/composer.json index 7e1ddf2..a1c5057 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,9 @@ ], "require": { "php": "^8.3 || ^8.2", + "illuminate/contracts": "^10.0||^11.0||^12.0", "spatie/laravel-package-tools": "^1.16", - "illuminate/contracts": "^10.0||^11.0||^12.0" + "tightenco/duster": "^3.2" }, "require-dev": { "driftingly/rector-laravel": "^1.2", @@ -45,14 +46,12 @@ }, "autoload": { "psr-4": { - "Msamgan\\LaravelEnvKeysChecker\\": "src/", - "Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\": "database/factories/" + "Msamgan\\LaravelEnvKeysChecker\\": "src/" } }, "autoload-dev": { "psr-4": { - "Msamgan\\LaravelEnvKeysChecker\\Tests\\": "tests/", - "Workbench\\App\\": "workbench/app/" + "Msamgan\\LaravelEnvKeysChecker\\Tests\\": "tests/" } }, "scripts": { @@ -71,7 +70,7 @@ "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/pest", "test-coverage": "vendor/bin/pest --coverage", - "format": "./vendor/bin/rector process && vendor/bin/pint" + "format": "./vendor/bin/rector process && vendor/bin/pint && ./vendor/bin/duster fix" }, "config": { "sort-packages": true, diff --git a/config/env-keys-checker.php b/config/env-keys-checker.php index 4a8f929..4a67e15 100644 --- a/config/env-keys-checker.php +++ b/config/env-keys-checker.php @@ -1,5 +1,7 @@ explode(',', (string) env('KEYS_CHECKER_IGNORE_FILES', '')), diff --git a/pint.json b/pint.json index 7eb5048..d2c1abc 100644 --- a/pint.json +++ b/pint.json @@ -1,11 +1,64 @@ { "preset": "laravel", + "notPath": [ + "tests/TestCase.php" + ], "rules": { + "array_push": true, + "backtick_to_shell_exec": true, + "date_time_immutable": true, + "declare_strict_types": true, + "lowercase_keywords": true, + "lowercase_static_reference": true, + "final_class": true, + "final_internal_class": true, + "final_public_method_for_abstract_class": true, + "fully_qualified_strict_types": true, + "global_namespace_import": { + "import_classes": true, + "import_constants": true, + "import_functions": true + }, + "mb_str_functions": true, + "modernize_types_casting": true, + "new_with_parentheses": false, + "no_superfluous_elseif": true, + "no_useless_else": true, + "no_multiple_statements_per_line": true, + "ordered_class_elements": { + "order": [ + "use_trait", + "case", + "constant", + "constant_public", + "constant_protected", + "constant_private", + "property_public", + "property_protected", + "property_private", + "construct", + "destruct", + "magic", + "phpunit", + "method_abstract", + "method_public_static", + "method_public", + "method_protected_static", + "method_protected", + "method_private_static", + "method_private" + ], + "sort_algorithm": "none" + }, + "ordered_interfaces": true, + "ordered_traits": true, + "protected_to_private": true, + "self_accessor": true, + "self_static_accessor": true, + "strict_comparison": true, + "visibility_required": true, "concat_space": { "spacing": "one" - }, - "ordered_imports": { - "sort_algorithm": "alpha" } } } diff --git a/src/Actions/AddKeys.php b/src/Actions/AddKeys.php index 9f72aaf..2b79911 100644 --- a/src/Actions/AddKeys.php +++ b/src/Actions/AddKeys.php @@ -1,10 +1,12 @@ filter(fn ($item): bool => $item['key'] !== "\n" && ! str_starts_with((string) $item['key'], '#')); + $collection = $collection->filter(fn ($item): bool => $item['key'] !== "\n" && ! str_starts_with($item['key'], '#')); } return $collection->reject(fn ($keyData): bool => in_array($keyData['key'], $ignoredKeys)); diff --git a/src/Commands/EnvInGitIgnoreCommand.php b/src/Commands/EnvInGitIgnoreCommand.php index 090d9bf..9051c56 100644 --- a/src/Commands/EnvInGitIgnoreCommand.php +++ b/src/Commands/EnvInGitIgnoreCommand.php @@ -1,11 +1,13 @@ str_starts_with((string) $line, $key)); + $keyLine = array_filter(array: $lines, callback: fn ($line): bool => str_starts_with($line, $key)); if ($keyLine === []) { return; diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index 238df41..7fc7e2b 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -1,20 +1,21 @@ expect(['dd', 'dump', 'ray']) ->each->not->toBeUsed(); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 29b56ec..f9c207b 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,5 +1,7 @@ toBeTrue(); }); diff --git a/tests/LaravelEnvKeysCheckerCommandTest.php b/tests/LaravelEnvKeysCheckerCommandTest.php index 126b25e..73ac4de 100644 --- a/tests/LaravelEnvKeysCheckerCommandTest.php +++ b/tests/LaravelEnvKeysCheckerCommandTest.php @@ -1,5 +1,7 @@ artisan('env:keys-check --auto-add=none') ->assertExitCode(0); diff --git a/tests/Pest.php b/tests/Pest.php index d880183..f0a7fb2 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php index 2b2eb72..ef28bd3 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,14 +19,6 @@ protected function setUp(): void ); } - #[Override] - protected function getPackageProviders($app) - { - return [ - LaravelEnvKeysCheckerServiceProvider::class, - ]; - } - #[Override] public function getEnvironmentSetUp($app): void { @@ -37,4 +29,12 @@ public function getEnvironmentSetUp($app): void $migration->up(); */ } + + #[Override] + protected function getPackageProviders($app) + { + return [ + LaravelEnvKeysCheckerServiceProvider::class, + ]; + } } diff --git a/tlint.json b/tlint.json new file mode 100644 index 0000000..9c53e11 --- /dev/null +++ b/tlint.json @@ -0,0 +1,3 @@ +{ + "disabled": ["QualifiedNamesOnlyForClassName", "RemoveLeadingSlashNamespaces"] +} From ae1dd1ac7c77b8b729b9da55c546eea7aaf5fe91 Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 10:12:17 -0400 Subject: [PATCH 4/8] dev dep. --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a1c5057..a024e3c 100644 --- a/composer.json +++ b/composer.json @@ -27,10 +27,10 @@ "require": { "php": "^8.3 || ^8.2", "illuminate/contracts": "^10.0||^11.0||^12.0", - "spatie/laravel-package-tools": "^1.16", - "tightenco/duster": "^3.2" + "spatie/laravel-package-tools": "^1.16" }, "require-dev": { + "tightenco/duster": "^3.2", "driftingly/rector-laravel": "^1.2", "larastan/larastan": "^2.9", "laravel/pint": "^1.14", From 8731946710215496aee6139ba996020010bed607 Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 11:21:14 -0400 Subject: [PATCH 5/8] removed .encrypted from list --- src/Commands/EnvKeysSyncCommand.php | 8 +++++--- src/Commands/KeysCheckerCommand.php | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Commands/EnvKeysSyncCommand.php b/src/Commands/EnvKeysSyncCommand.php index 85d5461..d739e32 100644 --- a/src/Commands/EnvKeysSyncCommand.php +++ b/src/Commands/EnvKeysSyncCommand.php @@ -39,7 +39,9 @@ public function handle(): int return self::FAILURE; } - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename(path: $file), haystack: $ignoredFiles))->toArray(); + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename(path: $file), haystack: (array) $ignoredFiles))->toArray(); + + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted'))->toArray(); if (empty($envFiles)) { $this->showFailureInfo(message: 'No .env files found.'); @@ -91,12 +93,12 @@ public function handle(): int private function getMasterEnv(): string { - return config(key: 'env-keys-checker.master_env', default: '.env'); + return (string) config(key: 'env-keys-checker.master_env', default: '.env'); } private function getKeyFromFileOnLine(string $file, int $line): string { - return file(filename: $file)[$line - 1]; + return file(filename: $file)[$line - 1] ?? ''; } private function checkIfComment(string $line): bool diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index 7fc7e2b..d4fcb7b 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -49,7 +49,9 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) return self::FAILURE; } - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename($file), haystack: $ignoredFiles))->toArray(); + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename($file), haystack: (array) $ignoredFiles))->toArray(); + + $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted'))->toArray(); if (empty($envFiles)) { if (! $this->option(key: 'no-display')) { From 54fe9095cf369aa2f7e6178b7d2f6356fc40721d Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 11:30:40 -0400 Subject: [PATCH 6/8] Refactoring. --- src/Actions/FilterFiles.php | 16 ++++++++++++++++ src/Commands/EnvKeysSyncCommand.php | 11 +++++------ src/Commands/KeysCheckerCommand.php | 11 +++++------ src/Concerns/HelperFunctions.php | 5 +++++ 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/Actions/FilterFiles.php diff --git a/src/Actions/FilterFiles.php b/src/Actions/FilterFiles.php new file mode 100644 index 0000000..37d9b63 --- /dev/null +++ b/src/Actions/FilterFiles.php @@ -0,0 +1,16 @@ +reject(callback: fn ($file): bool => in_array(needle: basename((string) $file), haystack: $ignoredFiles)) + ->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted')) + ->toArray(); + } +} diff --git a/src/Commands/EnvKeysSyncCommand.php b/src/Commands/EnvKeysSyncCommand.php index d739e32..625b204 100644 --- a/src/Commands/EnvKeysSyncCommand.php +++ b/src/Commands/EnvKeysSyncCommand.php @@ -5,6 +5,7 @@ namespace Msamgan\LaravelEnvKeysChecker\Commands; use Illuminate\Console\Command; +use Msamgan\LaravelEnvKeysChecker\Actions\FilterFiles; use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions; final class EnvKeysSyncCommand extends Command @@ -15,7 +16,7 @@ final class EnvKeysSyncCommand extends Command public $description = 'Sync keys from master .env file to other .env files.'; - public function handle(): int + public function handle(FilterFiles $filterFiles): int { $allKeysCheck = $this->call('env:keys-check', [ '--auto-add' => 'none', @@ -30,7 +31,7 @@ public function handle(): int return self::FAILURE; } - $envFiles = glob(pattern: base_path(path: '.env*')); + $envFiles = $this->getEnvs(); $ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []); if ($envFiles === [] || $envFiles === false) { @@ -39,11 +40,9 @@ public function handle(): int return self::FAILURE; } - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename(path: $file), haystack: (array) $ignoredFiles))->toArray(); + $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: (array) $ignoredFiles); - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted'))->toArray(); - - if (empty($envFiles)) { + if ($envFiles === []) { $this->showFailureInfo(message: 'No .env files found.'); return self::FAILURE; diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index d4fcb7b..21982f3 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -8,6 +8,7 @@ use Illuminate\Support\Collection; use Msamgan\LaravelEnvKeysChecker\Actions\AddKeys; use Msamgan\LaravelEnvKeysChecker\Actions\CheckKeys; +use Msamgan\LaravelEnvKeysChecker\Actions\FilterFiles; use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys; use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions; @@ -23,9 +24,9 @@ final class KeysCheckerCommand extends Command public $description = 'Check if all keys in .env file are present across all .env files. Like .env, .env.example, .env.testing, etc.'; - public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys): int + public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys, FilterFiles $filterFiles): int { - $envFiles = glob(base_path(path: '.env*')); + $envFiles = $this->getEnvs(); $ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []); $autoAddOption = $this->option(key: 'auto-add'); @@ -49,11 +50,9 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) return self::FAILURE; } - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => in_array(needle: basename($file), haystack: (array) $ignoredFiles))->toArray(); + $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: (array) $ignoredFiles); - $envFiles = collect(value: $envFiles)->reject(callback: fn ($file): bool => str_ends_with(haystack: basename((string) $file), needle: '.encrypted'))->toArray(); - - if (empty($envFiles)) { + if ($envFiles === []) { if (! $this->option(key: 'no-display')) { $this->showFailureInfo(message: 'No .env files found.'); } diff --git a/src/Concerns/HelperFunctions.php b/src/Concerns/HelperFunctions.php index 8271a4a..14ddba8 100644 --- a/src/Concerns/HelperFunctions.php +++ b/src/Concerns/HelperFunctions.php @@ -18,4 +18,9 @@ private function showFailureInfo(string $message): void { error(message: ' !! ' . $message); } + + private function getEnvs(): array + { + return glob(pattern: base_path(path: '.env*')); + } } From cdc28be8a1f71518868a9218b573d15415695fb0 Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 11:34:36 -0400 Subject: [PATCH 7/8] Refactor. --- src/Commands/EnvKeysSyncCommand.php | 4 ++-- src/Commands/KeysCheckerCommand.php | 4 ++-- src/Concerns/HelperFunctions.php | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Commands/EnvKeysSyncCommand.php b/src/Commands/EnvKeysSyncCommand.php index 625b204..debdbfc 100644 --- a/src/Commands/EnvKeysSyncCommand.php +++ b/src/Commands/EnvKeysSyncCommand.php @@ -32,7 +32,7 @@ public function handle(FilterFiles $filterFiles): int } $envFiles = $this->getEnvs(); - $ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []); + $ignoredFiles = $this->getFilesToIgnore(); if ($envFiles === [] || $envFiles === false) { $this->showFailureInfo(message: 'No .env files found.'); @@ -40,7 +40,7 @@ public function handle(FilterFiles $filterFiles): int return self::FAILURE; } - $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: (array) $ignoredFiles); + $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: $ignoredFiles); if ($envFiles === []) { $this->showFailureInfo(message: 'No .env files found.'); diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index 21982f3..db69091 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -28,7 +28,7 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys, { $envFiles = $this->getEnvs(); - $ignoredFiles = config(key: 'env-keys-checker.ignore_files', default: []); + $ignoredFiles = $this->getFilesToIgnore(); $autoAddOption = $this->option(key: 'auto-add'); $autoAddAvailableOptions = ['ask', 'auto', 'none']; @@ -50,7 +50,7 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys, return self::FAILURE; } - $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: (array) $ignoredFiles); + $envFiles = $filterFiles->handle(envFiles: $envFiles, ignoredFiles: $ignoredFiles); if ($envFiles === []) { if (! $this->option(key: 'no-display')) { diff --git a/src/Concerns/HelperFunctions.php b/src/Concerns/HelperFunctions.php index 14ddba8..72b6efd 100644 --- a/src/Concerns/HelperFunctions.php +++ b/src/Concerns/HelperFunctions.php @@ -23,4 +23,9 @@ private function getEnvs(): array { return glob(pattern: base_path(path: '.env*')); } + + private function getFilesToIgnore(): array + { + return (array) config(key: 'env-keys-checker.ignore_files', default: []); + } } From 5ceba047dc1951fa1d6fd5a172fe9e774ffa72a9 Mon Sep 17 00:00:00 2001 From: msamgan Date: Fri, 30 May 2025 11:46:22 -0400 Subject: [PATCH 8/8] updated the empty array check logic. --- src/Commands/EnvKeysSyncCommand.php | 2 +- src/Commands/KeysCheckerCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/EnvKeysSyncCommand.php b/src/Commands/EnvKeysSyncCommand.php index debdbfc..662c1cd 100644 --- a/src/Commands/EnvKeysSyncCommand.php +++ b/src/Commands/EnvKeysSyncCommand.php @@ -34,7 +34,7 @@ public function handle(FilterFiles $filterFiles): int $envFiles = $this->getEnvs(); $ignoredFiles = $this->getFilesToIgnore(); - if ($envFiles === [] || $envFiles === false) { + if ($envFiles === []) { $this->showFailureInfo(message: 'No .env files found.'); return self::FAILURE; diff --git a/src/Commands/KeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php index db69091..0933125 100644 --- a/src/Commands/KeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -42,7 +42,7 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys, return self::FAILURE; } - if ($envFiles === [] || $envFiles === false) { + if ($envFiles === []) { if (! $this->option(key: 'no-display')) { $this->showFailureInfo(message: 'No .env files found.'); }