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

ThatIs::isTrue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
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;
17
use CaptainHook\App\Hook\Condition\File;
18
use CaptainHook\App\Hook\Constrained;
19
use CaptainHook\App\Hook\Restriction;
20
use CaptainHook\App\Hooks;
21
use SebastianFeldmann\Git\Repository;
22
23
/**
24
 * Class ThatIs
25
 *
26
 * All FileStaged conditions are only applicable for `pre-commit` hooks.
27
 *
28
 * Example configuration:
29
 *
30
 * <code>
31
 * {
32
 *   "action": "some-action"
33
 *   "conditions": [
34
 *     {
35
 *       "exec": "CaptainHook.FileStaged.ThatIs",
36
 *       "args": [
37
 *         {"ofType": "php", "inDirectory": "foo/", "diff-filter": ["A", "C"]}
38
 *       ]
39
 *     }
40
 *   ]
41
 * }
42
 * </code>
43
 *
44
 * @package CaptainHook
45
 * @author  Sebastian Feldmann <[email protected]>
46
 * @link    https://github.com/captainhook-git/captainhook
47
 * @since   Class available since Release 5.16.0
48
 */
49
abstract class ThatIs extends File
50
{
51
    /**
52
     * Directory path to check e.g. 'src/' or 'path/To/Custom/Directory/'
53
     *
54
     * @var string[]
55
     */
56
    private array $directories;
57
58
    /**
59
     * File type to check e.g. 'php' or 'html'
60
     *
61
     * @var string[]
62
     */
63
    private array $suffixes;
64
65
    /**
66
     * --diff-filter options
67
     *
68
     * @var array<int, string>
69
     */
70
    protected array $diffFilter;
71
72
    /**
73
     * OfType constructor
74
     *
75
     * @param array<string, mixed> $options
76
     */
77 18
    public function __construct(array $options)
78
    {
79 18
        $this->setupDirectories($options);
80 18
        $this->setupSuffixes($options);
81
82 18
        $diffFilter       = $options['diffFilter'] ?? [];
83 18
        $this->diffFilter = FilterUtil::sanitize(is_array($diffFilter) ? $diffFilter : str_split($diffFilter));
0 ignored issues
show
Bug introduced by
It seems like is_array($diffFilter) ? ... str_split($diffFilter) can also be of type true; however, parameter $data of CaptainHook\App\Git\Diff\FilterUtil::sanitize() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $this->diffFilter = FilterUtil::sanitize(/** @scrutinizer ignore-type */ is_array($diffFilter) ? $diffFilter : str_split($diffFilter));
Loading history...
84
    }
85
86
    /**
87
     * Read directories from option
88
     *
89
     * @param  array<string, array<string>|string> $options
90
     * @return void
91
     */
92 18
    private function setupDirectories(array $options): void
93
    {
94 18
        if (empty($options['inDirectory'])) {
95 8
            $this->directories = [];
96 8
            return;
97
        }
98 10
        $this->directories = is_array($options['inDirectory']) ? $options['inDirectory'] : [$options['inDirectory']];
99
    }
100
101
    /**
102
     * Read filetypes from options
103
     *
104
     * @param  array<string, array<string>|string> $options
105
     * @return void
106
     */
107 18
    private function setupSuffixes(array $options): void
108
    {
109 18
        if (empty($options['ofType'])) {
110 8
            $this->suffixes = [];
111 8
            return;
112
        }
113 10
        $this->suffixes = is_array($options['ofType']) ? $options['ofType'] : [$options['ofType']];
114
    }
115
116
    /**
117
     * Evaluates the condition
118
     *
119
     * @param  \CaptainHook\App\Console\IO       $io
120
     * @param  \SebastianFeldmann\Git\Repository $repository
121
     * @return bool
122
     */
123 18
    public function isTrue(IO $io, Repository $repository): bool
124
    {
125 18
        $files = $this->getFiles($io, $repository, $this->diffFilter);
126 18
        $files = $this->filterFilesByDirectory($files, $this->directories);
127 18
        $files = $this->filterFilesByType($files, $this->suffixes);
128 18
        return count($files) > 0;
129
    }
130
}
131