diff --git a/README.md b/README.md index 727e1a6..4c67a1d 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,13 @@ This package comes with a `LaravelEnvScanner` class and artisan command which yo Example output of the command: ```bash -$ php artisan env:scan +php artisan env:scan Scanning: laravel-app/config... -+------------------------------------+----------------+---------------------------+-------------------+ -| Locations (2) | Defined (1) | Depending on default (1) | Undefined (0) | -+------------------------------------+----------------+---------------------------+-------------------+ -| laravel-app/config/database.php:36 | DB_CONNECTION | - | - | -| laravel-app/config/database.php:42 | - | DB_HOST | - | -+------------------------------------+----------------+---------------------------+-------------------+ +2 undefined variable(s) found in laravel-app/config/... ++-------------------------------+----------+ +| laravel-app/config/app.php:16 | APP_NAME | +| laravel-app/config/app.php:29 | APP_ENV | ++-------------------------------+----------+ ``` ## Installation @@ -42,21 +41,24 @@ php artisan env:scan -d app/Http/Controllers Scanning: app/Http/Controllers... ``` -Or only look for undefined variables: +Or show all used variables: ```bash -php artisan env:scan -u +php artisan env:scan -a Scanning: laravel-app/config... -+-------------------------------+----------+ -| laravel-app/config/app.php:16 | APP_NAME | -| laravel-app/config/app.php:29 | APP_ENV | -+-------------------------------+----------+ ++------------------------------------+----------------+---------------------------+-------------------+ +| Locations (2) | Defined (1) | Depending on default (1) | Undefined (0) | ++------------------------------------+----------------+---------------------------+-------------------+ +| laravel-app/config/database.php:36 | DB_CONNECTION | - | - | +| laravel-app/config/database.php:42 | - | DB_HOST | - | ++------------------------------------+----------------+---------------------------+-------------------+ + -php artisan env:scan -u -d app +php artisan env:scan -d app Scanning: app... Warning: env("RISKY_".$behavior) found in app/Http/Middleware/Authenticate.php - -php artisan env:scan -u -d storage + +php artisan env:scan -d storage Scanning: storage... Looking good! ``` diff --git a/src/Commands/EnvScan.php b/src/Commands/EnvScan.php index 442cfaf..083ddad 100644 --- a/src/Commands/EnvScan.php +++ b/src/Commands/EnvScan.php @@ -16,7 +16,7 @@ class EnvScan extends Command protected $signature = ' env:scan { --d|dir= : Specify directory to scan (defaults to your config folder) } - { --u|undefined-only : Only show undefined variables as output } + { --a|all : Show result containing all used variables } '; private $scanner; @@ -46,7 +46,7 @@ public function handle() } $this->output->write( - "Scanning: {$this->scanner->dir}...\n" + "Scanning: {$this->scanner->dir}/...\n" ); $this->scanner->scan(); @@ -54,31 +54,38 @@ public function handle() $this->showOutput(); } - private function showOutput() { + private function showOutput(): void + { foreach ($this->scanner->warnings as $warning) { $this->warn("Warning: {$warning->invocation} found in {$warning->location}"); } - if (empty($this->scanner->warnings) && $this->scanner->results['undefined'] === 0) { - $this->info('Looking good!'); + if ($this->option('all')) { + if (empty($this->scanner->results['rows'])) { + $this->line('Nothing there...'); + + return; + } + + $this->table([ + "Locations ({$this->scanner->results['locations']})", + "Defined ({$this->scanner->results['defined']})", + "Depending on default ({$this->scanner->results['depending_on_default']})", + "Undefined ({$this->scanner->results['undefined']})", + ], $this->scanner->results['rows']); return; } - if ($this->option('undefined-only')) { - $this->warn( - "{$this->scanner->results['undefined']} undefined variables found in {$this->scanner->dir}/..." - ); - $this->table([], $this->scanner->undefined); + if (empty($this->scanner->warnings) && $this->scanner->results['undefined'] === 0) { + $this->info('Looking good!'); return; } - $this->table([ - "Locations ({$this->scanner->results['locations']})", - "Defined ({$this->scanner->results['defined']})", - "Depending on default ({$this->scanner->results['depending_on_default']})", - "Undefined ({$this->scanner->results['undefined']})", - ], $this->scanner->results['columns']); + $this->warn( + "{$this->scanner->results['undefined']} undefined variable(s) found in {$this->scanner->dir}/..." + ); + $this->table([], $this->scanner->undefined); } } diff --git a/src/LaravelEnvScanner.php b/src/LaravelEnvScanner.php index d52ae92..5a4abdd 100644 --- a/src/LaravelEnvScanner.php +++ b/src/LaravelEnvScanner.php @@ -18,7 +18,7 @@ class LaravelEnvScanner 'defined' => 0, 'undefined' => 0, 'depending_on_default' => 0, - 'columns' => [], + 'rows' => [], ]; /** @@ -203,7 +203,7 @@ private function searchMultiLine(array $lines, int $number) * * @param array $matches */ - private function setInvocation(array $matches) + private function setInvocation(array $matches): void { $this->invocation = str_replace(' ', '', str_replace( ' ', '', $matches[0] @@ -291,7 +291,7 @@ private function storeResult(): void ]; } - $this->results['columns'][] = $resultData; + $this->results['rows'][] = $resultData; } private function getFiles(): array diff --git a/tests/EnvScanTest.php b/tests/EnvScanTest.php index 6d497de..00d6660 100644 --- a/tests/EnvScanTest.php +++ b/tests/EnvScanTest.php @@ -11,6 +11,8 @@ class EnvScanTest extends TestCase { private $results; + private $path; + public function getPackageProviders($app): array { return [ @@ -29,6 +31,8 @@ public function getPackageProviders($app): array private function scanning_for_env(string $dir = null) { $this->results = (new LaravelEnvScanner($dir))->scan()->results; + $this->path = basename(__DIR__).'/'.basename(__FILE__); + $risky = 'USAGE'; // Defined @@ -66,9 +70,9 @@ public function it_checks_if_example_env_scan_results_are_correct() $this->assertSame($this->results['defined'], 4); $this->assertSame($this->results['depending_on_default'], 3); $this->assertSame($this->results['undefined'], 2); - $this->assertContains( __FILE__, $this->results['columns'][0]['location']); + $this->assertContains( $this->path, $this->results['rows'][0]['location']); - foreach ($this->results['columns'] as $result) { + foreach ($this->results['rows'] as $result) { if ($result['defined'] !== '-') { $this->assertSame($result['depending_on_default'], '-'); $this->assertSame($result['undefined'], '-'); @@ -102,14 +106,13 @@ public function it_checks_if_command_output_is_correct_with_undefined_only_optio { Artisan::call('env:scan', [ '--dir' => __DIR__, - '--undefined-only' => 'true', ]); $output = Artisan::output(); - $this->assertContains('(\'POTENTIALLY_\'.$risky,\'behavior\') found in '. __FILE__, $output); - $this->assertContains('($risky) found in ' . __FILE__, $output); - $this->assertContains('2 undefined variables found in ' . __DIR__, $output); + $this->assertContains('(\'POTENTIALLY_\'.$risky,\'behavior\') found in '. $this->path, $output); + $this->assertContains('($risky) found in ' . $this->path, $output); + $this->assertContains('2 undefined variable(s) found in ' . $this->path, $output); $this->assertContains('UNDEFINED', $output); $this->assertContains('GET_UNDEFINED', $output); $this->assertNotContains('FILLED', $output);