Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ExportIgnoreFilter
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 filter
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 normalizePath
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
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\GitAttributes;
20
21use function Safe\preg_replace;
22
23/**
24 * Filters export-ignore candidates using normalized path comparisons.
25 *
26 * This filter SHALL compare configured keep-in-export paths against canonical
27 * candidates while ignoring leading and trailing slash differences. It MUST
28 * preserve the original candidate ordering in the filtered result.
29 */
30 class ExportIgnoreFilter implements ExportIgnoreFilterInterface
31{
32    /**
33     * Filters export-ignore candidates using the configured keep-in-export paths.
34     *
35     * @param list<string> $candidates the canonical candidate paths
36     * @param list<string> $keepInExportPaths the paths that MUST remain exportable
37     *
38     * @return list<string> the filtered export-ignore candidates
39     */
40    public function filter(array $candidates, array $keepInExportPaths): array
41    {
42        $keptPathLookup = [];
43
44        foreach ($keepInExportPaths as $path) {
45            $normalizedPath = $this->normalizePath($path);
46
47            if ('' === $normalizedPath) {
48                continue;
49            }
50
51            $keptPathLookup[$normalizedPath] = true;
52        }
53
54        return array_values(array_filter(
55            $candidates,
56            fn(string $candidate): bool => ! isset($keptPathLookup[$this->normalizePath($candidate)])
57        ));
58    }
59
60    /**
61     * Normalizes a configured path for stable matching.
62     *
63     * @param string $path the raw path from candidates or Composer extra config
64     *
65     * @return string the normalized path used for comparisons
66     */
67    private function normalizePath(string $path): string
68    {
69        $trimmedPath = trim($path);
70
71        if ('' === $trimmedPath) {
72            return '';
73        }
74
75        $normalizedPath = preg_replace('#/+#', '/', '/' . ltrim($trimmedPath, '/')) ?? $trimmedPath;
76
77        return '/' === $normalizedPath ? $normalizedPath : rtrim($normalizedPath, '/');
78    }
79}