Passed
Push — master ( 70cad6...f32c0c )
by Sebastian
01:46
created

FileChanged   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getChangedFiles() 0 6 1
A __construct() 0 3 1
A didMatchingFileChange() 0 8 3
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 SebastianFeldmann\Git\Repository;
17
18
/**
19
 * Class FileChange
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 4.2.0
25
 */
26
abstract class FileChanged implements Condition
27
{
28
    /**
29
     * List of file to watch
30
     *
31
     * @var string[]
32
     */
33
    protected $filesToWatch;
34
35
    /**
36
     * FileChange constructor
37
     *
38 5
     * @param string[] $files
39
     */
40 5
    public function __construct(array $files)
41 5
    {
42
        $this->filesToWatch = $files;
43
    }
44
45
    /**
46
     * Evaluates a condition
47
     *
48
     * @param  \CaptainHook\App\Console\IO       $io
49
     * @param  \SebastianFeldmann\Git\Repository $repository
50
     * @return bool
51
     */
52
    abstract public function isTrue(IO $io, Repository $repository): bool;
53
54
    /**
55
     * Use 'diff-tree' to find the changed files after this merge or checkout
56
     *
57
     * In case of a checkout it is easy because the arguments 'previousHead' and 'newHead' exist.
58
     * In case of a merge determining this hashes is more difficult so we are using the 'ref-log'
59
     * to do it and using 'HEAD@{1}' as the last position before the merge and 'HEAD' as the
60
     * current position after the merge.
61
     *
62
     * @param  \CaptainHook\App\Console\IO       $io
63
     * @param  \SebastianFeldmann\Git\Repository $repository
64 5
     * @return array|string[]
65
     */
66 5
    protected function getChangedFiles(IO $io, Repository $repository)
67 5
    {
68
        $oldHash = $io->getArgument('previousHead', 'HEAD@{1}');
69 5
        $newHash = $io->getArgument('newHead', 'HEAD');
70
71
        return $repository->getDiffOperator()->getChangedFiles($oldHash, $newHash);
72
    }
73
74
    /**
75
     * Check if a file matching a `fnmatch` pattern was changed
76
     *
77
     * @param  array  $changedFiles
78
     * @param  string $pattern
79
     * @return bool
80
     */
81
    protected function didMatchingFileChange(array $changedFiles, string $pattern): bool
82
    {
83
        foreach ($changedFiles as $file) {
84
            if (fnmatch($pattern, $file)) {
85
                return true;
86
            }
87
        }
88
        return false;
89
    }
90
}
91