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

eliashaeussler / gitattributes / 11573362694

29 Oct 2024 11:59AM UTC coverage: 25.0%. First build
11573362694

push

github

eliashaeussler
Initial commit

35 of 140 new or added lines in 9 files covered. (25.0%)

35 of 140 relevant lines covered (25.0%)

1.31 hits per line

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

81.4
/src/Entity/Attribute/Attribute.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\Entity\Attribute;
25

26
use Stringable;
27

28
use function array_map;
29
use function count;
30
use function explode;
31
use function str_starts_with;
32
use function trim;
33

34
/**
35
 * Attribute.
36
 *
37
 * @author Elias Häußler <elias@haeussler.dev>
38
 * @license GPL-3.0-or-later
39
 */
40
final class Attribute implements Stringable
41
{
42
    private function __construct(
16✔
43
        private readonly AttributeState $state,
44
        private readonly ?AttributeName $name = null,
45
        private readonly ?string $value = null,
46
    ) {}
16✔
47

48
    public static function set(AttributeName $name): self
4✔
49
    {
50
        return new self(AttributeState::Set, $name);
4✔
51
    }
52

53
    public static function unset(AttributeName $name): self
4✔
54
    {
55
        return new self(AttributeState::Unset, $name);
4✔
56
    }
57

58
    public static function setToValue(AttributeName $name, string $value): self
4✔
59
    {
60
        return new self(AttributeState::Value, $name, $value);
4✔
61
    }
62

63
    public static function unspecified(): self
4✔
64
    {
65
        return new self(AttributeState::Unspecified);
4✔
66
    }
67

68
    public static function fromString(string $attribute): self
8✔
69
    {
70
        $attribute = trim($attribute);
8✔
71

72
        if ('' === $attribute) {
8✔
73
            return self::unspecified();
2✔
74
        }
75

76
        $parts = array_map(trim(...), explode('=', $attribute, 2));
6✔
77

78
        if (2 === count($parts)) {
6✔
79
            return self::setToValue(
2✔
80
                AttributeName::from($parts[0]),
2✔
81
                $parts[1],
2✔
82
            );
2✔
83
        }
84

85
        if (str_starts_with($parts[0], '-')) {
4✔
86
            return self::unset(
2✔
87
                AttributeName::from(substr($parts[0], 1)),
2✔
88
            );
2✔
89
        }
90

91
        return self::set(
2✔
92
            AttributeName::from($parts[0]),
2✔
93
        );
2✔
94
    }
95

NEW
96
    public function state(): AttributeState
×
97
    {
NEW
98
        return $this->state;
×
99
    }
100

NEW
101
    public function name(): ?AttributeName
×
102
    {
NEW
103
        return $this->name;
×
104
    }
105

NEW
106
    public function value(): ?string
×
107
    {
NEW
108
        return $this->value;
×
109
    }
110

111
    public function toString(): string
8✔
112
    {
113
        $name = $this->name->value ?? '';
8✔
114

115
        return match ($this->state) {
8✔
116
            AttributeState::Set => $name,
8✔
117
            AttributeState::Unset => '-'.$name,
6✔
118
            AttributeState::Unspecified => '',
4✔
119
            AttributeState::Value => $name.'='.$this->value,
8✔
120
        };
8✔
121
    }
122

NEW
123
    public function __toString(): string
×
124
    {
NEW
125
        return $this->toString();
×
126
    }
127
}
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