Passed
Push — master ( aba9bd...1f9498 )
by Sander
02:13 queued 12s
created

PathHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 30
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getNormalizedPath() 0 7 2
A toAbsolutePath() 0 10 2
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