All::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
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\Condition\File;
13
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Git\Diff\FilterUtil;
16
use CaptainHook\App\Hook\Condition\File;
17
use SebastianFeldmann\Git\Repository;
18
19
/**
20
 * Class All
21
 *
22
 * The FileStaged condition is applicable for `pre-commit` hooks.
23
 * It checks if all configured files are staged for commit.
24
 *
25
 * Example configuration:
26
 *
27
 * <code>
28
 * {
29
 *   "action": "some-action"
30
 *   "conditions": [
31
 *     {
32
 *       "exec": "CaptainHook.FileStaged.All",
33
 *       "args": [
34
 *         ["list", "of", "files"]
35
 *       ]
36
 *     }
37
 *   ]
38
 * }
39
 * </code>
40
 *
41
 * The file list can also be defined as comma seperated string "file1,file2,file3"
42
 *
43
 * @package CaptainHook
44
 * @author  Sebastian Feldmann <[email protected]>
45
 * @link    https://github.com/captainhook-git/captainhook
46
 * @since   Class available since Release 5.27.3
47
 */
48
abstract class All extends File
49
{
50
    /**
51
     * List of file to watch
52
     *
53
     * @var array<string>
54
     */
55
    protected array $filesToWatch;
56
57
    /**
58
     * --diff-filter options
59
     *
60
     * @var array<int, string>
61
     */
62
    protected array $diffFilter;
63
64
    /**
65
     * FileStaged constructor
66
     *
67
     * @param mixed $files
68
     * @param mixed $diffFilter
69
     */
70 9
    public function __construct(mixed $files, mixed $diffFilter = [])
71
    {
72 9
        $this->filesToWatch = is_array($files) ? $files : explode(',', (string) $files);
73 9
        $this->diffFilter   = FilterUtil::filterFromConfigValue($diffFilter);
74
    }
75
76
    /**
77
     * Check if all the configured files are staged for commit
78
     *
79
     * @param  \CaptainHook\App\Console\IO       $io
80
     * @param  \SebastianFeldmann\Git\Repository $repository
81
     * @return bool
82
     */
83 8
    public function isTrue(IO $io, Repository $repository): bool
84
    {
85 8
        return $this->allFilesInHaystack($this->filesToWatch, $this->getFiles($io, $repository, $this->diffFilter));
86
    }
87
}
88