Passed
Push — main ( e485ec...2a6706 )
by Sebastian
04:37
created

Files   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isTrue() 0 12 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
declare(strict_types=1);
13
14
namespace CaptainHook\App\Hook\Condition\Branch;
15
16
use CaptainHook\App\Console\IO;
17
use CaptainHook\App\Hook\Condition;
18
use CaptainHook\App\Hook\FileList;
19
use SebastianFeldmann\Git\Repository;
20
21
/**
22
 * Files condition
23
 *
24
 * Example configuration:
25
 *
26
 *   "action": "some-action"
27
 *   "conditions": [
28
 *     {"exec": "\\CaptainHook\\App\\Hook\\Condition\\Branch\\Files",
29
 *      "args": [
30
 *        {"compare-to": "main", "of-type": "php"}
31
 *     ]}
32
 *   ]
33
 *
34
 * @package CaptainHook
35
 * @author  Sebastian Feldmann <[email protected]>
36
 * @link    https://github.com/captainhookphp/captainhook
37
 * @since   Class available since Release 5.21.0
38
 */
39
class Files implements Condition
40
{
41
    /**
42
     * Options
43
     *  - compare-to:   source branch if known, otherwise the reflog is used to figure it out
44
     *  - in-directory: only check for files in given directory
45
     *  - of-type:      only check for files of given type
46
     *
47
     * @var array<string>
48
     */
49
    private array $options;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param array<string> $options
55
     */
56 3
    public function __construct(array $options)
57
    {
58 3
        $this->options = $options;
59
    }
60
61
    /**
62
     * Check if the current branch contains changes to files
63
     *
64
     * @param  \CaptainHook\App\Console\IO       $io
65
     * @param  \SebastianFeldmann\Git\Repository $repository
66
     * @return bool
67
     */
68 3
    public function isTrue(IO $io, Repository $repository): bool
69
    {
70 3
        $branch = $repository->getInfoOperator()->getCurrentBranch();
71 3
        $start  = $this->options['compared-to'] ?? $repository->getLogOperator()->getBranchRevFromRefLog($branch);
72
73 3
        if (empty($start)) {
74 1
            return false;
75
        }
76
77 2
        $files = $repository->getLogOperator()->getChangedFilesSince($start . '..HEAD', ['A', 'C', 'M', 'R']);
78
79 2
        return count(FileList::filter($files, $this->options)) > 0;
80
    }
81
}
82