PathFixer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 13
c 2
b 0
f 1
dl 0
loc 66
ccs 17
cts 17
cp 1
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fix() 0 3 1
A getTrimLength() 0 3 1
A __construct() 0 4 1
A getPath() 0 3 1
A execute() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Compolomus\FSHelper;
6
7
use Compolomus\FSHelper\Exceptions\PathNotFoundException;
8
9
class PathFixer
10
{
11
    /**
12
     * Absolute or relative path
13
     */
14
    private string $path;
15
16
    /**
17
     * Amount of symbols which can be removed from provided path
18
     */
19
    private int $trimLength;
20
21
    /**
22
     * PathFixer constructor.
23
     * @throws PathNotFoundException
24
     */
25 7
    public function __construct(string $path)
26
    {
27 7
        $this->path = $path;
28 7
        $this->execute();
29 6
    }
30
31
    /**
32
     * Provided path processing
33
     *
34
     * @throws PathNotFoundException
35
     */
36 7
    private function execute(): void
37
    {
38
        // Get rale path from provided path (because it can be relative)
39 7
        $real = realpath($this->path);
40
41
        // Throw an exception if the path does not exist
42 7
        if (!$real) {
43 1
            throw new PathNotFoundException($this->path);
44
        }
45
46
        // Get directory name by provided real path
47 6
        $base = basename($real);
48
49
        // Calculate trimmed length between real path and directory only
50 6
        $this->trimLength = mb_strlen($real) - mb_strlen($base);
51 6
    }
52
53
    /**
54
     * Remove required amount of characters from start of provided path
55
     */
56 3
    public function fix(string $path): string
57
    {
58 3
        return mb_substr($path, $this->trimLength);
59
    }
60
61
    /**
62
     * Return path added via constructor
63
     */
64 1
    public function getPath(): string
65
    {
66 1
        return $this->path;
67
    }
68
69
    /**
70
     * Return amount of symbols which will be trimmed
71
     */
72 1
    public function getTrimLength(): int
73
    {
74 1
        return $this->trimLength;
75
    }
76
}
77