Skip to content

Commit 93a3d86

Browse files
committed
wip LarastanPreCommitHook.php
1 parent 46f8221 commit 93a3d86

File tree

4 files changed

+98
-5
lines changed

4 files changed

+98
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"require-dev": {
3333
"laravel/pint": "^1.2",
3434
"mockery/mockery": "^1.5.1",
35+
"nunomaduro/larastan": "^2.0",
3536
"orchestra/testbench": "^7.0",
3637
"pestphp/pest": "^1.22",
3738
"pestphp/pest-plugin-mock": "^1.0",

config/git-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@
178178
'phpcbf_path' => env('PHPCBF_PATH', 'vendor/bin/phpcbf'),
179179
'standard' => env('PHPCS_STANDARD', 'phpcs.xml'),
180180
],
181+
'larastan' => [
182+
'path' => env('LARASTAN_PATH', 'vendor/bin/phpstan'),
183+
'config' => env('LARASTAN_CONFIG', 'phpstan.neon'),
184+
'additional_params' => env('LARASTAN_ADDITIONAL_PARAMS', '--xdebug'),
185+
],
181186
'blade_formatter' => [
182187
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
183188
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ abstract class BaseCodeAnalyzerPreCommitHook
2424

2525
/**
2626
* Name of the hook
27+
*
2728
* @var string
2829
*/
2930
protected $name;
@@ -99,10 +100,7 @@ protected function analizeCommittedFiles($commitFiles)
99100
{
100101
/** @var ChangedFile $file */
101102
foreach ($commitFiles as $file) {
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-
) {
103+
if (! $this->canFileBeAnalyzed($file)) {
106104
continue;
107105
}
108106

@@ -126,6 +124,19 @@ protected function analizeCommittedFiles($commitFiles)
126124
return $this;
127125
}
128126

127+
/**
128+
* Checks whether the given ChangedFile can be analyzed based on its file extension and the list of allowed extensions.
129+
*/
130+
protected function canFileBeAnalyzed(ChangedFile $file): bool
131+
{
132+
if (empty($this->fileExtensions) || $this->fileExtensions === 'all') {
133+
return true;
134+
}
135+
136+
return (is_array($this->fileExtensions) && in_array($file->extension(), $this->fileExtensions)) ||
137+
(is_string($this->fileExtensions) && preg_match($this->fileExtensions, $file->getFilePath()));
138+
}
139+
129140
/**
130141
* Returns the message to display when the commit fails.
131142
*
@@ -175,7 +186,7 @@ protected function checkAnalyzerInstallation()
175186
*/
176187
protected function suggestAutoFixOrExit()
177188
{
178-
if (Terminal::hasSttyAvailable() &&
189+
if (Terminal::hasSttyAvailable() && ! empty($this->fixerExecutable) &&
179190
$this->command->confirm('Would you like to attempt to correct files automagically?')
180191
) {
181192
$errorFilesString = implode(' ', $this->filesBadlyFormattedPaths);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 LarastanPreCommitHook 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 = 'Larastan';
22+
23+
/**
24+
* Analyzes committed files using Larastan
25+
*
26+
* @param ChangedFiles $files The list of committed files to analyze.
27+
* @param Closure $next The next hook in the chain to execute.
28+
* @return mixed|null
29+
*/
30+
public function handle(ChangedFiles $files, Closure $next)
31+
{
32+
$this->configParam = $this->configParam();
33+
34+
return $this->setAnalyzerExecutable(config('git-hooks.code_analyzers.larastan.path'))
35+
->handleCommittedFiles($files, $next);
36+
}
37+
38+
/**
39+
* Returns the command to run Larastan analyzer with the given configuration file.
40+
* By default, it turns off XDebug if it’s enabled to achieve better performance.
41+
*/
42+
public function analyzerCommand(): string
43+
{
44+
$additionalParams = config('git-hooks.code_analyzers.larastan.additional_params');
45+
46+
if (! empty($additionalParams)) {
47+
// Removing configuration/c/xdebug parameters from additional parameters to avoid conflicts
48+
// because they are already set in the command by default.
49+
$additionalParams = preg_replace('/\s*--(configuration|c|xdebug)\b(=\S*)?\s*/', '', $additionalParams);
50+
}
51+
52+
return trim(
53+
sprintf('%s analyse %s --xdebug %s', $this->getAnalyzerExecutable(), $this->configParam, $additionalParams)
54+
);
55+
}
56+
57+
/**
58+
* Empty fixer command because Larastan doesn't provide any type of auto-fixing.
59+
*/
60+
public function fixerCommand(): string
61+
{
62+
return '';
63+
}
64+
65+
/**
66+
* Gets the command-line parameter for specifying the configuration file for Larastan.
67+
*
68+
* @return string The command-line parameter for the configuration file, or an empty string if not set.
69+
*/
70+
protected function configParam(): string
71+
{
72+
$phpStanConfigFile = rtrim(config('git-hooks.code_analyzers.larastan.config'), '/');
73+
74+
return empty($phpStanConfigFile) ? '' : '--configuration='.$phpStanConfigFile;
75+
}
76+
}

0 commit comments

Comments
 (0)