Skip to content

Commit

Permalink
Merge pull request #11 from chrisdicarlo/use-config-directly
Browse files Browse the repository at this point in the history
Refactor to check config directly
  • Loading branch information
chrisdicarlo authored Oct 7, 2024
2 parents 16455de + f1c1e99 commit 0415822
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 41 deletions.
14 changes: 4 additions & 10 deletions src/Commands/LaravelConfigCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use ChrisDiCarlo\LaravelConfigChecker\Resolvers\BladeFileResolver;
use ChrisDiCarlo\LaravelConfigChecker\Resolvers\PhpFileResolver;
use ChrisDiCarlo\LaravelConfigChecker\Support\FileChecker;
use ChrisDiCarlo\LaravelConfigChecker\Support\LoadConfigKeys;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

Expand All @@ -26,8 +25,6 @@ class LaravelConfigCheckerCommand extends Command

public $description = 'Check all references to config values in PHP and Blade files';

private Collection $configKeys;

private array $bladeIssues = [];

private array $phpIssues = [];
Expand All @@ -41,12 +38,9 @@ public function getIssues(): Collection
}

public function handle(
LoadConfigKeys $loadConfigKeys,
PhpFileResolver $phpFiles,
BladeFileResolver $bladeFiles
): int {
$this->configKeys = $loadConfigKeys();

if ($this->option('no-progress')) {
intro('--no-progress option used. Disabling progress bar.');

Expand All @@ -57,7 +51,7 @@ public function handle(
foreach ($phpFiles as $file) {
$content = file_get_contents($file->getRealPath());

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

foreach ($fileChecker->check() as $issue) {
$this->phpIssues[$file->getRelativePathname()][] = $issue;
Expand All @@ -70,7 +64,7 @@ public function handle(
$bladeFiles = $bladeFiles->resolve();
foreach ($bladeFiles as $file) {
$content = file_get_contents($file->getRealPath());
$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

foreach ($fileChecker->check() as $issue) {
$this->bladeIssues[$file->getRelativePathname()][] = $issue;
Expand All @@ -87,7 +81,7 @@ public function handle(

$content = file_get_contents($file->getRealPath());

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

foreach ($fileChecker->check() as $issue) {
$this->phpIssues[$file->getRelativePathname()][] = $issue;
Expand All @@ -104,7 +98,7 @@ public function handle(
$progress->hint = "Checking {$file->getRelativePathname()}";

$content = file_get_contents($file->getRealPath());
$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

foreach ($fileChecker->check() as $issue) {
$this->bladeIssues[$file->getRelativePathname()][] = $issue;
Expand Down
6 changes: 3 additions & 3 deletions src/Support/FileChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace ChrisDiCarlo\LaravelConfigChecker\Support;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

class FileChecker
{
public function __construct(
private readonly Collection $configKeys,
private readonly string $content,
) {}

Expand Down Expand Up @@ -46,7 +46,7 @@ private function checkForFacadeUsage(string $content): array
$offset = (int) $match[1];
$lineNumber = substr_count(substr($content, 0, $offset), "\n") + 1;

if ($this->configKeys->doesntContain($key)) {
if (! Config::has($key)) {
$issues[] = [
'key' => $key,
'type' => sprintf('Config::%s()', $matches[1][$index][0]),
Expand All @@ -71,7 +71,7 @@ private function checkForHelperUsage(string $content): array
$offset = (int) $match[1];
$lineNumber = substr_count(substr($content, 0, $offset), "\n") + 1;

if ($this->configKeys->doesntContain($key)) {
if (! Config::has($key)) {
$issues[] = [
'key' => $key,
'type' => 'config()',
Expand Down
141 changes: 115 additions & 26 deletions tests/FileCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use ChrisDiCarlo\LaravelConfigChecker\Support\FileChecker;
use ChrisDiCarlo\LaravelConfigChecker\Support\FileCheckInfo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

beforeEach(function () {
$this->configKeys = collect([
'file',
'file.valid_key',
'file.nested',
'file.nested.key',
Config::set('file', [
'valid_key' => 'value',
'nested' => [
'key' => 'value',
],
]);
});
it('returns a collection of FileCheckInfo objects', function () {
Expand All @@ -20,7 +21,7 @@
Config::get("invalid_key");
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -34,7 +35,7 @@
echo "Hello, World!";
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -44,7 +45,7 @@
{{ "Hello, World!" }}
BLADE;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -59,7 +60,7 @@
config("file.valid_key");
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -71,7 +72,7 @@
{{ config("file.valid_key") }}
BLADE;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -89,7 +90,7 @@
config("file.valid_key");
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand Down Expand Up @@ -117,7 +118,7 @@
{{ config("file.valid_key") }}
BLADE;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -140,7 +141,7 @@
it('handles empty content gracefully', function () {
$content = '';

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand All @@ -158,12 +159,34 @@
config("file.valid_key");
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

expect($issues->contains('key', 'file.valid_key'))->toBeFalse();
expect($issues->contains('key', 'file.invalid_key'))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'Config::get()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'Config::has()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'config()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::get()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::has()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'config()';
}))->toBeFalse();

$content = <<<'BLADE'
{{ Config::get("file.invalid_key") }}
Expand All @@ -174,12 +197,34 @@
{{ config("file.valid_key") }}
BLADE;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

expect($issues->contains('key', 'file.valid_key'))->toBeFalse();
expect($issues->contains('key', 'file.invalid_key'))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'Config::get()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'Config::has()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.invalid_key' &&
$issue->type === 'config()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::get()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::has()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'config()';
}))->toBeFalse();
});

it('detects issues for invalid nested keys', function () {
Expand All @@ -193,12 +238,34 @@
config("file.valid_key");
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

expect($issues->contains('key', 'file.valid_key'))->toBeFalse();
expect($issues->contains('key', 'file.nested.invalid_key'))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'Config::get()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'Config::has()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'config()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::get()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::has()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'config()';
}))->toBeFalse();

$content = <<<'BLADE'
{{ Config::get("file.nested.invalid_key") }}
Expand All @@ -209,12 +276,34 @@
{{ config("file.valid_key") }}
BLADE;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

expect($issues->contains('key', 'file.valid_key'))->toBeFalse();
expect($issues->contains('key', 'file.nested.invalid_key'))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'Config::get()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'Config::has()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.nested.invalid_key' &&
$issue->type === 'config()';
}))->toBeTrue();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::get()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'Config::has()';
}))->toBeFalse();
expect($issues->contains(function ($issue) {
return $issue->key === 'file.valid_key' &&
$issue->type === 'config()';
}))->toBeFalse();
});

it('ignores methods called config', function () {
Expand All @@ -227,7 +316,7 @@
Config::get('some.other.invalid.key');
PHP;

$fileChecker = new FileChecker($this->configKeys, $content);
$fileChecker = new FileChecker($content);

$issues = $fileChecker->check();

Expand Down
Loading

0 comments on commit 0415822

Please sign in to comment.