Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Writer
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
3
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%
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
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\GitIgnore;
21
22use FastForward\DevTools\Filesystem\FilesystemInterface;
23
24/**
25 * Renders and persists normalized .gitignore content.
26 *
27 * This writer SHALL transform a GitIgnoreInterface representation into the
28 * textual format expected by a .gitignore file and MUST persist that content to
29 * the target path exposed by the provided object. Implementations MUST write a
30 * trailing line feed to ensure consistent file formatting.
31 */
32  class Writer implements WriterInterface
33{
34    /**
35     * Creates a writer with the filesystem dependency used for persistence.
36     *
37     * The provided filesystem implementation MUST support writing file contents
38     * to the target path returned by a GitIgnoreInterface instance.
39     *
40     * @param FilesystemInterface $filesystem The filesystem service responsible for
41     *                                        writing the rendered .gitignore content.
42     */
43    public function __construct(
44        private FilesystemInterface $filesystem
45    ) {}
46
47    /**
48     * Writes the normalized .gitignore entries to the target file path.
49     *
50     * The implementation SHALL join all entries using a Unix line feed and MUST
51     * append a final trailing line feed to the generated content. The resulting
52     * content MUST be written to the path returned by $gitignore->path().
53     *
54     * @param GitIgnoreInterface $gitignore The .gitignore representation whose
55     *                                      path and entries SHALL be written.
56     *
57     * @return void
58     */
59    public function write(GitIgnoreInterface $gitignore): void
60    {
61        $this->filesystem->dumpFile($gitignore->path(), $this->render($gitignore));
62    }
63
64    /**
65     * Renders the normalized .gitignore content.
66     *
67     * @param GitIgnoreInterface $gitignore the gitignore representation to render
68     *
69     * @return string the rendered file content
70     */
71    public function render(GitIgnoreInterface $gitignore): string
72    {
73        return implode("\n", $gitignore->entries()) . "\n";
74    }
75}