Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.33% covered (success)
98.33%
59 / 60
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Writer
98.33% covered (success)
98.33%
59 / 60
83.33% covered (warning)
83.33%
5 / 6
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
7
 parseEntry
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
 firstUnescapedWhitespacePosition
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\GitAttributes;
21
22use FastForward\DevTools\Filesystem\FilesystemInterface;
23
24use function Safe\preg_split;
25
26/**
27 * Persists normalized .gitattributes content.
28 *
29 * This writer SHALL align attribute declarations using the longest path spec,
30 * write the provided textual content to the target path, and MUST append a
31 * final trailing line feed for deterministic formatting.
32 */
33  class Writer implements WriterInterface
34{
35    /**
36     * @param FilesystemInterface $filesystem the filesystem service responsible for writing the file
37     */
38    public function __construct(
39        private FilesystemInterface $filesystem
40    ) {}
41
42    /**
43     * Writes the .gitattributes content to the specified filesystem path.
44     *
45     * @param string $gitattributesPath The filesystem path to the .gitattributes file.
46     * @param string $content The merged .gitattributes content to persist.
47     *
48     * @return void
49     */
50    public function write(string $gitattributesPath, string $content): void
51    {
52        $this->filesystem->dumpFile($gitattributesPath, $this->render($content));
53    }
54
55    /**
56     * Renders normalized .gitattributes content.
57     *
58     * @param string $content the merged .gitattributes content to normalize
59     *
60     * @return string the normalized file content
61     */
62    public function render(string $content): string
63    {
64        return $this->format($content);
65    }
66
67    /**
68     * Formats .gitattributes content with aligned attribute columns.
69     *
70     * @param string $content The merged .gitattributes content to normalize.
71     *
72     * @return string
73     */
74    private function format(string $content): string
75    {
76        $rows = [];
77        $maxPathSpecLength = 0;
78
79        foreach (preg_split('/\R/', $content) as $line) {
80            $trimmedLine = trim((string) $line);
81
82            if ('' === $trimmedLine) {
83                $rows[] = [
84                    'type' => 'raw',
85                    'line' => '',
86                ];
87
88                continue;
89            }
90
91            if (str_starts_with($trimmedLine, '#')) {
92                $rows[] = [
93                    'type' => 'raw',
94                    'line' => $trimmedLine,
95                ];
96
97                continue;
98            }
99
100            $entry = $this->parseEntry($trimmedLine);
101
102            if (null === $entry) {
103                $rows[] = [
104                    'type' => 'raw',
105                    'line' => $trimmedLine,
106                ];
107
108                continue;
109            }
110
111            $maxPathSpecLength = max($maxPathSpecLength, \strlen($entry['path_spec']));
112            $rows[] = [
113                'type' => 'entry',
114                'path_spec' => $entry['path_spec'],
115                'attributes' => $entry['attributes'],
116            ];
117        }
118
119        $formattedLines = [];
120
121        foreach ($rows as $row) {
122            if ('entry' !== $row['type']) {
123                $formattedLines[] = $row['line'];
124
125                continue;
126            }
127
128            $formattedLines[] = str_pad($row['path_spec'], $maxPathSpecLength + 1) . $row['attributes'];
129        }
130
131        return implode("\n", $formattedLines) . "\n";
132    }
133
134    /**
135     * Parses a .gitattributes entry into its path spec and attribute segment.
136     *
137     * @param string $line The normalized .gitattributes line.
138     *
139     * @return array{path_spec: string, attributes: string}|null
140     */
141    private function parseEntry(string $line): ?array
142    {
143        $separatorPosition = $this->firstUnescapedWhitespacePosition($line);
144
145        if (null === $separatorPosition) {
146            return null;
147        }
148
149        $pathSpec = substr($line, 0, $separatorPosition);
150        $attributes = ltrim(substr($line, $separatorPosition));
151
152        if ('' === $pathSpec || '' === $attributes) {
153            return null;
154        }
155
156        return [
157            'path_spec' => $pathSpec,
158            'attributes' => $attributes,
159        ];
160    }
161
162    /**
163     * Locates the first non-escaped whitespace separator in a line.
164     *
165     * @param string $line the line to inspect
166     *
167     * @return int|null
168     */
169    private function firstUnescapedWhitespacePosition(string $line): ?int
170    {
171        $length = \strlen($line);
172
173        for ($position = 0; $position < $length; ++$position) {
174            if (! \in_array($line[$position], [' ', "\t"], true)) {
175                continue;
176            }
177
178            $backslashCount = 0;
179
180            for ($index = $position - 1; $index >= 0 && '\\' === $line[$index]; --$index) {
181                ++$backslashCount;
182            }
183
184            if (0 === $backslashCount % 2) {
185                return $position;
186            }
187        }
188
189        return null;
190    }
191}