Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ExistenceChecker
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
6 / 6
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
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filterExisting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDirectory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 absolutePath
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\GitAttributes;
21
22use FastForward\DevTools\Filesystem\Filesystem;
23use FastForward\DevTools\Filesystem\FilesystemInterface;
24
25/**
26 * Checks the existence of files and directories in a given base path.
27 *
28 * This class determines which candidate paths from the canonical list
29 * actually exist in the target repository, enabling selective export-ignore rules.
30 */
31  class ExistenceChecker implements ExistenceCheckerInterface
32{
33    /**
34     * @param FilesystemInterface $filesystem
35     */
36    public function __construct(
37        private FilesystemInterface $filesystem = new Filesystem()
38    ) {}
39
40    /**
41     * Checks if a path exists as a file or directory.
42     *
43     * @param string $basePath the repository base path used to resolve the candidate
44     * @param string $path The path to check (e.g., "/.github/" or "/.editorconfig")
45     *
46     * @return bool True if the path exists as a file or directory
47     */
48    public function exists(string $basePath, string $path): bool
49    {
50        return $this->filesystem->exists($this->absolutePath($basePath, $path));
51    }
52
53    /**
54     * Filters a list of paths to only those that exist.
55     *
56     * @param string $basePath the repository base path used to resolve the candidates
57     * @param list<string> $paths The paths to filter
58     *
59     * @return list<string> Only the paths that exist
60     */
61    public function filterExisting(string $basePath, array $paths): array
62    {
63        return array_values(array_filter($paths, fn(string $path): bool => $this->exists($basePath, $path)));
64    }
65
66    /**
67     * Checks if a path is a directory.
68     *
69     * @param string $basePath the repository base path used to resolve the candidate
70     * @param string $path The path to check (e.g., "/.github/")
71     *
72     * @return bool True if the path exists and is a directory
73     */
74    public function isDirectory(string $basePath, string $path): bool
75    {
76        return is_dir($this->absolutePath($basePath, $path));
77    }
78
79    /**
80     * Checks if a path is a file.
81     *
82     * @param string $basePath the repository base path used to resolve the candidate
83     * @param string $path The path to check (e.g., "/.editorconfig")
84     *
85     * @return bool True if the path exists and is a file
86     */
87    public function isFile(string $basePath, string $path): bool
88    {
89        return is_file($this->absolutePath($basePath, $path));
90    }
91
92    /**
93     * Resolves a candidate path against the repository base path.
94     *
95     * @param string $basePath the repository base path
96     * @param string $path the candidate path in canonical form
97     *
98     * @return string the absolute path used for filesystem checks
99     */
100    private function absolutePath(string $basePath, string $path): string
101    {
102        return rtrim($basePath, '/\\') . $path;
103    }
104}