Passed
Push — master ( e83f83...b17f39 )
by Sebastian
02:59
created

FileChanged::didMatchingFileChange()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
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;
13
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Hook\Condition;
16
use CaptainHook\App\Hook\Constrained;
17
use CaptainHook\App\Hook\Restriction;
18
use CaptainHook\App\Hooks;
19
use SebastianFeldmann\Git\Repository;
20
21
/**
22
 * Class FileChange
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 4.2.0
28
 */
29
abstract class FileChanged extends File
30
{
31
    /**
32
     * List of file to watch
33
     *
34
     * @var array<string>
35
     */
36
    protected $filesToWatch;
37
38 5
    /**
39
     * FileChange constructor
40 5
     *
41 5
     * @param array<string> $files
42
     */
43
    public function __construct(array $files)
44
    {
45
        $this->filesToWatch = $files;
46
    }
47
48
    /**
49
     * Return the hook restriction information
50
     *
51
     * @return \CaptainHook\App\Hook\Restriction
52
     */
53
    public static function getRestriction(): Restriction
54
    {
55
        return Restriction::fromArray([Hooks::POST_CHECKOUT, Hooks::POST_MERGE]);
56
    }
57
58
    /**
59
     * Evaluates a condition
60
     *
61
     * @param  \CaptainHook\App\Console\IO       $io
62
     * @param  \SebastianFeldmann\Git\Repository $repository
63
     * @return bool
64 5
     */
65
    abstract public function isTrue(IO $io, Repository $repository): bool;
66 5
67 5
    /**
68
     * Use 'diff-tree' to find the changed files after this merge or checkout
69 5
     *
70
     * In case of a checkout it is easy because the arguments 'previousHead' and 'newHead' exist.
71
     * In case of a merge determining this hashes is more difficult so we are using the 'ref-log'
72
     * to do it and using 'HEAD@{1}' as the last position before the merge and 'HEAD' as the
73
     * current position after the merge.
74
     *
75
     * @param  \CaptainHook\App\Console\IO       $io
76
     * @param  \SebastianFeldmann\Git\Repository $repository
77
     * @return array<string>
78
     */
79
    protected function getChangedFiles(IO $io, Repository $repository)
80
    {
81
        $oldHash = $io->getArgument('previousHead', 'HEAD@{1}');
82
        $newHash = $io->getArgument('newHead', 'HEAD');
83
84
        return $repository->getDiffOperator()->getChangedFiles($oldHash, $newHash);
85
    }
86
}
87