Skip to content

Commit 25eee20

Browse files
committed
- Add file_extensions config to code analyzers: Allow customizable file extensions for each code analyzer to enable flexibility in supported file types.
- Refactor BaseCodeAnalyzerPreCommitHook: Simplify file extension checking and remove unused extension method from ChangedFile. - Update test datasets: Add file_extensions to datasets to reflect changes in configurations.
1 parent 2ee8951 commit 25eee20

11 files changed

+24
-19
lines changed

config/git-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@
172172
'path' => env('LARAVEL_PINT_PATH', 'vendor/bin/pint'),
173173
'config' => env('LARAVEL_PINT_CONFIG', 'pint.json'),
174174
'preset' => env('LARAVEL_PINT_PRESET', 'psr12'),
175+
'file_extensions' => env('LARAVEL_PINT_FILE_EXTENSIONS', '/\.php$/'),
175176
],
176177
'php_code_sniffer' => [
177178
'phpcs_path' => env('PHPCS_PATH', 'vendor/bin/phpcs'),
178179
'phpcbf_path' => env('PHPCBF_PATH', 'vendor/bin/phpcbf'),
179180
'standard' => env('PHPCS_STANDARD', 'phpcs.xml'),
181+
'file_extensions' => env('PHPCS_FILE_EXTENSIONS', '/\.php$/'),
180182
],
181183
'larastan' => [
182184
'path' => env('LARASTAN_PATH', 'vendor/bin/phpstan'),
@@ -186,16 +188,19 @@
186188
'blade_formatter' => [
187189
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
188190
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),
191+
'file_extensions' => env('BLADE_FORMATTER_FILE_EXTENSIONS', '/\.blade\.php$/'),
189192
],
190193
'prettier' => [
191194
'path' => env('PRETTIER_PATH', 'node_modules/.bin/prettier'),
192195
'config' => env('PRETTIER_CONFIG', '.prettierrc.json'),
193196
'additional_params' => env('PRETTIER_ADDITIONAL_PARAMS', ''),
197+
'file_extensions' => env('PRETTIER_FILE_EXTENSIONS', '/\.(jsx?|tsx?|vue)$/'),
194198
],
195199
'eslint' => [
196200
'path' => env('ESLINT_PATH', 'node_modules/.bin/eslint'),
197201
'config' => env('ESLINT_CONFIG', '.eslintrc.js'),
198202
'additional_params' => env('ESLINT_ADDITIONAL_PARAMS', ''),
203+
'file_extensions' => env('ESLINT_FILE_EXTENSIONS', '/\.(jsx?|tsx?|vue)$/'),
199204
],
200205
],
201206

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ protected function canFileBeAnalyzed(ChangedFile $file): bool
135135
return true;
136136
}
137137

138-
return (is_array($this->fileExtensions) && in_array($file->extension(), $this->fileExtensions)) ||
139-
(is_string($this->fileExtensions) && preg_match($this->fileExtensions, $file->getFilePath()));
138+
return is_string($this->fileExtensions) && preg_match($this->fileExtensions, $file->getFilePath());
140139
}
141140

142141
/**

src/Console/Commands/Hooks/BladeFormatterPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ChangedFiles $files, Closure $next)
3131
{
3232
$this->configParam = $this->configParam();
3333

34-
return $this->setFileExtensions('/\.blade\.php$/')
34+
return $this->setFileExtensions(config('git-hooks.code_analyzers.blade_formatter.file_extensions'))
3535
->setAnalyzerExecutable(config('git-hooks.code_analyzers.blade_formatter.path'), true)
3636
->handleCommittedFiles($files, $next);
3737
}

src/Console/Commands/Hooks/ESLintPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ChangedFiles $files, Closure $next)
3131
{
3232
$this->configParam = $this->configParam();
3333

34-
return $this->setFileExtensions('/\.(jsx?|tsx?|vue)$/')
34+
return $this->setFileExtensions(config('git-hooks.code_analyzers.eslint.file_extensions'))
3535
->setAnalyzerExecutable(config('git-hooks.code_analyzers.eslint.path'), true)
3636
->handleCommittedFiles($files, $next);
3737
}

src/Console/Commands/Hooks/PHPCodeSnifferPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ChangedFiles $files, Closure $next)
3131
{
3232
$this->configParam = $this->configParam();
3333

34-
return $this->setFileExtensions(['php'])
34+
return $this->setFileExtensions(config('git-hooks.code_analyzers.php_code_sniffer.file_extensions'))
3535
->setAnalyzerExecutable(config('git-hooks.code_analyzers.php_code_sniffer.phpcs_path'))
3636
->setFixerExecutable(config('git-hooks.code_analyzers.php_code_sniffer.phpcbf_path'))
3737
->handleCommittedFiles($files, $next);

src/Console/Commands/Hooks/PintPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ChangedFiles $files, Closure $next)
3131
{
3232
$this->configParam = $this->configParam();
3333

34-
return $this->setFileExtensions(['php'])
34+
return $this->setFileExtensions(config('git-hooks.code_analyzers.laravel_pint.file_extensions'))
3535
->setAnalyzerExecutable(config('git-hooks.code_analyzers.laravel_pint.path'), true)
3636
->handleCommittedFiles($files, $next);
3737
}

src/Console/Commands/Hooks/PrettierPreCommitHook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ChangedFiles $files, Closure $next)
3131
{
3232
$this->configParam = $this->configParam();
3333

34-
return $this->setFileExtensions('/\.(jsx?|tsx?|vue)$/')
34+
return $this->setFileExtensions(config('git-hooks.code_analyzers.prettier.file_extensions'))
3535
->setAnalyzerExecutable(config('git-hooks.code_analyzers.prettier.path'), true)
3636
->handleCommittedFiles($files, $next);
3737
}

src/Git/ChangedFile.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ public function isCopied(): bool
116116
return $this->X & static::C || $this->Y & static::C;
117117
}
118118

119-
public function extension()
120-
{
121-
return pathinfo($this->getFilePath(), PATHINFO_EXTENSION);
122-
}
123-
124119
/**
125120
* @return string
126121
*/

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66
use Igorsgm\GitHooks\Console\Commands\Hooks\PintPreCommitHook;
77
use Igorsgm\GitHooks\Console\Commands\Hooks\PrettierPreCommitHook;
88

9-
dataset('pintConfigurations', [
9+
dataset('pintConfiguration', [
1010
'Config File' => [
1111
[
1212
'path' => '../../../bin/pint',
1313
'config' => __DIR__.'/../Fixtures/pintFixture.json',
14+
'file_extensions' => '/\.php$/',
1415
],
1516
],
1617
'Preset' => [
1718
[
1819
'path' => '../../../bin/pint',
1920
'preset' => 'psr12',
21+
'file_extensions' => '/\.php$/',
2022
],
2123
],
2224
]);
@@ -27,6 +29,7 @@
2729
'phpcs_path' => '../../../bin/phpcs',
2830
'phpcbf_path' => '../../../bin/phpcbf',
2931
'standard' => __DIR__.'/../Fixtures/phpcsFixture.xml',
32+
'file_extensions' => '/\.php$/',
3033
],
3134
],
3235
]);
@@ -36,6 +39,7 @@
3639
[
3740
'path' => '../../../../node_modules/.bin/blade-formatter',
3841
'config' => __DIR__.'/../Fixtures/bladeFormatterFixture.json',
42+
'file_extensions' => '/\.blade\.php$/',
3943
],
4044
],
4145
]);
@@ -56,6 +60,7 @@
5660
'path' => '../../../../node_modules/.bin/prettier',
5761
'config' => __DIR__.'/../Fixtures/.prettierrcFixture.json',
5862
'additional_params' => '--config --find-config-path',
63+
'file_extensions' => '/\.(jsx?|tsx?|vue)$/',
5964
],
6065
],
6166
]);
@@ -66,6 +71,7 @@
6671
'path' => '../../../../node_modules/.bin/eslint',
6772
'config' => __DIR__.'/../Fixtures/.eslintrcFixture.js',
6873
'additional_params' => '--config',
74+
'file_extensions' => '/\.(jsx?|tsx?|vue)$/',
6975
],
7076
],
7177
]);

tests/Features/Commands/Hooks/BladeFormatterPreCommitHookTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
$this->initializeTempDirectory(base_path('temp'));
1111
});
1212

13-
test('Skips Blade Formatter check when there is none .blade.php files added to commit', function ($phpCSConfiguration) {
14-
$this->config->set('git-hooks.code_analyzers.php_code_sniffer', $phpCSConfiguration);
13+
test('Skips Blade Formatter check when there is none .blade.php files added to commit', function ($bladeFormatterConfiguration) {
14+
$this->config->set('git-hooks.code_analyzers.blade_formatter', $bladeFormatterConfiguration);
1515
$this->config->set('git-hooks.pre-commit', [
1616
BladeFormatterPreCommitHook::class,
1717
]);
@@ -24,7 +24,7 @@
2424
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn('AM src/CommitMessageFixtureHook1.php');
2525

2626
$this->artisan('git-hooks:pre-commit')->assertSuccessful();
27-
})->with('phpcsConfiguration');
27+
})->with('bladeFormatterConfiguration');
2828

2929
test('Fails commit when Blade Formatter is not passing and user does not autofix the files',
3030
function ($bladeFormatterConfiguration, $listOfFixablePhpFiles) {

0 commit comments

Comments
 (0)