ProjectRoot::getAbsoluteFileName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\Path;
8
use Webmozart\Assert\Assert;
9
10
/**
11
 * Holds the root directory for the project being analysed.
12
 *
13
 * It is recommended that all static analysis tools report the full path of the files they analyse.
14
 * The ResultsParses then use the getRelativePath method of the class and all file paths in the baseline are
15
 * stored as relative to the project root.
16
 *
17
 * NOTE: Assuming the GitHistoryAnalyser is being used then the root directory contains the .git directory.
18
 */
19
final class ProjectRoot
20
{
21
    /**
22
     * @var string
23
     */
24
    private $relativePath = '';
25
26
    /**
27
     * @throws InvalidPathException
28
     */
29
    public static function fromCurrentWorkingDirectory(string $currentWorkingDirectory): self
30
    {
31
        Assert::true(Path::isAbsolute($currentWorkingDirectory));
32
        $rootDirectory = Path::canonicalize($currentWorkingDirectory);
33
34
        return new self($rootDirectory);
35
    }
36
37
    /**
38
     * @throws InvalidPathException
39
     */
40
    public static function fromProjectRoot(string $projectRoot, string $currentWorkingDirectory): self
41
    {
42
        if (Path::isAbsolute($projectRoot)) {
43
            $rootDirectory = Path::canonicalize($projectRoot);
44
        } else {
45
            $rootDirectory = Path::makeAbsolute($projectRoot, $currentWorkingDirectory);
46
        }
47
48
        return new self($rootDirectory);
49
    }
50
51
    private function __construct(
52
        private string $rootDirectory,
53
    ) {
54
    }
55
56
    /**
57
     * Return a new ProjectRoot configured with a relativePath.
58
     */
59
    public function withRelativePath(string $relativePath): self
60
    {
61
        $projectRoot = new self($this->rootDirectory);
62
        $projectRoot->relativePath = $relativePath;
63
64
        return $projectRoot;
65
    }
66
67
    /**
68
     * Returns path relative to project root.
69
     *
70
     * @throws InvalidPathException
71
     */
72
    public function getPathRelativeToRootDirectory(AbsoluteFileName $absoluteFileName): RelativeFileName
73
    {
74
        $fullPath = $absoluteFileName->getFileName();
75
        if (!Path::isBasePath($this->rootDirectory, $fullPath)) {
76
            throw InvalidPathException::notInProjectRoot($fullPath, $this->rootDirectory);
77
        }
78
79
        $relativeFileName = Path::makeRelative($fullPath, $this->rootDirectory);
80
81
        return new RelativeFileName($relativeFileName);
82
    }
83
84
    public function __toString(): string
85
    {
86
        return $this->getProjectRootDirectory();
87
    }
88
89
    /**
90
     * @codeCoverageIgnore
91
     *
92
     * @throws InvalidPathException
93
     */
94
    public function getAbsoluteFileName(RelativeFileName $relativeFileName): AbsoluteFileName
95
    {
96
        $absoluteFileName = Path::join([
97
            $this->rootDirectory,
98
            $this->relativePath,
99
            $relativeFileName->getFileName(),
100
        ]);
101
102
        try {
103
            return new AbsoluteFileName($absoluteFileName);
104
        } catch (InvalidPathException) {
105
            throw new \LogicException("Invalid $absoluteFileName");
106
        }
107
    }
108
109
    public function getProjectRootDirectory(): string
110
    {
111
        return $this->rootDirectory;
112
    }
113
}
114