Skip to content

Commit 2e96903

Browse files
committed
Merge branch 'feature/blade-formatter-pre-commit-hook' into develop
2 parents 57de3d5 + ffa4892 commit 2e96903

14 files changed

+326
-102
lines changed

config/git-hooks.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@
178178
'phpcbf_path' => env('PHPCBF_PATH', 'vendor/bin/phpcbf'),
179179
'standard' => env('PHPCS_STANDARD', 'phpcs.xml'),
180180
],
181+
'blade_formatter' => [
182+
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
183+
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),
184+
],
181185
],
182186

183187
];

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
77
use Igorsgm\GitHooks\Facades\GitHooks;
8+
use Igorsgm\GitHooks\Git\ChangedFile;
89
use Igorsgm\GitHooks\Git\ChangedFiles;
910
use Igorsgm\GitHooks\Traits\ProcessHelper;
1011
use Illuminate\Console\Command;
@@ -21,9 +22,16 @@ abstract class BaseCodeAnalyzerPreCommitHook
2122
*/
2223
public $command;
2324

25+
/**
26+
* Name of the hook
27+
* @var string
28+
*/
29+
protected $name;
30+
2431
/*
25-
* List of files extensions that will be analyzed by the hook
26-
* @var array
32+
* List of files extensions that will be analyzed by the hook.
33+
* Can also be a regular expression.
34+
* @var array|string
2735
*/
2836
public $fileExtensions = [];
2937

@@ -89,8 +97,12 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next)
8997
*/
9098
protected function analizeCommittedFiles($commitFiles)
9199
{
100+
/** @var ChangedFile $file */
92101
foreach ($commitFiles as $file) {
93-
if (! in_array($file->extension(), $this->fileExtensions)) {
102+
if (
103+
(is_array($this->fileExtensions) && ! in_array($file->extension(), $this->fileExtensions)) ||
104+
(is_string($this->fileExtensions) && ! preg_match($this->fileExtensions, $file->getFilePath()))
105+
) {
94106
continue;
95107
}
96108

@@ -177,13 +189,21 @@ protected function suggestAutoFixOrExit()
177189
}
178190
}
179191

192+
/**
193+
* Get the name of the hook.
194+
*/
195+
public function getName(): ?string
196+
{
197+
return $this->name;
198+
}
199+
180200
/**
181201
* @param array|string $fileExtensions
182202
* @return BaseCodeAnalyzerPreCommitHook
183203
*/
184204
public function setFileExtensions($fileExtensions)
185205
{
186-
$this->fileExtensions = (array) $fileExtensions;
206+
$this->fileExtensions = $fileExtensions;
187207

188208
return $this;
189209
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Git\ChangedFiles;
8+
9+
class BladeFormatterPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $configParam;
15+
16+
/**
17+
* Name of the hook
18+
*
19+
* @var string
20+
*/
21+
protected $name = 'Blade Formatter';
22+
23+
/**
24+
* Analyze and fix committed blade.php files using blade-formatter npm package
25+
*
26+
* @param ChangedFiles $files The files that have been changed in the current commit.
27+
* @param Closure $next A closure that represents the next middleware in the pipeline.
28+
* @return mixed|null
29+
*/
30+
public function handle(ChangedFiles $files, Closure $next)
31+
{
32+
$this->configParam = $this->configParam();
33+
34+
return $this->setFileExtensions('/\.blade\.php$/')
35+
->setAnalyzerExecutable(config('git-hooks.code_analyzers.blade_formatter.path'), true)
36+
->handleCommittedFiles($files, $next);
37+
}
38+
39+
/**
40+
* Returns the command to run Blade Formatter tester
41+
*/
42+
public function analyzerCommand(): string
43+
{
44+
return trim(sprintf('%s -c %s', $this->getAnalyzerExecutable(), $this->configParam));
45+
}
46+
47+
/**
48+
* Returns the command to run Blade Formatter fixer
49+
*/
50+
public function fixerCommand(): string
51+
{
52+
return trim(sprintf('%s --write %s', $this->getFixerExecutable(), $this->configParam));
53+
}
54+
55+
/**
56+
* Returns the configuration parameter for the analyzer.
57+
* This method retrieves the Blade Formatter configuration path (usually .bladeformatterrc.json or .bladeformatterrc)
58+
* from the Git hooks configuration file and returns it as a string in the format '--config=<configFile>'.
59+
*
60+
* @return string The configuration parameter for the analyzer.
61+
*/
62+
public function configParam(): string
63+
{
64+
$bladeFormatterConfig = rtrim(config('git-hooks.code_analyzers.blade_formatter.config'), '/');
65+
66+
return empty($bladeFormatterConfig) ? '' : '--config='.$bladeFormatterConfig;
67+
}
68+
}

src/Console/Commands/Hooks/PHPCodeSnifferPreCommitHook.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ class PHPCodeSnifferPreCommitHook extends BaseCodeAnalyzerPreCommitHook implemen
1111
/**
1212
* @var string
1313
*/
14-
protected $analyzerConfigParam;
14+
protected $configParam;
1515

1616
/**
17-
* Get the name of the hook.
17+
* Name of the hook
18+
*
19+
* @var string
1820
*/
19-
public function getName(): ?string
20-
{
21-
return 'PHP_CodeSniffer';
22-
}
21+
protected $name = 'PHP_CodeSniffer';
2322

2423
/**
2524
* Analyze and fix committed PHP files using PHP Code Sniffer and PHP Code Beautifier and Fixer.
@@ -30,7 +29,7 @@ public function getName(): ?string
3029
*/
3130
public function handle(ChangedFiles $files, Closure $next)
3231
{
33-
$this->analyzerConfigParam = $this->analyzerConfigParam();
32+
$this->configParam = $this->configParam();
3433

3534
return $this->setFileExtensions(['php'])
3635
->setAnalyzerExecutable(config('git-hooks.code_analyzers.php_code_sniffer.phpcs_path'))
@@ -43,15 +42,15 @@ public function handle(ChangedFiles $files, Closure $next)
4342
*/
4443
public function analyzerCommand(): string
4544
{
46-
return trim(sprintf('%s %s', $this->getAnalyzerExecutable(), $this->analyzerConfigParam));
45+
return trim(sprintf('%s %s', $this->getAnalyzerExecutable(), $this->configParam));
4746
}
4847

4948
/**
5049
* Returns the command to run PHPCS
5150
*/
5251
public function fixerCommand(): string
5352
{
54-
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->analyzerConfigParam));
53+
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
5554
}
5655

5756
/**
@@ -61,7 +60,7 @@ public function fixerCommand(): string
6160
*
6261
* @return string The configuration parameter for the analyzer.
6362
*/
64-
public function analyzerConfigParam(): string
63+
public function configParam(): string
6564
{
6665
$phpCSStandard = rtrim(config('git-hooks.code_analyzers.php_code_sniffer.standard'), '/');
6766

src/Console/Commands/Hooks/PintPreCommitHook.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ class PintPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAna
1111
/**
1212
* @var string
1313
*/
14-
protected $analyzerConfigParam;
14+
protected $configParam;
1515

1616
/**
17-
* Get the name of the hook.
17+
* Name of the hook
18+
*
19+
* @var string
1820
*/
19-
public function getName(): ?string
20-
{
21-
return 'Laravel Pint';
22-
}
21+
protected $name = 'Laravel Pint';
2322

2423
/**
2524
* Analyze and fix committed PHP files using Laravel Pint
@@ -30,7 +29,7 @@ public function getName(): ?string
3029
*/
3130
public function handle(ChangedFiles $files, Closure $next)
3231
{
33-
$this->analyzerConfigParam = $this->analyzerConfigParam();
32+
$this->configParam = $this->configParam();
3433

3534
return $this->setFileExtensions(['php'])
3635
->setAnalyzerExecutable(config('git-hooks.code_analyzers.laravel_pint.path'), true)
@@ -42,23 +41,23 @@ public function handle(ChangedFiles $files, Closure $next)
4241
*/
4342
public function analyzerCommand(): string
4443
{
45-
return trim(sprintf('%s --test %s', $this->getAnalyzerExecutable(), $this->analyzerConfigParam));
44+
return trim(sprintf('%s --test %s', $this->getAnalyzerExecutable(), $this->configParam));
4645
}
4746

4847
/**
4948
* Returns the command to run Pint fixer
5049
*/
5150
public function fixerCommand(): string
5251
{
53-
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->analyzerConfigParam));
52+
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
5453
}
5554

5655
/**
5756
* Gets the command-line parameter for specifying the configuration file for Laravel Pint.
5857
*
5958
* @return string The command-line parameter for the configuration file, or an empty string if not set.
6059
*/
61-
protected function analyzerConfigParam(): string
60+
protected function configParam(): string
6261
{
6362
$pintConfigFile = config('git-hooks.code_analyzers.laravel_pint.config');
6463

tests/Datasets/GitLogsDataset.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
]);
2626

2727
dataset('listOfFixableFiles', [
28-
'List of Fixable Files' => 'AM temp/ClassWithFixableIssues.php',
28+
'List of Fixable Files' => implode(PHP_EOL, [
29+
'AM temp/ClassWithFixableIssues.php',
30+
'AM temp/fixable-blade-file.blade.php',
31+
]),
2932
]);

tests/Datasets/PreCommitHooksDataset.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@
2424
],
2525
],
2626
]);
27+
28+
dataset('bladeFormatterConfiguration', [
29+
'.bladeformatterrc.json file' => [
30+
[
31+
'path' => '../../../../node_modules/.bin/blade-formatter',
32+
'config' => __DIR__.'/../Fixtures/bladeFormatterFixture.json',
33+
],
34+
],
35+
]);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Igorsgm\GitHooks\Facades\GitHooks;
4+
use Igorsgm\GitHooks\Git\ChangedFiles;
5+
use Igorsgm\GitHooks\Tests\Fixtures\ConcreteBaseCodeAnalyzerFixture;
6+
use Igorsgm\GitHooks\Traits\GitHelper;
7+
8+
uses(GitHelper::class);
9+
beforeEach(function () {
10+
$this->gitInit();
11+
$this->initializeTempDirectory(base_path('temp'));
12+
});
13+
14+
test('Skips check if there are no files added to commit', function () {
15+
$changedFiles = mock(ChangedFiles::class)
16+
->shouldReceive('getAddedToCommit')
17+
->andReturn(collect())
18+
->getMock();
19+
20+
$next = function ($files) {
21+
return 'passed';
22+
};
23+
24+
$hook = new ConcreteBaseCodeAnalyzerFixture();
25+
$result = $hook->handleCommittedFiles($changedFiles, $next);
26+
expect($result)->toBe('passed');
27+
});
28+
29+
test('Skips check during a Merge process', function ($modifiedFilesList) {
30+
$changedFiles = new ChangedFiles($modifiedFilesList);
31+
GitHooks::shouldReceive('isMergeInProgress')->andReturn(true);
32+
33+
$next = function ($files) {
34+
return 'passed';
35+
};
36+
37+
$hook = new ConcreteBaseCodeAnalyzerFixture();
38+
$result = $hook->handleCommittedFiles($changedFiles, $next);
39+
expect($result)->toBe('passed');
40+
})->with('modifiedFilesList');

0 commit comments

Comments
 (0)