Passed
Push — master ( 85921c...ba0af4 )
by Petr
08:14
created

Paths::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace kalanis\kw_clipr\Clipr;
4
5
6
use kalanis\kw_clipr\CliprException;
7
use kalanis\kw_clipr\Interfaces;
8
9
10
/**
11
 * Class Paths
12
 * @package kalanis\kw_clipr\Clipr
13
 * Paths to accessing tasks/commands somewhere on volumes
14
 * It's singleton, because it's passed to different parts of Clipr - loaders and one of modules - and at first needs
15
 * to be set outside of the system
16
 */
17
class Paths
18
{
19
    /** @var self */
20
    protected static $instance = null;
21
    /** @var array<string, string> */
22
    protected $paths = [];
23
24 13
    public static function getInstance(): self
25
    {
26
        // @phpstan-ignore-next-line
27 13
        if (empty(static::$instance)) {
28 1
            static::$instance = new self();
29
        }
30 13
        return static::$instance;
31
    }
32
33 1
    protected function __construct()
34
    {
35 1
    }
36
37
    /**
38
     * @codeCoverageIgnore why someone would run that?!
39
     */
40
    private function __clone()
41
    {
42
    }
43
44
    /**
45
     * @param string $namespace
46
     * @param string $path
47
     * @throws CliprException
48
     * @return $this
49
     */
50 11
    public function addPath(string $namespace, string $path): self
51
    {
52 11
        $realPath = realpath($path);
53 11
        if (false === $realPath) {
54 1
            throw new CliprException(sprintf('Unknown path *%s*!', $path), Interfaces\IStatuses::STATUS_BAD_CONFIG);
55
        }
56 10
        $this->paths[$namespace] = $path;
57 10
        return $this;
58
    }
59
60
    /**
61
     * @return array<string, string>
62
     */
63 4
    public function getPaths(): array
64
    {
65 4
        return $this->paths;
66
    }
67
68 7
    public function clearPaths(): void
69
    {
70 7
        $this->paths = [];
71
    }
72
73 5
    public function classToRealFile(string $classPath, string $namespace): string
74
    {
75
        // remove ext
76 5
        $withExt = mb_strripos($classPath, Interfaces\ISources::EXT_PHP);
77 5
        $classNoExt = (false !== $withExt)
78 5
        && (mb_strlen($classPath) == $withExt + mb_strlen(Interfaces\ISources::EXT_PHP))
79 1
            ? mb_substr($classPath, 0, $withExt)
80 5
            : $classPath;
81
        // change slashes
82 5
        $classNoExt = strtr($classNoExt, ['\\' => DIRECTORY_SEPARATOR, '/' => DIRECTORY_SEPARATOR, ':' => DIRECTORY_SEPARATOR]);
83
        // rewrite namespace
84 5
        return mb_substr($classNoExt, mb_strlen($namespace));
85
    }
86
87 5
    public function realFileToClass(string $dir, string $file): ?string
88
    {
89 5
        $dirLen = mb_strlen($dir);
90 5
        foreach ($this->paths as $namespace => $path) {
91
            // got some path
92 5
            $compLen = min($dirLen, mb_strlen($path));
93 5
            $lgPath = mb_substr(Useful::mb_str_pad($path, $compLen, '-'), 0, $compLen);
94 5
            $lgDir = mb_substr(Useful::mb_str_pad($dir, $compLen, '-'), 0, $compLen);
95 5
            if ($lgDir == $lgPath) {
96
                // rewrite namespace
97 4
                $lcDir = DIRECTORY_SEPARATOR == $dir[0] ? $dir : DIRECTORY_SEPARATOR . $dir;
98 4
                $end = $namespace . mb_substr($lcDir, $compLen);
99
                // change slashes
100 4
                $namespaced = DIRECTORY_SEPARATOR == mb_substr($end, -1) ? $end : $end . DIRECTORY_SEPARATOR;
101 4
                $namespaced = strtr($namespaced, DIRECTORY_SEPARATOR, '\\');
102
                // remove ext
103 4
                $withExt = mb_strripos($file, Interfaces\ISources::EXT_PHP);
104 4
                $withoutExt = (false !== $withExt) ? mb_substr($file, 0, $withExt) : $file ;
105
                // return named class
106 4
                return $namespaced . $withoutExt;
107
            }
108
        }
109 1
        return null;
110
    }
111
}
112