File::filterFilesByDirectory()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
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/captainhook-git/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 8
    protected function allFilesInHaystack(array $files, array $haystack): bool
57
    {
58 8
        foreach ($files as $filePattern) {
59 8
            if (!$this->isFileInList($haystack, $filePattern)) {
60 4
                return false;
61
            }
62
        }
63 4
        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 13
    protected function anyFileInHaystack(array $files, array $haystack): bool
76
    {
77 13
        foreach ($files as $filePattern) {
78 13
            if ($this->isFileInList($haystack, $filePattern)) {
79 9
                return true;
80
            }
81
        }
82 5
        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 21
    protected function isFileInList(array $listOfFiles, string $pattern): bool
93
    {
94 21
        foreach ($listOfFiles as $file) {
95 21
            if (fnmatch($pattern, $file)) {
96 17
                return true;
97
            }
98
        }
99 9
        return false;
100
    }
101
102
    /**
103
     * Remove all files not in a given directory
104
     *
105
     * @param  array<string> $files
106
     * @param  array<string> $directories
107
     * @return array<string>
108
     */
109 22
    protected function filterFilesByDirectory(array $files, array $directories): array
110
    {
111 22
        if (empty($directories)) {
112 8
            return $files;
113
        }
114 14
        return array_filter($files, function ($file) use ($directories) {
115 14
            foreach ($directories as $directory) {
116 14
                if (str_starts_with($file, $directory)) {
117 8
                    return true;
118
                }
119
            }
120 14
            return false;
121 14
        });
122
    }
123
124
    /**
125
     * Remove all files not of a configured type
126
     *
127
     * @param  array<string> $files
128
     * @param  array<string> $suffixes
129
     * @return array<string>
130
     */
131 25
    protected function filterFilesByType(array $files, array $suffixes): array
132
    {
133 25
        if (empty($suffixes)) {
134 8
            return $files;
135
        }
136 17
        return array_filter($files, fn($file) => in_array(
137 17
            strtolower(pathinfo($file, PATHINFO_EXTENSION)),
0 ignored issues
show
Bug introduced by
It seems like pathinfo($file, CaptainH...ion\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
            strtolower(/** @scrutinizer ignore-type */ pathinfo($file, PATHINFO_EXTENSION)),
Loading history...
138 17
            $suffixes,
139 17
            true
140 17
        ));
141
    }
142
143
    /**
144
     * This method is responsible for finding the files that should be checked
145
     *
146
     * During pre-commit it's all staged files.
147
     * During post-checkout it's the files that changed from old-commit to new-commit
148
     *
149
     * @param  \CaptainHook\App\Console\IO       $io
150
     * @param  \SebastianFeldmann\Git\Repository $repository
151
     * @param  array<string>                     $diffFilter
152
     * @return array<string>
153
     */
154
    abstract protected function getFiles(IO $io, Repository $repository, array $diffFilter): array;
155
}
156