Skip to content

Commit 91a0a88

Browse files
committed
- Update phpcs config key and add validateConfigPath: Rename PHPCS_STANDARD to PHPCS_STANDARD_CONFIG for clarity; add a method to validate config file paths for all code analyzers.
- Rename checkAnalyzerInstallation to validateAnalyzerInstallation: Improve method name to better reflect its purpose. - Improve error message formatting: Enhance readability of error messages in BaseCodeAnalyzerPreCommitHook.
1 parent 25eee20 commit 91a0a88

10 files changed

+51
-7
lines changed

config/git-hooks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
'php_code_sniffer' => [
178178
'phpcs_path' => env('PHPCS_PATH', 'vendor/bin/phpcs'),
179179
'phpcbf_path' => env('PHPCBF_PATH', 'vendor/bin/phpcbf'),
180-
'standard' => env('PHPCS_STANDARD', 'phpcs.xml'),
180+
'config' => env('PHPCS_STANDARD_CONFIG', 'phpcs.xml'),
181181
'file_extensions' => env('PHPCS_FILE_EXTENSIONS', '/\.php$/'),
182182
],
183183
'larastan' => [

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next)
7979
return $next($files);
8080
}
8181

82-
$this->checkAnalyzerInstallation()
82+
$this->validateAnalyzerInstallation()
8383
->analizeCommittedFiles($commitFiles);
8484

8585
if (empty($this->filesBadlyFormattedPaths)) {
@@ -148,8 +148,10 @@ protected function commitFailMessage()
148148
$this->command->newLine();
149149

150150
$message = '<bg=red;fg=white> COMMIT FAILED </> ';
151-
$message .= sprintf("Your commit contains files that should pass %s but do not. Please fix the errors in the files above and try again.\n", $this->getName());
152-
$message .= sprintf('You can check which %s errors happened in them by executing: <comment>%s {filePath}</comment>', $this->getName(), $this->analyzerCommand());
151+
$message .= sprintf("Your commit contains files that should pass %s but do not. Please fix the errors in the files above and try again.\n",
152+
$this->getName());
153+
$message .= sprintf('You can check which %s errors happened in them by executing: <comment>%s {filePath}</comment>',
154+
$this->getName(), $this->analyzerCommand());
153155

154156
$this->command->getOutput()->writeln($message);
155157

@@ -163,7 +165,7 @@ protected function commitFailMessage()
163165
*
164166
* @throws HookFailException
165167
*/
166-
protected function checkAnalyzerInstallation()
168+
protected function validateAnalyzerInstallation()
167169
{
168170
if (file_exists($this->analyzerExecutable)) {
169171
return $this;
@@ -179,6 +181,22 @@ protected function checkAnalyzerInstallation()
179181
throw new HookFailException();
180182
}
181183

184+
protected function validateConfigPath($path)
185+
{
186+
if (file_exists($path)) {
187+
return $this;
188+
}
189+
190+
$this->command->newLine(2);
191+
$this->command->getOutput()->writeln(
192+
sprintf('<bg=red;fg=white> ERROR </> %s config file does not exist. Please check the path and try again.',
193+
$this->getName())
194+
);
195+
$this->command->newLine();
196+
197+
throw new HookFailException();
198+
}
199+
182200
/**
183201
* Suggests attempting to automatically fix the incorrectly formatted files or exit.
184202
*

src/Console/Commands/Hooks/BladeFormatterPreCommitHook.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function fixerCommand(): string
6262
public function configParam(): string
6363
{
6464
$bladeFormatterConfig = rtrim(config('git-hooks.code_analyzers.blade_formatter.config'), '/');
65+
$this->validateConfigPath($bladeFormatterConfig);
6566

6667
return empty($bladeFormatterConfig) ? '' : '--config='.$bladeFormatterConfig;
6768
}

src/Console/Commands/Hooks/ESLintPreCommitHook.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function fixerCommand(): string
6666
protected function configParam(): string
6767
{
6868
$eslintConfig = rtrim(config('git-hooks.code_analyzers.eslint.config'), '/');
69+
$this->validateConfigPath($eslintConfig);
6970

7071
return empty($eslintConfig) ? '' : '--config='.$eslintConfig;
7172
}

src/Console/Commands/Hooks/LarastanPreCommitHook.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function fixerCommand(): string
7070
protected function configParam(): string
7171
{
7272
$phpStanConfigFile = rtrim(config('git-hooks.code_analyzers.larastan.config'), '/');
73+
$this->validateConfigPath($phpStanConfigFile);
7374

7475
return empty($phpStanConfigFile) ? '' : '--configuration='.$phpStanConfigFile;
7576
}

src/Console/Commands/Hooks/PHPCodeSnifferPreCommitHook.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public function fixerCommand(): string
6262
*/
6363
public function configParam(): string
6464
{
65-
$phpCSStandard = rtrim(config('git-hooks.code_analyzers.php_code_sniffer.standard'), '/');
65+
$phpCSStandard = rtrim(config('git-hooks.code_analyzers.php_code_sniffer.config'), '/');
66+
$this->validateConfigPath($phpCSStandard);
6667

6768
return empty($phpCSStandard) ? '' : '--standard='.$phpCSStandard;
6869
}

src/Console/Commands/Hooks/PintPreCommitHook.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ protected function configParam(): string
6262
$pintConfigFile = config('git-hooks.code_analyzers.laravel_pint.config');
6363

6464
if (! empty($pintConfigFile)) {
65+
$this->validateConfigPath($pintConfigFile);
66+
6567
return '--config '.trim($pintConfigFile, '/');
6668
}
6769

src/Console/Commands/Hooks/PrettierPreCommitHook.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function fixerCommand(): string
6464
protected function configParam(): string
6565
{
6666
$prettierConfig = rtrim(config('git-hooks.code_analyzers.prettier.config'), '/');
67+
$this->validateConfigPath($prettierConfig);
6768

6869
return empty($prettierConfig) ? '' : '--config='.$prettierConfig;
6970
}

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
[
2929
'phpcs_path' => '../../../bin/phpcs',
3030
'phpcbf_path' => '../../../bin/phpcbf',
31-
'standard' => __DIR__.'/../Fixtures/phpcsFixture.xml',
31+
'config' => __DIR__.'/../Fixtures/phpcsFixture.xml',
3232
'file_extensions' => '/\.php$/',
3333
],
3434
],
@@ -79,6 +79,7 @@
7979
$nonExistentPath = [
8080
'path' => 'nonexistent/path',
8181
'phpcs_path' => 'nonexistent/path',
82+
'config' => __DIR__.'/../Fixtures/pintFixture.json',
8283
];
8384

8485
dataset('codeAnalyzersList', [

tests/Features/Commands/Hooks/BaseCodeAnalyzerPreCommitHookTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,21 @@ function ($configName, $nonExistentPathConfig, $preCommitHookClass, $listOfFixab
5555
->expectsOutputToContain($preCommitHook->getName().' is not installed.')
5656
->assertExitCode(1);
5757
})->with('codeAnalyzersList', 'listOfFixablePhpFiles');
58+
59+
test('Throws HookFailException and notifies when config path does not exist',
60+
function ($configName, $nonExistentPathConfig, $preCommitHookClass, $listOfFixablePhpFiles) {
61+
$nonExistentPathConfig['config'] = 'nonexistent/path';
62+
$this->config->set('git-hooks.code_analyzers.'.$configName, $nonExistentPathConfig);
63+
64+
$this->config->set('git-hooks.pre-commit', [
65+
$preCommitHookClass,
66+
]);
67+
68+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(false);
69+
GitHooks::shouldReceive('getListOfChangedFiles')->andReturn($listOfFixablePhpFiles);
70+
71+
$preCommitHook = new $preCommitHookClass();
72+
$this->artisan('git-hooks:pre-commit')
73+
->expectsOutputToContain($preCommitHook->getName().' config file does not exist.')
74+
->assertExitCode(1);
75+
})->with('codeAnalyzersList', 'listOfFixablePhpFiles');

0 commit comments

Comments
 (0)