Passed
Push — master ( aba9bd...af331f )
by Sander
02:04
created

PathHelper::getNormalizedPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink;
17
18
use InvalidArgumentException;
19
20
class PathHelper
21
{
22
    protected string $path;
23
24
    protected string $absolutePath;
25
26
    public function __construct(string $path)
27
    {
28
        $this->path = $path;
29
    }
30
31
    public function toAbsolutePath(string $workingDirectory): PathHelper
32
    {
33
        $real = realpath($workingDirectory . DIRECTORY_SEPARATOR . $this->path);
34
        if ($real === false) {
35
            throw new InvalidArgumentException(
36
                sprintf('Cannot resolve absolute path to %s.', $this->path)
37
            );
38
        }
39
40
        return new PathHelper($real);
41
    }
42
43
    public function getNormalizedPath(): string
44
    {
45
        if (substr($this->path, -1) === DIRECTORY_SEPARATOR) {
46
            return substr($this->path, 0, -1);
47
        }
48
49
        return $this->path;
50
    }
51
}
52