33namespace Igorsgm \GitHooks \Console \Commands \Hooks ;
44
55use Closure ;
6+ use Igorsgm \GitHooks \Contracts \CodeAnalyzerPreCommitHook ;
67use Igorsgm \GitHooks \Exceptions \HookFailException ;
78use Igorsgm \GitHooks \Facades \GitHooks ;
89use Igorsgm \GitHooks \Git \ChangedFile ;
1415use Illuminate \Support \Collection ;
1516use Symfony \Component \Console \Terminal ;
1617
17- abstract class BaseCodeAnalyzerPreCommitHook
18+ abstract class BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
1819{
1920 use ProcessHelper;
2021 use WithPipelineFailCheck;
2122
2223 /**
2324 * Command instance that is bound automatically by Hooks Pipeline, so it can be used inside the Hook.
24- *
25- * @var Command
2625 */
27- public $ command ;
26+ public Command $ command ;
2827
2928 /**
3029 * Name of the hook
31- *
32- * @var string
3330 */
34- protected $ name ;
31+ protected string $ name ;
3532
36- /*
33+ /**
3734 * List of files extensions that will be analyzed by the hook.
3835 * Can also be a regular expression.
39- * @var array|string
36+ *
37+ * @var array<int, string>|string
4038 */
41- public $ fileExtensions = [];
39+ public array | string $ fileExtensions = [];
4240
4341 /**
4442 * The path to the analyzer executable.
45- *
46- * @var string
4743 */
48- protected $ analyzerExecutable ;
44+ protected string $ analyzerExecutable ;
4945
5046 /**
5147 * The path to the fixer executable. In multiple cases it's the same of the analyzer executable.
52- *
53- * @var string
5448 */
55- protected $ fixerExecutable ;
49+ protected string $ fixerExecutable ;
5650
5751 /**
5852 * The list of paths of files that are badly formatted and should be fixed.
5953 *
60- * @var array
54+ * @var array<int, string>
6155 */
62- protected $ filesBadlyFormattedPaths = [];
56+ protected array $ filesBadlyFormattedPaths = [];
6357
6458 /**
6559 * Run tool in docker
66- *
67- * @var bool
6860 */
69- protected $ runInDocker = false ;
61+ protected bool $ runInDocker = false ;
7062
7163 /**
7264 * Docker container on which to run
73- *
74- * @var string
7565 */
76- protected $ dockerContainer = '' ;
66+ protected string $ dockerContainer = '' ;
7767
7868 public function __construct ()
7969 {
@@ -83,13 +73,9 @@ public function __construct()
8373 /**
8474 * Handles the committed files and checks if they are properly formatted.
8575 *
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- *
9076 * @throws HookFailException If the hook fails to analyze the committed files.
9177 */
92- public function handleCommittedFiles (ChangedFiles $ files , Closure $ next )
78+ public function handleCommittedFiles (ChangedFiles $ files , Closure $ next ): mixed
9379 {
9480 $ commitFiles = $ files ->getStaged ();
9581
@@ -115,10 +101,10 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next)
115101 * whether it is properly formatted according to the configured analyzer, and collects
116102 * paths of any files that are not properly formatted.
117103 *
118- * @param ChangedFile[]| Collection $commitFiles The files to analyze.
104+ * @param Collection<int, ChangedFile> $commitFiles The files to analyze.
119105 * @return $this
120106 */
121- protected function analizeCommittedFiles ($ commitFiles )
107+ protected function analizeCommittedFiles (Collection $ commitFiles ): self
122108 {
123109 foreach ($ commitFiles as $ file ) {
124110 if (! $ this ->canFileBeAnalyzed ($ file )) {
@@ -175,10 +161,8 @@ protected function canFileBeAnalyzed(ChangedFile $file): bool
175161
176162 /**
177163 * Returns the message to display when the commit fails.
178- *
179- * @return $this
180164 */
181- protected function commitFailMessage ()
165+ protected function commitFailMessage (): self
182166 {
183167 $ this ->command ->newLine ();
184168
@@ -196,11 +180,9 @@ protected function commitFailMessage()
196180 /**
197181 * Check if the BaseCodeAnalyzerPreCommitHook is installed.
198182 *
199- * @return $this
200- *
201183 * @throws HookFailException
202184 */
203- protected function validateAnalyzerInstallation ()
185+ protected function validateAnalyzerInstallation (): self
204186 {
205187 if (file_exists ($ this ->analyzerExecutable )) {
206188 return $ this ;
@@ -219,12 +201,9 @@ protected function validateAnalyzerInstallation()
219201 /**
220202 * Validates the given configuration path.
221203 *
222- * @param string $path The path to the configuration file.
223- * @return $this This instance for method chaining.
224- *
225204 * @throws HookFailException If the configuration file does not exist.
226205 */
227- protected function validateConfigPath ($ path )
206+ protected function validateConfigPath (string $ path ): self
228207 {
229208 if (file_exists ($ path )) {
230209 return $ this ;
@@ -282,7 +261,6 @@ protected function suggestAutoFixOrExit(): bool
282261 * configured fixer command. For each fixed file, adds it to Git and removes its path
283262 * from the `$filesBadlyFormattedPaths` array.
284263 *
285- *
286264 * @throws HookFailException if any files cannot be fixed.
287265 */
288266 private function autoFixFiles (): bool
@@ -344,6 +322,11 @@ public function getOutput(): ?OutputStyle
344322 return $ this ->command ->getOutput ();
345323 }
346324
325+ public function setCommand (Command $ command ): void
326+ {
327+ $ this ->command = $ command ;
328+ }
329+
347330 /**
348331 * Get the name of the hook.
349332 */
@@ -353,20 +336,16 @@ public function getName(): ?string
353336 }
354337
355338 /**
356- * @param array|string $fileExtensions
357- * @return BaseCodeAnalyzerPreCommitHook
339+ * @param array<int, string>|string $fileExtensions
358340 */
359- public function setFileExtensions ($ fileExtensions )
341+ public function setFileExtensions (array | string $ fileExtensions ): self
360342 {
361343 $ this ->fileExtensions = $ fileExtensions ;
362344
363345 return $ this ;
364346 }
365347
366- /**
367- * @return BaseCodeAnalyzerPreCommitHook
368- */
369- public function setAnalyzerExecutable ($ executablePath , $ isSameAsFixer = false )
348+ public function setAnalyzerExecutable (string $ executablePath , bool $ isSameAsFixer = false ): self
370349 {
371350 $ this ->analyzerExecutable = './ ' .trim ($ executablePath , '/ ' );
372351
@@ -378,12 +357,9 @@ public function getAnalyzerExecutable(): string
378357 return $ this ->analyzerExecutable ;
379358 }
380359
381- /**
382- * @return BaseCodeAnalyzerPreCommitHook
383- */
384- public function setFixerExecutable ($ exacutablePath )
360+ public function setFixerExecutable (string $ executablePath ): self
385361 {
386- $ this ->fixerExecutable = './ ' .trim ($ exacutablePath , '/ ' );
362+ $ this ->fixerExecutable = './ ' .trim ($ executablePath , '/ ' );
387363
388364 return $ this ;
389365 }
@@ -393,10 +369,7 @@ public function getFixerExecutable(): string
393369 return $ this ->fixerExecutable ;
394370 }
395371
396- /**
397- * @return BaseCodeAnalyzerPreCommitHook
398- */
399- public function setRunInDocker ($ runInDocker )
372+ public function setRunInDocker (bool $ runInDocker ): self
400373 {
401374 $ this ->runInDocker = (bool ) $ runInDocker ;
402375
@@ -408,11 +381,7 @@ public function getRunInDocker(): bool
408381 return $ this ->runInDocker ;
409382 }
410383
411- /**
412- * @param string $dockerContainer
413- * @return BaseCodeAnalyzerPreCommitHook
414- */
415- public function setDockerContainer ($ dockerContainer )
384+ public function setDockerContainer (string $ dockerContainer ): self
416385 {
417386 $ this ->dockerContainer = $ dockerContainer ;
418387
0 commit comments