Skip to content

Commit 8d4f057

Browse files
authored
Merge pull request #1 from indy2kro/v2-upgrade
Refactoring for strong typing enforce, added phpstan and rector
2 parents 4348e40 + 125e7f6 commit 8d4f057

Some content is hidden

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

55 files changed

+364
-397
lines changed

.github/workflows/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ jobs:
108108
- name: Execute Code Sniffer via Laravel Pint
109109
run: vendor/bin/pint --test src config
110110

111+
- name: Execute PHP Stan
112+
run: vendor/bin/phpstan
113+
114+
- name: Execute Rector
115+
run: vendor/bin/rector
116+
111117
laravel10-tests:
112118
runs-on: ubuntu-latest
113119

@@ -202,6 +208,12 @@ jobs:
202208
- name: Execute Code Sniffer via Laravel Pint
203209
run: vendor/bin/pint --test src config
204210

211+
- name: Execute PHP Stan
212+
run: vendor/bin/phpstan
213+
214+
- name: Execute Rector
215+
run: vendor/bin/rector
216+
205217
laravel11-tests:
206218
runs-on: ubuntu-latest
207219

@@ -296,3 +308,9 @@ jobs:
296308
- name: Execute Code Sniffer via Laravel Pint
297309
run: vendor/bin/pint --test src config
298310

311+
- name: Execute PHP Stan
312+
run: vendor/bin/phpstan
313+
314+
- name: Execute Rector
315+
run: vendor/bin/rector
316+

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"orchestra/testbench": "^v8.0.0|^v9.0.0",
3838
"pestphp/pest": "^2.0",
3939
"pestphp/pest-plugin-laravel": "^2.3",
40+
"rector/rector": "^1.0",
4041
"squizlabs/php_codesniffer": "^3.7"
4142
},
4243
"autoload": {

config/git-hooks.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
| during execution directly for easy debug.
246246
|
247247
*/
248-
'output_errors' => false,
248+
'output_errors' => env('GITHOOKS_OUTPUT_ERRORS', false),
249249

250250
/*
251251
|--------------------------------------------------------------------------
@@ -256,7 +256,7 @@
256256
| automatically run the fixer without any CLI prompt.
257257
|
258258
*/
259-
'automatically_fix_errors' => false,
259+
'automatically_fix_errors' => env('GITHOOKS_AUTOMATICALLY_FIX_ERRORS', false),
260260

261261
/*
262262
|--------------------------------------------------------------------------
@@ -268,7 +268,7 @@
268268
| The git hooks will not fail in case the re-run is succesful.
269269
|
270270
*/
271-
'rerun_analyzer_after_autofix' => false,
271+
'rerun_analyzer_after_autofix' => env('GITHOOKS_RERUN_ANALYZER_AFTER_AUTOFIX', false),
272272

273273
/*
274274
|--------------------------------------------------------------------------
@@ -279,7 +279,7 @@
279279
| stop (or not) at the first analyzer failure encountered.
280280
|
281281
*/
282-
'stop_at_first_analyzer_failure' => true,
282+
'stop_at_first_analyzer_failure' => env('GITHOOKS_STOP_AT_FIRST_ANALYZER_FAILURE', true),
283283

284284
/*
285285
|--------------------------------------------------------------------------
@@ -290,7 +290,7 @@
290290
| display the commands that are executed (usually for debug purpose).
291291
|
292292
*/
293-
'debug_commands' => false,
293+
'debug_commands' => env('GITHOOKS_DEBUG_COMMANDS', false),
294294

295295
/*
296296
|--------------------------------------------------------------------------
@@ -301,5 +301,5 @@
301301
| command during execution directly for easy debug.
302302
|
303303
*/
304-
'debug_output' => false,
304+
'debug_output' => env('GITHOOKS_DEBUG_OUTPUT', false),
305305
];

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
paths:
3+
- ./src
4+
# The level 9 is the highest level (with check for mixed type)
5+
level: 6

rector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__.'/src',
11+
__DIR__.'/tests',
12+
])
13+
// uncomment to reach your current PHP version
14+
->withPhpSets()
15+
->withRules([
16+
AddVoidReturnTypeWhereNoReturnRector::class,
17+
]);

src/Console/Commands/Hooks/BaseCodeAnalyzerPreCommitHook.php

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
44

55
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
67
use Igorsgm\GitHooks\Exceptions\HookFailException;
78
use Igorsgm\GitHooks\Facades\GitHooks;
89
use Igorsgm\GitHooks\Git\ChangedFile;
@@ -14,66 +15,55 @@
1415
use Illuminate\Support\Collection;
1516
use 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

src/Console/Commands/Hooks/BladeFormatterPreCommitHook.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,9 @@
88

99
class BladeFormatterPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
1010
{
11-
/**
12-
* @var string
13-
*/
14-
protected $configParam;
11+
protected string $configParam;
1512

16-
/**
17-
* Name of the hook
18-
*
19-
* @var string
20-
*/
21-
protected $name = 'Blade Formatter';
13+
protected string $name = 'Blade Formatter';
2214

2315
/**
2416
* Analyze and fix committed blade.php files using blade-formatter npm package
@@ -63,7 +55,7 @@ public function fixerCommand(): string
6355
*/
6456
public function configParam(): string
6557
{
66-
$bladeFormatterConfig = rtrim(config('git-hooks.code_analyzers.blade_formatter.config'), '/');
58+
$bladeFormatterConfig = rtrim((string) config('git-hooks.code_analyzers.blade_formatter.config'), '/');
6759
$this->validateConfigPath($bladeFormatterConfig);
6860

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

0 commit comments

Comments
 (0)