ThatIs   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 80
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isTrue() 0 6 1
A setupSuffixes() 0 7 3
A setupDirectories() 0 7 3
A __construct() 0 7 2
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 ThatIs
21
 *
22
 * All FileStaged conditions are only applicable for `pre-commit` hooks.
23
 *
24
 * Example configuration:
25
 *
26
 * <code>
27
 * {
28
 *   "action": "some-action"
29
 *   "conditions": [
30
 *     {
31
 *       "exec": "CaptainHook.FileStaged.ThatIs",
32
 *       "args": [
33
 *         {"ofType": "php", "inDirectory": "foo/", "diff-filter": ["A", "C"]}
34
 *       ]
35
 *     }
36
 *   ]
37
 * }
38
 * </code>
39
 *
40
 * @package CaptainHook
41
 * @author  Sebastian Feldmann <[email protected]>
42
 * @link    https://github.com/captainhook-git/captainhook
43
 * @since   Class available since Release 5.16.0
44
 */
45
abstract class ThatIs extends File
46
{
47
    /**
48
     * Directory path to check e.g. 'src/' or 'path/To/Custom/Directory/'
49
     *
50
     * @var string[]
51
     */
52
    private array $directories;
53
54
    /**
55
     * File type to check e.g. 'php' or 'html'
56
     *
57
     * @var string[]
58
     */
59
    private array $suffixes;
60
61
    /**
62
     * --diff-filter options
63
     *
64
     * @var array<int, string>
65
     */
66
    protected array $diffFilter;
67
68
    /**
69
     * OfType constructor
70
     *
71
     * @param array<string, mixed> $options
72
     */
73 18
    public function __construct(array $options)
74
    {
75 18
        $this->setupDirectories($options);
76 18
        $this->setupSuffixes($options);
77
78 18
        $diffFilter       = $options['diffFilter'] ?? [];
79 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

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