Passed
Push — main ( 604bfc...f70419 )
by Sebastian
04:51
created

FileList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 68
rs 10
ccs 25
cts 25
cp 1
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterByDirectory() 0 15 4
A replaceInAll() 0 13 3
A filterByType() 0 13 4
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;
13
14
/**
15
 * Class FileList
16
 *
17
 * Helper class performing some manipulation operations on plain file lists.
18
 *
19
 *   ['file1.txt', 'file2.txt' ...]
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 5.20.0
25
 */
26
abstract class FileList
27
{
28
    /**
29
     * Filter files by type
30
     *
31
     * @param  array<string>         $files
32
     * @param  array<string, string> $options
33
     * @return array<string>
34
     */
35 7
    public static function filterByType(array $files, array $options): array
36
    {
37 7
        if (!isset($options['of-type'])) {
38 6
            return $files;
39
        }
40
41 1
        $filtered = [];
42 1
        foreach ($files as $file) {
43 1
            if (str_ends_with($file, $options['of-type'])) {
44 1
                $filtered[] = $file;
45
            }
46
        }
47 1
        return $filtered;
48
    }
49
50
    /**
51
     * Filter staged files by directory
52
     *
53
     * @param  array<string>         $files
54
     * @param  array<string, string> $options
55
     * @return array<string>
56
     */
57 11
    public static function filterByDirectory(array $files, array $options): array
58
    {
59 11
        if (!isset($options['in-dir'])) {
60 9
            return $files;
61
        }
62
63 3
        $directory = $options['in-dir'];
64 3
        $filtered  = [];
65 3
        foreach ($files as $file) {
66 3
            if (str_starts_with($file, $directory)) {
67 3
                $filtered[] = $file;
68
            }
69
        }
70
71 3
        return $filtered;
72
    }
73
74
    /**
75
     * Run search replace for all files
76
     *
77
     * @param  array<string>         $files
78
     * @param  array<string, string> $options
79
     * @return array<string>
80
     */
81 11
    public static function replaceInAll(array $files, array $options): array
82
    {
83 11
        if (!isset($options['replace'])) {
84 9
            return $files;
85
        }
86
87 2
        $search  = $options['replace'];
88 2
        $replace = $options['with'] ?? '';
89
90 2
        foreach ($files as $index => $file) {
91 2
            $files[$index] = str_replace($search, $replace, $file);
92
        }
93 2
        return $files;
94
    }
95
}
96