Completed
Push — master ( 95fe89...6b0ea2 )
by Dave
19s queued 16s
created

ProjectRoot::withRelativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common;
6
7
use LogicException;
8
use Webmozart\Assert\Assert;
9
use Webmozart\PathUtil\Path;
10
11
/**
12
 * Holds the root directory for the project being analysed.
13
 *
14
 * It is recommended that all static analysis tools report the full path of the files they analyse.
15
 * The ResultsParses then use the getRelativePath method of the class and all file paths in the baseline are
16
 * stored as relative to the project root.
17
 *
18
 * NOTE: Assuming the GitHistoryAnalyser is being used then the root directory contains the .git directory.
19
 */
20
class ProjectRoot
21
{
22
    /**
23
     * @var string
24
     */
25
    private $rootDirectory;
26
27
    /**
28
     * @var string
29
     */
30
    private $relativePath = '';
31
32
    public static function fromCurrentWorkingDirectory(string $currentWorkingDirectory): self
33
    {
34
        Assert::true(Path::isAbsolute($currentWorkingDirectory));
35
        $rootDirectory = Path::canonicalize($currentWorkingDirectory);
36
37
        return new self($rootDirectory);
38
    }
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(string $rootDirectory)
52
    {
53
        $this->rootDirectory = $rootDirectory;
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
    public function getAbsoluteFileName(RelativeFileName $relativeFileName): AbsoluteFileName
93
    {
94
        $absoluteFileName = Path::join([
95
            $this->rootDirectory,
96
            $this->relativePath,
97
            $relativeFileName->getFileName(),
98
        ]);
99
100
        try {
101
            return new AbsoluteFileName($absoluteFileName);
102
        } catch (InvalidPathException $e) {
103
            throw new LogicException("Invalid $absoluteFileName");
104
        }
105
    }
106
107
    public function getProjectRootDirectory(): string
108
    {
109
        return $this->rootDirectory;
110
    }
111
}
112