Skip to content

Commit d0fbf47

Browse files
committed
feat: enhance pre-commit hooks with strict types and additional methods for command handling
1 parent 4d5d7ef commit d0fbf47

File tree

70 files changed

+342
-190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+342
-190
lines changed

pint.json

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,62 @@
11
{
22
"preset": "laravel",
3+
"notPath": [
4+
"tests/TestCase.php"
5+
],
36
"notName": [
47
"*WithFixableIssues.php"
5-
]
8+
],
9+
"rules": {
10+
"array_push": true,
11+
"backtick_to_shell_exec": true,
12+
"date_time_immutable": true,
13+
"declare_strict_types": true,
14+
"lowercase_keywords": true,
15+
"lowercase_static_reference": true,
16+
"fully_qualified_strict_types": true,
17+
"global_namespace_import": {
18+
"import_classes": true,
19+
"import_constants": true,
20+
"import_functions": true
21+
},
22+
"mb_str_functions": true,
23+
"modernize_types_casting": true,
24+
"new_with_parentheses": false,
25+
"no_superfluous_elseif": true,
26+
"no_useless_else": true,
27+
"no_multiple_statements_per_line": true,
28+
"not_operator_with_successor_space": false,
29+
"ordered_class_elements": {
30+
"order": [
31+
"use_trait",
32+
"case",
33+
"constant",
34+
"constant_public",
35+
"constant_protected",
36+
"constant_private",
37+
"property_public",
38+
"property_protected",
39+
"property_private",
40+
"construct",
41+
"destruct",
42+
"magic",
43+
"phpunit",
44+
"method_abstract",
45+
"method_public_static",
46+
"method_public",
47+
"method_protected_static",
48+
"method_protected",
49+
"method_private_static",
50+
"method_private"
51+
],
52+
"sort_algorithm": "none"
53+
},
54+
"ordered_interfaces": true,
55+
"ordered_traits": true,
56+
"protected_to_private": true,
57+
"self_accessor": true,
58+
"self_static_accessor": true,
59+
"strict_comparison": true,
60+
"visibility_required": true
61+
}
662
}

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ public function __construct()
7474
$this->chunkSize = config('git-hooks.analyzer_chunk_size');
7575
}
7676

77+
/**
78+
* Get the analyzer command to be executed.
79+
*/
80+
abstract public function analyzerCommand(): string;
81+
82+
/**
83+
* Get the fixer command to be executed.
84+
*/
85+
abstract public function fixerCommand(): string;
86+
7787
/**
7888
* Handles the committed files and checks if they are properly formatted.
7989
*
@@ -105,7 +115,7 @@ public function handleCommittedFiles(ChangedFiles $files, Closure $next): mixed
105115
*/
106116
public function getOutput(): ?OutputStyle
107117
{
108-
if (! config('git-hooks.debug_output')) {
118+
if (!config('git-hooks.debug_output')) {
109119
return null;
110120
}
111121

@@ -160,14 +170,14 @@ public function getFixerExecutable(): string
160170
}
161171

162172
/**
163-
* Get the analyzer command to be executed.
164-
*/
165-
abstract public function analyzerCommand(): string;
166-
167-
/**
168-
* Get the fixer command to be executed.
173+
* Get the file extensions that can be analyzed.
174+
*
175+
* @return array<int, string>|string
169176
*/
170-
abstract public function fixerCommand(): string;
177+
public function getFileExtensions(): array|string
178+
{
179+
return $this->fileExtensions;
180+
}
171181

172182
/**
173183
* Analyzes an array of ChangedFile objects and checks whether each file can be analyzed,
@@ -185,7 +195,7 @@ protected function analizeCommittedFiles(Collection $commitFiles): self
185195

186196
/** @var ChangedFile $file */
187197
foreach ($chunk as $file) {
188-
if (! $this->canFileBeAnalyzed($file)) {
198+
if (!$this->canFileBeAnalyzed($file)) {
189199
continue;
190200
}
191201

@@ -212,7 +222,7 @@ protected function analizeCommittedFiles(Collection $commitFiles): self
212222

213223
$isProperlyFormatted = $process->isSuccessful();
214224

215-
if (! $isProperlyFormatted) {
225+
if (!$isProperlyFormatted) {
216226
if (empty($this->filesBadlyFormattedPaths)) {
217227
$this->command->newLine();
218228
}
@@ -222,7 +232,7 @@ protected function analizeCommittedFiles(Collection $commitFiles): self
222232
);
223233
$this->filesBadlyFormattedPaths[] = $filePath;
224234

225-
if (config('git-hooks.output_errors') && ! config('git-hooks.debug_output')) {
235+
if (config('git-hooks.output_errors') && !config('git-hooks.debug_output')) {
226236
$this->command->newLine();
227237
$this->command->getOutput()->write($process->getOutput());
228238
}
@@ -274,7 +284,7 @@ protected function commitFailMessage(): self
274284
*/
275285
protected function validateAnalyzerInstallation(): self
276286
{
277-
if (! config('git-hooks.validate_paths') || file_exists($this->analyzerExecutable)) {
287+
if (!config('git-hooks.validate_paths') || file_exists($this->analyzerExecutable)) {
278288
return $this;
279289
}
280290

@@ -297,7 +307,7 @@ protected function validateAnalyzerInstallation(): self
297307
*/
298308
protected function validateConfigPath(string $path): self
299309
{
300-
if (! config('git-hooks.validate_paths') || file_exists($path)) {
310+
if (!config('git-hooks.validate_paths') || file_exists($path)) {
301311
return $this;
302312
}
303313

@@ -312,14 +322,4 @@ protected function validateConfigPath(string $path): self
312322

313323
throw new HookFailException;
314324
}
315-
316-
/**
317-
* Get the file extensions that can be analyzed.
318-
*
319-
* @return array<int, string>|string
320-
*/
321-
public function getFileExtensions(): array|string
322-
{
323-
return $this->fileExtensions;
324-
}
325325
}

src/Console/Commands/Hooks/BladeFormatterPreCommitHook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public function handle(ChangedFiles $files, Closure $next): mixed
3636
*/
3737
public function analyzerCommand(): string
3838
{
39-
return trim(sprintf('%s -c %s', $this->getAnalyzerExecutable(), $this->configParam));
39+
return mb_trim(sprintf('%s -c %s', $this->getAnalyzerExecutable(), $this->configParam));
4040
}
4141

4242
/**
4343
* Returns the command to run Blade Formatter fixer
4444
*/
4545
public function fixerCommand(): string
4646
{
47-
return trim(sprintf('%s --write %s', $this->getFixerExecutable(), $this->configParam));
47+
return mb_trim(sprintf('%s --write %s', $this->getFixerExecutable(), $this->configParam));
4848
}
4949

5050
/**
@@ -56,7 +56,7 @@ public function fixerCommand(): string
5656
*/
5757
public function configParam(): string
5858
{
59-
$bladeFormatterConfig = rtrim((string) config('git-hooks.code_analyzers.blade_formatter.config'), '/');
59+
$bladeFormatterConfig = mb_rtrim((string) config('git-hooks.code_analyzers.blade_formatter.config'), '/');
6060
$this->validateConfigPath($bladeFormatterConfig);
6161

6262
return empty($bladeFormatterConfig) ? '' : '--config='.$bladeFormatterConfig;

src/Console/Commands/Hooks/ESLintPreCommitHook.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function handle(ChangedFiles $files, Closure $next): mixed
3939
*/
4040
public function analyzerCommand(): string
4141
{
42-
return trim(implode(' ', [
42+
return mb_trim(implode(' ', [
4343
$this->getAnalyzerExecutable(),
4444
$this->configParam,
4545
$this->additionalParams(),
@@ -51,7 +51,7 @@ public function analyzerCommand(): string
5151
*/
5252
public function fixerCommand(): string
5353
{
54-
return trim(
54+
return mb_trim(
5555
sprintf('%s --fix %s %s', $this->getFixerExecutable(), $this->configParam, $this->additionalParams())
5656
);
5757
}
@@ -63,7 +63,7 @@ public function fixerCommand(): string
6363
*/
6464
protected function configParam(): string
6565
{
66-
$eslintConfig = rtrim((string) config('git-hooks.code_analyzers.eslint.config'), '/');
66+
$eslintConfig = mb_rtrim((string) config('git-hooks.code_analyzers.eslint.config'), '/');
6767
$this->validateConfigPath($eslintConfig);
6868

6969
return empty($eslintConfig) ? '' : '--config='.$eslintConfig;
@@ -77,7 +77,7 @@ protected function additionalParams(): ?string
7777
{
7878
$additionalParams = (string) config('git-hooks.code_analyzers.eslint.additional_params');
7979

80-
if (! empty($additionalParams)) {
80+
if (!empty($additionalParams)) {
8181
$additionalParams = (string) preg_replace('/\s+\.(?:(\s)|$)/', '$1', (string) $additionalParams);
8282
$additionalParams = (string) preg_replace('/\s*--(config|c)\b(=\S*)?\s*/', '', (string) $additionalParams);
8383
}

src/Console/Commands/Hooks/LarastanPreCommitHook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public function analyzerCommand(): string
4242
{
4343
$additionalParams = (string) config('git-hooks.code_analyzers.larastan.additional_params');
4444

45-
if (! empty($additionalParams)) {
45+
if (!empty($additionalParams)) {
4646
// Removing configuration/c/xdebug parameters from additional parameters to avoid conflicts
4747
// because they are already set in the command by default.
4848
$additionalParams = (string) preg_replace('/\s*--(configuration|c|xdebug)\b(=\S*)?\s*/', '', (string) $additionalParams);
4949
}
5050

51-
return trim(
51+
return mb_trim(
5252
sprintf('%s analyse %s --xdebug %s', $this->getAnalyzerExecutable(), $this->configParam, $additionalParams)
5353
);
5454
}
@@ -68,7 +68,7 @@ public function fixerCommand(): string
6868
*/
6969
protected function configParam(): string
7070
{
71-
$phpStanConfigFile = rtrim((string) config('git-hooks.code_analyzers.larastan.config'), '/');
71+
$phpStanConfigFile = mb_rtrim((string) config('git-hooks.code_analyzers.larastan.config'), '/');
7272
$this->validateConfigPath($phpStanConfigFile);
7373

7474
return empty($phpStanConfigFile) ? '' : '--configuration='.$phpStanConfigFile;

src/Console/Commands/Hooks/PHPCSFixerPreCommitHook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public function handle(ChangedFiles $files, Closure $next): mixed
3939
*/
4040
public function analyzerCommand(): string
4141
{
42-
return trim(sprintf('%s check %s', $this->getAnalyzerExecutable(), $this->configParam));
42+
return mb_trim(sprintf('%s check %s', $this->getAnalyzerExecutable(), $this->configParam));
4343
}
4444

4545
/**
4646
* Returns the command to run PHPCS
4747
*/
4848
public function fixerCommand(): string
4949
{
50-
return trim(sprintf('%s fix %s', $this->getAnalyzerExecutable(), $this->configParam));
50+
return mb_trim(sprintf('%s fix %s', $this->getAnalyzerExecutable(), $this->configParam));
5151
}
5252

5353
/**
@@ -59,7 +59,7 @@ public function configParam(): string
5959
{
6060
$configFile = (string) config('git-hooks.code_analyzers.php_cs_fixer.config');
6161

62-
if (! empty($configFile)) {
62+
if (!empty($configFile)) {
6363
$this->validateConfigPath($configFile);
6464

6565
return '--config='.$configFile;

src/Console/Commands/Hooks/PHPCodeSnifferPreCommitHook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function handle(ChangedFiles $files, Closure $next): mixed
4040
*/
4141
public function analyzerCommand(): string
4242
{
43-
return trim(sprintf('%s %s', $this->getAnalyzerExecutable(), $this->configParam));
43+
return mb_trim(sprintf('%s %s', $this->getAnalyzerExecutable(), $this->configParam));
4444
}
4545

4646
/**
4747
* Returns the command to run PHPCS
4848
*/
4949
public function fixerCommand(): string
5050
{
51-
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
51+
return mb_trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
5252
}
5353

5454
/**
@@ -60,7 +60,7 @@ public function fixerCommand(): string
6060
*/
6161
public function configParam(): string
6262
{
63-
$phpCSStandard = rtrim((string) config('git-hooks.code_analyzers.php_code_sniffer.config'), '/');
63+
$phpCSStandard = mb_rtrim((string) config('git-hooks.code_analyzers.php_code_sniffer.config'), '/');
6464
$this->validateConfigPath($phpCSStandard);
6565

6666
return empty($phpCSStandard) ? '' : '--standard='.$phpCSStandard;

src/Console/Commands/Hooks/PhpInsightsPreCommitHook.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public function handle(ChangedFiles $files, Closure $next): mixed
3939
*/
4040
public function analyzerCommand(): string
4141
{
42-
return trim(sprintf('%s analyse %s --no-interaction %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams()));
42+
return mb_trim(sprintf('%s analyse %s --no-interaction %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams()));
4343
}
4444

4545
/**
4646
* Fixer command
4747
*/
4848
public function fixerCommand(): string
4949
{
50-
return trim(sprintf('%s analyse %s --no-interaction --fix %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams()));
50+
return mb_trim(sprintf('%s analyse %s --no-interaction --fix %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams()));
5151
}
5252

5353
/**
@@ -59,7 +59,7 @@ protected function configParam(): string
5959
{
6060
$phpInsightsConfigFile = (string) config('git-hooks.code_analyzers.phpinsights.config');
6161

62-
if (! empty($phpInsightsConfigFile)) {
62+
if (!empty($phpInsightsConfigFile)) {
6363
$this->validateConfigPath($phpInsightsConfigFile);
6464

6565
return '--config-path='.$phpInsightsConfigFile;

src/Console/Commands/Hooks/PintPreCommitHook.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public function handle(ChangedFiles $files, Closure $next): mixed
3939
*/
4040
public function analyzerCommand(): string
4141
{
42-
return trim(sprintf('%s --test %s', $this->getAnalyzerExecutable(), $this->configParam));
42+
return mb_trim(sprintf('%s --test %s', $this->getAnalyzerExecutable(), $this->configParam));
4343
}
4444

4545
/**
4646
* Returns the command to run Pint fixer
4747
*/
4848
public function fixerCommand(): string
4949
{
50-
return trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
50+
return mb_trim(sprintf('%s %s', $this->getFixerExecutable(), $this->configParam));
5151
}
5252

5353
/**
@@ -59,14 +59,14 @@ protected function configParam(): string
5959
{
6060
$pintConfigFile = config('git-hooks.code_analyzers.laravel_pint.config');
6161

62-
if (! empty($pintConfigFile)) {
62+
if (!empty($pintConfigFile)) {
6363
$this->validateConfigPath($pintConfigFile);
6464

65-
return '--config '.trim((string) $pintConfigFile, '/');
65+
return '--config '.mb_trim((string) $pintConfigFile, '/');
6666
}
6767

6868
$pintPreset = config('git-hooks.code_analyzers.laravel_pint.preset');
6969

70-
return empty($pintPreset) ? '' : '--preset '.trim((string) $pintPreset, '/');
70+
return empty($pintPreset) ? '' : '--preset '.mb_trim((string) $pintPreset, '/');
7171
}
7272
}

0 commit comments

Comments
 (0)