Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Writer
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 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%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of fast-forward/dev-tools.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/dev-tools
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\DevTools\GitIgnore;
20
21use Symfony\Component\Filesystem\Filesystem;
22
23/**
24 * Renders and persists normalized .gitignore content.
25 *
26 * This writer SHALL transform a GitIgnoreInterface representation into the
27 * textual format expected by a .gitignore file and MUST persist that content to
28 * the target path exposed by the provided object. Implementations MUST write a
29 * trailing line feed to ensure consistent file formatting.
30 */
31  class Writer implements WriterInterface
32{
33    /**
34     * Creates a writer with the filesystem dependency used for persistence.
35     *
36     * The provided filesystem implementation MUST support writing file contents
37     * to the target path returned by a GitIgnoreInterface instance.
38     *
39     * @param Filesystem $filesystem The filesystem service responsible for
40     *                               writing the rendered .gitignore content.
41     */
42    public function __construct(
43        private Filesystem $filesystem
44    ) {}
45
46    /**
47     * Writes the normalized .gitignore entries to the target file path.
48     *
49     * The implementation SHALL join all entries using a Unix line feed and MUST
50     * append a final trailing line feed to the generated content. The resulting
51     * content MUST be written to the path returned by $gitignore->path().
52     *
53     * @param GitIgnoreInterface $gitignore The .gitignore representation whose
54     *                                      path and entries SHALL be written.
55     *
56     * @return void
57     */
58    public function write(GitIgnoreInterface $gitignore): void
59    {
60        $content = implode("\n", $gitignore->entries()) . "\n";
61
62        $this->filesystem->dumpFile($gitignore->path(), $content);
63    }
64}