Skip to content

Commit da5ec6c

Browse files
committed
refactor(Commands): change variables to class attributes
1 parent b6a99c9 commit da5ec6c

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

qa/phpstan.neon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Phpqa copia este fichero de configuracion y lo ejecuta desde el directorio "build"
2-
# currentWorkingDirectory es donde se ejecuta phpstan, en este caso desde ./
31
{
42
parameters: {
53
level: 8
@@ -10,7 +8,7 @@
108
checkMissingIterableValueType: false
119
ignoreErrors: [
1210
'#Method [a-zA-Z0-9\\_]+Command::handle\(\)#',
13-
'#root of method GitHooks\Commands\HookCommand::[a-zA-Z0-9\\_]+() expects string, string|false given#'
11+
'#Property [a-zA-Z0-9\\_]+Command::\$root \(string\) does not accept string\|false.#'
1412
]
1513
excludes_analyse: [
1614
# Exclude all files relatives to Artisan Console

src/Commands/CleanHookCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class CleanHookCommand extends Command
1010
{
1111
protected $signature = 'hook:clean {hook=pre-commit}';
1212
protected $description = 'Deletes the hook passed as argument (default pre-commit)';
13+
14+
/**
15+
* Extra information about the command invoked with the --help flag.
16+
*
17+
* @var string
18+
*/
1319
protected $help = 'Without arguments deletes the pre-commit hook. A optional argument can be the name of another hook. Example: hook:clean pre-push.';
1420

1521
/**

src/Commands/CreateHookCommand.php

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class CreateHookCommand extends Command
1111
{
1212
protected $signature = 'hook {hook=pre-commit : The hook to be setted} {scriptFile? : The custom script to be setted as the hook (default:GitHooks)}';
1313
protected $description = 'Copies the hook for the GitHooks execution. The default hook is pre-commit. You can pass scriptFile as argument to set custom scripts.';
14+
15+
/**
16+
* Extra information about the command invoked with the --help flag.
17+
*
18+
* @var string
19+
*/
1420
protected $help = 'The default script is the default GitHooks execution. You can custom your script to execute what ever you want.
1521
Even the default script and after, other tools which GitHooks not support or vice versa';
1622

@@ -19,6 +25,27 @@ class CreateHookCommand extends Command
1925
*/
2026
protected $printer;
2127

28+
/**
29+
* Path to the root project
30+
*
31+
* @var string
32+
*/
33+
protected $root;
34+
35+
/**
36+
* First argument. The hook that will be setted.
37+
*
38+
* @var string
39+
*/
40+
protected $hook;
41+
42+
/**
43+
* Second argument. The custom script to be setted as the hook. Per default is the GitHooks script to execute the tools setted in githooks.yml file.
44+
*
45+
* @var string
46+
*/
47+
protected $scriptFile;
48+
2249
public function __construct(Printer $printer)
2350
{
2451
$this->printer = $printer;
@@ -27,46 +54,42 @@ public function __construct(Printer $printer)
2754

2855
public function handle()
2956
{
30-
$root = getcwd();
31-
$hook = strval($this->argument('hook'));
32-
$scriptFile = $this->argument('scriptFile') ?? '';
33-
57+
$this->root = getcwd();
58+
$this->hook = strval($this->argument('hook'));
59+
$this->scriptFile = strval($this->argument('scriptFile')) ?? '';
3460

35-
if (!Hooks::validate($hook)) {
36-
$this->printer->error("'$hook' is not a valid git hook. Avaliable hooks are:");
61+
if (!Hooks::validate($this->hook)) {
62+
$this->printer->error("'{$this->hook}' is not a valid git hook. Avaliable hooks are:");
3763
$this->printer->error(implode(', ', Hooks::HOOKS));
3864
return;
3965
}
4066

41-
$origin = $this->path2OriginFile($root, strval($scriptFile));
67+
$origin = $this->path2OriginFile();
4268
try {
43-
$destiny = "$root/.git/hooks/$hook";
69+
$destiny = "{$this->root}/.git/hooks/{$this->hook}";
4470
copy($origin, $destiny);
4571
chmod($destiny, 0755);
46-
$this->printer->success("Hook $hook created");
72+
$this->printer->success("Hook {$this->hook} created");
4773
} catch (\Throwable $th) {
48-
$this->printer->error("Error copying $origin in $hook");
74+
$this->printer->error("Error copying $origin in {$this->hook}");
4975
}
5076
}
5177

5278
/**
5379
* Find the origin of the scripts for the hook
5480
*
55-
* @param string $root Path to the root of the project.
56-
* @param string $scriptFile File with the custom script. It's optional.
57-
*
5881
* @return string File to be executed in the hook.
5982
*/
60-
public function path2OriginFile(string $root, string $scriptFile): string
83+
public function path2OriginFile(): string
6184
{
6285
$origin = '';
63-
if (empty($scriptFile)) {
64-
$origin = $this->defaultPrecommit($root);
86+
if (empty($this->scriptFile)) {
87+
$origin = $this->defaultPrecommit();
6588
} else {
66-
if (!file_exists($scriptFile)) {
67-
throw new Exception("$scriptFile file not found");
89+
if (!file_exists($this->scriptFile)) {
90+
throw new Exception("{$this->scriptFile} file not found");
6891
}
69-
$origin = $scriptFile;
92+
$origin = $this->scriptFile;
7093
}
7194
return $origin;
7295
}
@@ -76,19 +99,17 @@ public function path2OriginFile(string $root, string $scriptFile): string
7699
* 1. When GitHooks will be a library, it returns the path through 'vendor'.
77100
* 2. To work on developing GitHooks itself, it will return the local path to 'hooks'
78101
*
79-
* @param string $root Root path of the project.
80-
*
81102
* @return string The default script to run GitHooks in the hook.
82103
*/
83-
public function defaultPrecommit(string $root): string
104+
public function defaultPrecommit(): string
84105
{
85106
$origin = '';
86-
if (file_exists($root . '/vendor/wtyd/githooks/hooks/pre-commit.php')) {
87-
$origin = $root . '/vendor/wtyd/githooks/hooks/pre-commit.php';
107+
if (file_exists($this->root . '/vendor/wtyd/githooks/hooks/pre-commit.php')) {
108+
$origin = $this->root . '/vendor/wtyd/githooks/hooks/pre-commit.php';
88109
}
89110

90-
if (file_exists($root . '/hooks/pre-commit.php')) {
91-
$origin = $root . '/hooks/pre-commit.php';
111+
if (file_exists($this->root . '/hooks/pre-commit.php')) {
112+
$origin = $this->root . '/hooks/pre-commit.php';
92113
}
93114

94115
if (empty($origin)) {

0 commit comments

Comments
 (0)