Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mtolhuys/laravel-env-scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
mtolhuys committed Jul 31, 2019
2 parents c7970a9 + 667f427 commit 2d78dbc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
```
Expand Down
39 changes: 23 additions & 16 deletions src/Commands/EnvScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,39 +46,46 @@ public function handle()
}

$this->output->write(
"<fg=green>Scanning:</fg=green> <fg=white>{$this->scanner->dir}...</fg=white>\n"
"<fg=green>Scanning:</fg=green> <fg=white>{$this->scanner->dir}/...</fg=white>\n"
);

$this->scanner->scan();

$this->showOutput();
}

private function showOutput() {
private function showOutput(): void
{
foreach ($this->scanner->warnings as $warning) {
$this->warn("Warning: <fg=red>{$warning->invocation}</fg=red> 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(
"<fg=red>{$this->scanner->results['undefined']} undefined variables found in {$this->scanner->dir}/...</fg=red>"
);
$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(
"<fg=red>{$this->scanner->results['undefined']} undefined variable(s) found in {$this->scanner->dir}/...</fg=red>"
);
$this->table([], $this->scanner->undefined);
}
}
6 changes: 3 additions & 3 deletions src/LaravelEnvScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LaravelEnvScanner
'defined' => 0,
'undefined' => 0,
'depending_on_default' => 0,
'columns' => [],
'rows' => [],
];

/**
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -291,7 +291,7 @@ private function storeResult(): void
];
}

$this->results['columns'][] = $resultData;
$this->results['rows'][] = $resultData;
}

private function getFiles(): array
Expand Down
15 changes: 9 additions & 6 deletions tests/EnvScanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class EnvScanTest extends TestCase
{
private $results;

private $path;

public function getPackageProviders($app): array
{
return [
Expand All @@ -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
Expand Down Expand Up @@ -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'], '-');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2d78dbc

Please sign in to comment.