OfType   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 40
ccs 7
cts 7
cp 1
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isTrue() 0 5 1
A __construct() 0 4 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 OfType
21
 *
22
 * All FileStaged conditions are only applicable for `pre-commit` hooks.
23
 * The diff filter argument is optional.
24
 *
25
 * Example configuration:
26
 *
27
 * <code>
28
 * {
29
 *   "action": "some-action"
30
 *   "conditions": [
31
 *     {
32
 *       "exec": "CaptainHook.FileStaged.OfType",
33
 *       "args": [
34
 *         "php",
35
 *         ["A", "C"]
36
 *       ]
37
 *     }
38
 *   ]
39
 * }
40
 * </code>
41
 *
42
 * Multiple types can be configured using a comma-separated string or an array
43
 *  - "php,html,xml"
44
 *  - ["php", "html", "xml"]
45
 *
46
 * @package CaptainHook
47
 * @author  Sebastian Feldmann <[email protected]>
48
 * @link    https://github.com/captainhook-git/captainhook
49
 * @since   Class available since Release 5.0.0
50
 */
51
abstract class OfType extends File
52
{
53
    /**
54
     * File type to check e.g. 'php' or 'html'
55
     *
56
     * @var array<int, string>
57
     */
58
    protected array $suffixes;
59
60
    /**
61
     * --diff-filter option
62
     *
63
     * @var array<int, string>
64
     */
65
    protected array $diffFilter;
66
67
    /**
68
     * OfType constructor
69
     *
70
     * @param array<int, string>|string $types
71
     * @param array<int, string>|string $filter
72
     */
73 7
    public function __construct(array|string $types, array|string $filter = [])
74
    {
75 7
        $this->suffixes   = is_array($types) ? $types : explode(',', $types);
0 ignored issues
show
introduced by
The condition is_array($types) is always true.
Loading history...
76 7
        $this->diffFilter = FilterUtil::filterFromConfigValue($filter);
77
    }
78
79
    /**
80
     * Evaluates the condition
81
     *
82
     * @param  \CaptainHook\App\Console\IO       $io
83
     * @param  \SebastianFeldmann\Git\Repository $repository
84
     * @return bool
85
     */
86 7
    public function isTrue(IO $io, Repository $repository): bool
87
    {
88 7
        $files    = $this->getFiles($io, $repository, $this->diffFilter);
89 7
        $filtered = $this->filterFilesByType($files, $this->suffixes);
90 7
        return count($filtered) > 0;
91
    }
92
}
93