Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Merger
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
6
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
 merge
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
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
22/**
23 * Merges, deduplicates, and sorts .gitignore entries.
24 *
25 * This service SHALL combine canonical and project-specific .gitignore
26 * definitions into a single normalized result. The resulting entry list MUST
27 * exclude blank lines and comment lines from the merged output, MUST remove
28 * duplicate entries, and MUST group directory entries before file entries.
29 * Directory and file groups SHALL be sorted independently in ascending string
30 * order to provide deterministic output.
31 */
32  class Merger implements MergerInterface
33{
34    /**
35     * Initializes the merger with a classifier implementation.
36     *
37     * The classifier MUST be capable of determining whether a normalized
38     * .gitignore entry represents a directory or a file pattern. When no
39     * classifier is provided, a default Classifier instance SHALL be used.
40     *
41     * @param ClassifierInterface $classifier the classifier responsible for
42     *                                        distinguishing directory entries
43     *                                        from file entries during merging
44     */
45    public function __construct(
46        private ClassifierInterface $classifier = new Classifier()
47    ) {}
48
49    /**
50     * Merges canonical and project .gitignore entries into a normalized result.
51     *
52     * The implementation MUST combine entries from both sources, MUST remove
53     * duplicates, and MUST ignore blank or commented entries after trimming.
54     * Entries identified as directories SHALL be collected separately from file
55     * entries. Each group MUST be sorted using string comparison, and directory
56     * entries MUST appear before file entries in the final result.
57     *
58     * The returned GitIgnore instance SHALL preserve the project path provided by
59     * the $project argument.
60     *
61     * @param GitIgnoreInterface $canonical The canonical .gitignore source whose
62     *                                      entries provide the shared baseline.
63     * @param GitIgnoreInterface $project The project-specific .gitignore source
64     *                                    whose path MUST be preserved in the
65     *                                    merged result.
66     *
67     * @return GitIgnoreInterface A new merged .gitignore representation containing
68     *                            normalized, deduplicated, and ordered entries.
69     */
70    public function merge(GitIgnoreInterface $canonical, GitIgnoreInterface $project): GitIgnoreInterface
71    {
72        $entries = array_unique(array_merge($canonical->entries(), $project->entries()));
73
74        $directories = [];
75        $files = [];
76
77        foreach ($entries as $entry) {
78            $trimmed = trim($entry);
79            if ('' === $trimmed) {
80                continue;
81            }
82
83            if (str_starts_with($trimmed, '#')) {
84                continue;
85            }
86
87            if ($this->classifier->isDirectory($trimmed)) {
88                $directories[] = $trimmed;
89            } else {
90                $files[] = $trimmed;
91            }
92        }
93
94        sort($directories, \SORT_STRING);
95        sort($files, \SORT_STRING);
96
97        $mergedEntries = array_merge($directories, $files);
98
99        return new GitIgnore($project->path(), array_values($mergedEntries));
100    }
101}