Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ManagedWorkspace
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 getOutputDirectory
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getCacheDirectory
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
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\Path;
21
22use Symfony\Component\Filesystem\Path;
23
24/**
25 * Provides canonical repository-local paths for generated DevTools artifacts.
26 */
27 class ManagedWorkspace
28{
29    /**
30     * @var string the output segment used for coverage artifacts
31     */
32    public const string COVERAGE = 'coverage';
33
34    /**
35     * @var string the output segment used for metrics artifacts
36     */
37    public const string METRICS = 'metrics';
38
39    /**
40     * @var string the cache segment used for phpDocumentor
41     */
42    public const string PHPDOC = 'phpdoc';
43
44    /**
45     * @var string the cache segment used for PHPUnit
46     */
47    public const string PHPUNIT = 'phpunit';
48
49    /**
50     * @var string the cache segment used for Rector
51     */
52    public const string RECTOR = 'rector';
53
54    /**
55     * @var string the cache segment used for PHP-CS-Fixer
56     */
57    public const string PHP_CS_FIXER = 'php-cs-fixer';
58
59    /**
60     * @var string the repository-local root directory for generated artifacts
61     */
62    private const string WORKSPACE_ROOT = '.dev-tools';
63
64    /**
65     * @var string the repository-local root directory for generated tool caches
66     */
67    private const string CACHE_ROOT = 'cache';
68
69    /**
70     * Returns a repository-local managed output directory.
71     *
72     * The optional $path MUST be a relative segment within the managed
73     * workspace, while $baseDir MAY provide the repository root used to
74     * materialize the same `.dev-tools` structure under a different base path.
75     *
76     * @param string $path the optional relative segment to append under the managed output root
77     * @param string $baseDir the optional repository root used to resolve the managed workspace path
78     */
79    public static function getOutputDirectory(string $path = '', string $baseDir = ''): string
80    {
81        $baseDir = '' === $baseDir
82            ? self::WORKSPACE_ROOT
83            : Path::join($baseDir, self::WORKSPACE_ROOT);
84
85        return '' === $path
86            ? $baseDir
87            : Path::join($baseDir, $path);
88    }
89
90    /**
91     * Returns a repository-local managed cache directory.
92     *
93     * The optional $path MUST be a relative cache segment, while $baseDir MAY
94     * resolve the managed workspace root before the `cache` directory is
95     * appended.
96     *
97     * @param string $path the optional relative cache segment to append under the managed cache root
98     * @param string $baseDir the optional repository root used to resolve the managed cache path
99     */
100    public static function getCacheDirectory(string $path = '', string $baseDir = ''): string
101    {
102        $baseDir = self::getOutputDirectory(self::CACHE_ROOT, $baseDir);
103
104        return '' === $path
105            ? $baseDir
106            : Path::join($baseDir, $path);
107    }
108}