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

File   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 13
c 1
b 0
f 0
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A anyFileInHaystack() 0 8 3
A isFileInList() 0 8 3
A allFilesInHaystack() 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 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 5.2.0
28
 */
29
abstract class File implements Condition, Constrained
30
{
31
    /**
32
     * Return the hook restriction information
33
     *
34
     * @return \CaptainHook\App\Hook\Restriction
35
     */
36
    abstract public static function getRestriction(): Restriction;
37
38
    /**
39
     * Evaluates a condition
40
     *
41
     * @param  \CaptainHook\App\Console\IO       $io
42
     * @param  \SebastianFeldmann\Git\Repository $repository
43
     * @return bool
44
     */
45
    abstract public function isTrue(IO $io, Repository $repository): bool;
46
47
    /**
48
     * Check if all of the given files can be found in a haystack of files
49
     *
50
     * IMPORTANT: If no files are provided this is always true.
51
     *
52
     * @param  array<string> $files
53
     * @param  array<string> $haystack
54
     * @return bool
55
     */
56
    protected function allFilesInHaystack(array $files, array $haystack): bool
57
    {
58
        foreach ($files as $filePattern) {
59
            if (!$this->isFileInList($haystack, $filePattern)) {
60
                return false;
61
            }
62
        }
63
        return true;
64
    }
65
66
    /**
67
     * Check if any of the given files can be found in a haystack of files
68
     *
69
     * IMPORTANT: If no files are provided this is always false.
70
     *
71
     * @param  array<string> $files
72
     * @param  array<string> $haystack
73
     * @return bool
74
     */
75
    protected function anyFileInHaystack(array $files, array $haystack): bool
76
    {
77
        foreach ($files as $filePattern) {
78
            if ($this->isFileInList($haystack, $filePattern)) {
79
                return true;
80
            }
81
        }
82
        return false;
83
    }
84
85
    /**
86
     * Check if a file matching a `fnmatch` pattern was changed
87
     *
88
     * @param  array<string> $listOfFiles List of files to scan
89
     * @param  string        $pattern     Pattern in fnmatch format to look for
90
     * @return bool
91
     */
92
    protected function isFileInList(array $listOfFiles, string $pattern): bool
93
    {
94
        foreach ($listOfFiles as $file) {
95
            if (fnmatch($pattern, $file)) {
96
                return true;
97
            }
98
        }
99
        return false;
100
    }
101
}
102