Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.77% |
30 / 31 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WorkingProjectPathResolver | |
96.77% |
30 / 31 |
|
66.67% |
2 / 3 |
8 | |
0.00% |
0 / 1 |
| getProjectPath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getToolingExcludedDirectories | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| getToolingSourcePaths | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(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 | |
| 20 | namespace FastForward\DevTools\Path; |
| 21 | |
| 22 | use Symfony\Component\Finder\Finder; |
| 23 | use Symfony\Component\Filesystem\Path; |
| 24 | |
| 25 | use function Safe\getcwd; |
| 26 | |
| 27 | /** |
| 28 | * Provides canonical repository-root paths that are not part of the managed workspace. |
| 29 | */ |
| 30 | class WorkingProjectPathResolver |
| 31 | { |
| 32 | /** |
| 33 | * Returns the current working project directory or a path under it. |
| 34 | * |
| 35 | * @param string $path the optional relative segment to append under the project directory |
| 36 | */ |
| 37 | public static function getProjectPath(string $path = ''): string |
| 38 | { |
| 39 | if ('' !== $path && Path::isAbsolute($path)) { |
| 40 | return $path; |
| 41 | } |
| 42 | |
| 43 | return Path::join(getcwd(), $path); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the project directories that static-analysis and coding-style tooling SHOULD skip. |
| 48 | * |
| 49 | * @param string $baseDir the optional repository base directory used to materialize absolute paths |
| 50 | * |
| 51 | * @return list<string> |
| 52 | */ |
| 53 | public static function getToolingExcludedDirectories(string $baseDir = ''): array |
| 54 | { |
| 55 | return [ |
| 56 | ManagedWorkspace::getOutputDirectory(baseDir: $baseDir), |
| 57 | Path::join($baseDir, 'backup'), |
| 58 | Path::join($baseDir, 'cache'), |
| 59 | Path::join($baseDir, 'public'), |
| 60 | Path::join($baseDir, 'resources'), |
| 61 | Path::join($baseDir, 'tmp'), |
| 62 | Path::join($baseDir, 'vendor'), |
| 63 | Path::join($baseDir, '*/vendor'), |
| 64 | Path::join($baseDir, '*/vendor/*'), |
| 65 | Path::join($baseDir, '**/vendor'), |
| 66 | Path::join($baseDir, '**/vendor/*'), |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns PHP source files that tooling SHOULD inspect without traversing generated directories. |
| 72 | * |
| 73 | * @param string $baseDir the optional repository base directory used to materialize absolute paths |
| 74 | * |
| 75 | * @return list<string> |
| 76 | */ |
| 77 | public static function getToolingSourcePaths(string $baseDir = ''): array |
| 78 | { |
| 79 | $workingDirectory = '' === $baseDir ? getcwd() : $baseDir; |
| 80 | $finder = Finder::create() |
| 81 | ->files() |
| 82 | ->name('*.php') |
| 83 | ->in($workingDirectory) |
| 84 | ->exclude(['.dev-tools', 'backup', 'cache', 'public', 'resources', 'tmp', 'vendor']) |
| 85 | ->sortByName(); |
| 86 | $paths = []; |
| 87 | |
| 88 | foreach ($finder as $file) { |
| 89 | $realPath = $file->getRealPath(); |
| 90 | |
| 91 | if (false === $realPath) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | $relativePath = Path::makeRelative($realPath, $workingDirectory); |
| 96 | $paths[] = Path::join($baseDir, $relativePath); |
| 97 | } |
| 98 | |
| 99 | return $paths; |
| 100 | } |
| 101 | } |