Skip to content

Commit de57db5

Browse files
Refactor hook arguments logic into separate class
1 parent 6c9a60f commit de57db5

File tree

3 files changed

+114
-19
lines changed

3 files changed

+114
-19
lines changed

src/Hook/Cli/Command.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Hook\Cli;
13+
14+
use CaptainHook\App\Config;
15+
use CaptainHook\App\Console\IO;
16+
use CaptainHook\App\Hook\Action;
17+
use SebastianFeldmann\Git\Repository;
18+
19+
/**
20+
* Class Notify
21+
*
22+
* @package CaptainHook
23+
* @author Sebastian Feldmann <[email protected]>
24+
* @link https://github.com/captainhookphp/captainhook
25+
* @since Class available since Release 5.23.1
26+
*/
27+
class Command implements Action
28+
{
29+
/**
30+
* Executes the action
31+
*
32+
* @param \CaptainHook\App\Config $config
33+
* @param \CaptainHook\App\Console\IO $io
34+
* @param \SebastianFeldmann\Git\Repository $repository
35+
* @param \CaptainHook\App\Config\Action $action
36+
* @return void
37+
* @throws \Exception
38+
*/
39+
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
40+
{
41+
}
42+
}

src/Runner/Files.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CaptainHook\App\Exception;
1717
use CaptainHook\App\Hook\Util as HookUtil;
1818
use CaptainHook\App\Hooks;
19+
use CaptainHook\App\Runner\Hook\Arg;
1920
use RuntimeException;
2021
use SebastianFeldmann\Camino\Check;
2122

@@ -44,7 +45,7 @@ abstract class Files extends RepositoryAware
4445
protected string $moveExistingTo = '';
4546

4647
/**
47-
* Hook that should be handled.
48+
* Hook(s) that should be handled.
4849
*
4950
* @var array<int, string>
5051
*/
@@ -81,25 +82,14 @@ public function setMoveExistingTo(string $backup): static
8182
*/
8283
public function setHook(string $hook): self
8384
{
84-
if (empty($hook)) {
85-
return $this;
86-
}
87-
88-
/** @var array<string> $hooks */
89-
$hooks = explode(',', $hook);
90-
$hooks = array_map('trim', $hooks);
91-
92-
$hooksValidationCallback = static function (string $hook): bool {
93-
return !HookUtil::isInstallable($hook);
94-
};
95-
if (!empty(($invalidHooks = array_filter($hooks, $hooksValidationCallback)))) {
96-
throw new Exception\InvalidHookName(
97-
'Invalid hook name \'' . implode('\', \'', $invalidHooks) . '\''
98-
);
99-
}
100-
101-
$this->hooksToHandle = $hooks;
85+
$arg = new Arg(
86+
$hook,
87+
static function (string $hook): bool {
88+
return !HookUtil::isInstallable($hook);
89+
}
90+
);
10291

92+
$this->hooksToHandle = $arg->hooks();
10393
return $this;
10494
}
10595

src/Runner/Hook/Arg.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Runner\Hook;
13+
14+
use CaptainHook\App\Exception\InvalidHookName;
15+
16+
/**
17+
* Hook argument for lots of commands
18+
*
19+
* - install pre-push,pre-commit
20+
* - info commit-message
21+
*/
22+
class Arg
23+
{
24+
/**
25+
* List of hooks
26+
*
27+
* @var array<int, string>
28+
*/
29+
private array $hooks = [];
30+
31+
/**
32+
* @param string $hook
33+
* @param callable $hookValidation
34+
* @throws \CaptainHook\App\Exception\InvalidHookName
35+
*/
36+
public function __construct(string $hook, callable $hookValidation)
37+
{
38+
if (empty($hook)) {
39+
return;
40+
}
41+
42+
/** @var array<string> $hooks */
43+
$hooks = explode(',', $hook);
44+
$hooks = array_map('trim', $hooks);
45+
46+
if (!empty(($invalidHooks = array_filter($hooks, $hookValidation)))) {
47+
throw new InvalidHookName(
48+
'Invalid hook name \'' . implode('\', \'', $invalidHooks) . '\''
49+
);
50+
}
51+
$this->hooks = $hooks;
52+
}
53+
54+
/**
55+
* Return the list of hooks provided as an argument
56+
*
57+
* @return array<int, string>
58+
*/
59+
public function hooks(): array
60+
{
61+
return $this->hooks;
62+
}
63+
}

0 commit comments

Comments
 (0)