• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / gitattributes / 11745881131

08 Nov 2024 04:31PM UTC coverage: 86.061% (+49.6%) from 36.424%
11745881131

push

github

eliashaeussler
Initial commit

142 of 165 new or added lines in 10 files covered. (86.06%)

142 of 165 relevant lines covered (86.06%)

4.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/GitattributesParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/gitattributes".
7
 *
8
 * Copyright (C) 2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Gitattributes;
25

26
use function array_filter;
27
use function array_map;
28
use function array_values;
29
use function file;
30
use function is_readable;
31

32
/**
33
 * GitattributesParser.
34
 *
35
 * @author Elias Häußler <elias@haeussler.dev>
36
 * @license GPL-3.0-or-later
37
 */
38
final class GitattributesParser
39
{
40
    public function __construct(
2✔
41
        private readonly string $rootPath,
42
    ) {}
2✔
43

44
    /**
45
     * @throws Exception\FileDoesNotExist
46
     * @throws Exception\FileIsNotReadable
47
     */
48
    public function parse(string $filename): Rule\Ruleset
2✔
49
    {
50
        $file = Helper\FilesystemHelper::createFileObject($filename, $this->rootPath);
2✔
51
        $path = $file->getPathname();
2✔
52

53
        if (!file_exists($path)) {
2✔
54
            throw new Exception\FileDoesNotExist($path);
1✔
55
        }
56
        if (!is_readable($path)) {
1✔
NEW
57
            throw new Exception\FileIsNotReadable($path);
×
58
        }
59

60
        $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
1✔
61

62
        if (false === $lines) {
1✔
NEW
63
            throw new Exception\FileIsNotReadable($path);
×
64
        }
65

66
        $rules = array_values(
1✔
67
            array_filter(
1✔
68
                array_map(Rule\Rule::fromString(...), $lines),
1✔
69
            ),
1✔
70
        );
1✔
71

72
        return new Rule\Ruleset($file, $rules);
1✔
73
    }
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc