Passed
Push — main ( 6c9a60...f17c77 )
by Sebastian
13:38
created

Arg::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 3
rs 10
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 33
    public function __construct(string $hook, callable $hookValidation)
37
    {
38 33
        if (empty($hook)) {
39 1
            return;
40
        }
41
42
        /** @var array<string> $hooks */
43 32
        $hooks = explode(',', $hook);
44 32
        $hooks = array_map('trim', $hooks);
45
46 32
        if (!empty(($invalidHooks = array_filter($hooks, $hookValidation)))) {
47 6
            throw new InvalidHookName(
48 6
                'Invalid hook name \'' . implode('\', \'', $invalidHooks) . '\''
49 6
            );
50
        }
51 26
        $this->hooks = $hooks;
52
    }
53
54
    /**
55
     * Return the list of hooks provided as an argument
56
     *
57
     * @return array<int, string>
58
     */
59 27
    public function hooks(): array
60
    {
61 27
        return $this->hooks;
62
    }
63
}
64