3
3
namespace Igorsgm \GitHooks \Console \Commands \Hooks ;
4
4
5
5
use Closure ;
6
+ use Igorsgm \GitHooks \Contracts \CodeAnalyzerPreCommitHook ;
6
7
use Igorsgm \GitHooks \Exceptions \HookFailException ;
7
8
use Igorsgm \GitHooks \Facades \GitHooks ;
8
9
use Igorsgm \GitHooks \Git \ChangedFile ;
14
15
use Illuminate \Support \Collection ;
15
16
use Symfony \Component \Console \Terminal ;
16
17
17
- abstract class BaseCodeAnalyzerPreCommitHook
18
+ abstract class BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
18
19
{
19
20
use ProcessHelper;
20
21
use WithPipelineFailCheck;
21
22
22
23
/**
23
24
* Command instance that is bound automatically by Hooks Pipeline, so it can be used inside the Hook.
24
- *
25
- * @var Command
26
25
*/
27
- public $ command ;
26
+ public Command $ command ;
28
27
29
28
/**
30
29
* Name of the hook
31
- *
32
- * @var string
33
30
*/
34
- protected $ name ;
31
+ protected string $ name ;
35
32
36
- /*
33
+ /**
37
34
* List of files extensions that will be analyzed by the hook.
38
35
* Can also be a regular expression.
39
- * @var array|string
36
+ *
37
+ * @var array<int, string>|string
40
38
*/
41
- public $ fileExtensions = [];
39
+ public array | string $ fileExtensions = [];
42
40
43
41
/**
44
42
* The path to the analyzer executable.
45
- *
46
- * @var string
47
43
*/
48
- protected $ analyzerExecutable ;
44
+ protected string $ analyzerExecutable ;
49
45
50
46
/**
51
47
* The path to the fixer executable. In multiple cases it's the same of the analyzer executable.
52
- *
53
- * @var string
54
48
*/
55
- protected $ fixerExecutable ;
49
+ protected string $ fixerExecutable ;
56
50
57
51
/**
58
52
* The list of paths of files that are badly formatted and should be fixed.
59
53
*
60
- * @var array
54
+ * @var array<int, string>
61
55
*/
62
- protected $ filesBadlyFormattedPaths = [];
56
+ protected array $ filesBadlyFormattedPaths = [];
63
57
64
58
/**
65
59
* Run tool in docker
66
- *
67
- * @var bool
68
60
*/
69
- protected $ runInDocker = false ;
61
+ protected bool $ runInDocker = false ;
70
62
71
63
/**
72
64
* Docker container on which to run
73
- *
74
- * @var string
75
65
*/
76
- protected $ dockerContainer = '' ;
66
+ protected string $ dockerContainer = '' ;
77
67
78
68
public function __construct ()
79
69
{
@@ -83,13 +73,9 @@ public function __construct()
83
73
/**
84
74
* Handles the committed files and checks if they are properly formatted.
85
75
*
86
- * @param ChangedFiles $files The instance of the changed files.
87
- * @param Closure $next The closure to be executed after the files are handled.
88
- * @return mixed|void
89
- *
90
76
* @throws HookFailException If the hook fails to analyze the committed files.
91
77
*/
92
- public function handleCommittedFiles (ChangedFiles $ files , Closure $ next )
78
+ public function handleCommittedFiles (ChangedFiles $ files , Closure $ next ): mixed
93
79
{
94
80
$ commitFiles = $ files ->getStaged ();
95
81
@@ -115,10 +101,10 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next)
115
101
* whether it is properly formatted according to the configured analyzer, and collects
116
102
* paths of any files that are not properly formatted.
117
103
*
118
- * @param ChangedFile[]| Collection $commitFiles The files to analyze.
104
+ * @param Collection<int, ChangedFile> $commitFiles The files to analyze.
119
105
* @return $this
120
106
*/
121
- protected function analizeCommittedFiles ($ commitFiles )
107
+ protected function analizeCommittedFiles (Collection $ commitFiles ): self
122
108
{
123
109
foreach ($ commitFiles as $ file ) {
124
110
if (! $ this ->canFileBeAnalyzed ($ file )) {
@@ -175,10 +161,8 @@ protected function canFileBeAnalyzed(ChangedFile $file): bool
175
161
176
162
/**
177
163
* Returns the message to display when the commit fails.
178
- *
179
- * @return $this
180
164
*/
181
- protected function commitFailMessage ()
165
+ protected function commitFailMessage (): self
182
166
{
183
167
$ this ->command ->newLine ();
184
168
@@ -196,11 +180,9 @@ protected function commitFailMessage()
196
180
/**
197
181
* Check if the BaseCodeAnalyzerPreCommitHook is installed.
198
182
*
199
- * @return $this
200
- *
201
183
* @throws HookFailException
202
184
*/
203
- protected function validateAnalyzerInstallation ()
185
+ protected function validateAnalyzerInstallation (): self
204
186
{
205
187
if (file_exists ($ this ->analyzerExecutable )) {
206
188
return $ this ;
@@ -219,12 +201,9 @@ protected function validateAnalyzerInstallation()
219
201
/**
220
202
* Validates the given configuration path.
221
203
*
222
- * @param string $path The path to the configuration file.
223
- * @return $this This instance for method chaining.
224
- *
225
204
* @throws HookFailException If the configuration file does not exist.
226
205
*/
227
- protected function validateConfigPath ($ path )
206
+ protected function validateConfigPath (string $ path ): self
228
207
{
229
208
if (file_exists ($ path )) {
230
209
return $ this ;
@@ -282,7 +261,6 @@ protected function suggestAutoFixOrExit(): bool
282
261
* configured fixer command. For each fixed file, adds it to Git and removes its path
283
262
* from the `$filesBadlyFormattedPaths` array.
284
263
*
285
- *
286
264
* @throws HookFailException if any files cannot be fixed.
287
265
*/
288
266
private function autoFixFiles (): bool
@@ -344,6 +322,11 @@ public function getOutput(): ?OutputStyle
344
322
return $ this ->command ->getOutput ();
345
323
}
346
324
325
+ public function setCommand (Command $ command ): void
326
+ {
327
+ $ this ->command = $ command ;
328
+ }
329
+
347
330
/**
348
331
* Get the name of the hook.
349
332
*/
@@ -353,20 +336,16 @@ public function getName(): ?string
353
336
}
354
337
355
338
/**
356
- * @param array|string $fileExtensions
357
- * @return BaseCodeAnalyzerPreCommitHook
339
+ * @param array<int, string>|string $fileExtensions
358
340
*/
359
- public function setFileExtensions ($ fileExtensions )
341
+ public function setFileExtensions (array | string $ fileExtensions ): self
360
342
{
361
343
$ this ->fileExtensions = $ fileExtensions ;
362
344
363
345
return $ this ;
364
346
}
365
347
366
- /**
367
- * @return BaseCodeAnalyzerPreCommitHook
368
- */
369
- public function setAnalyzerExecutable ($ executablePath , $ isSameAsFixer = false )
348
+ public function setAnalyzerExecutable (string $ executablePath , bool $ isSameAsFixer = false ): self
370
349
{
371
350
$ this ->analyzerExecutable = './ ' .trim ($ executablePath , '/ ' );
372
351
@@ -378,12 +357,9 @@ public function getAnalyzerExecutable(): string
378
357
return $ this ->analyzerExecutable ;
379
358
}
380
359
381
- /**
382
- * @return BaseCodeAnalyzerPreCommitHook
383
- */
384
- public function setFixerExecutable ($ exacutablePath )
360
+ public function setFixerExecutable (string $ executablePath ): self
385
361
{
386
- $ this ->fixerExecutable = './ ' .trim ($ exacutablePath , '/ ' );
362
+ $ this ->fixerExecutable = './ ' .trim ($ executablePath , '/ ' );
387
363
388
364
return $ this ;
389
365
}
@@ -393,10 +369,7 @@ public function getFixerExecutable(): string
393
369
return $ this ->fixerExecutable ;
394
370
}
395
371
396
- /**
397
- * @return BaseCodeAnalyzerPreCommitHook
398
- */
399
- public function setRunInDocker ($ runInDocker )
372
+ public function setRunInDocker (bool $ runInDocker ): self
400
373
{
401
374
$ this ->runInDocker = (bool ) $ runInDocker ;
402
375
@@ -408,11 +381,7 @@ public function getRunInDocker(): bool
408
381
return $ this ->runInDocker ;
409
382
}
410
383
411
- /**
412
- * @param string $dockerContainer
413
- * @return BaseCodeAnalyzerPreCommitHook
414
- */
415
- public function setDockerContainer ($ dockerContainer )
384
+ public function setDockerContainer (string $ dockerContainer ): self
416
385
{
417
386
$ this ->dockerContainer = $ dockerContainer ;
418
387
0 commit comments