11<?php
22
3+ declare (strict_types=1 );
4+
35namespace Msamgan \LaravelEnvKeysChecker \Commands ;
46
57use Illuminate \Console \Command ;
8+ use Msamgan \LaravelEnvKeysChecker \Actions \FilterFiles ;
69use 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