Passed
Push — main ( 241083...5efb85 )
by Sebastian
04:13
created

Any::isTrue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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