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

PathHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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