Passed
Push — master ( 9630dc...59d7dc )
by Jesús
03:24 queued 18s
created

ClassInfo::parentNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver;
6
7
use function array_slice;
8
use function count;
9
use function get_class;
10
11
final class ClassInfo
12
{
13
    public const MODULE_NAME_ANONYMOUS = 'module-name@anonymous';
14
15
    private ?string $cacheKey = null;
16
    private string $callerModuleName;
17
    private string $callerNamespace;
18
19 23
    public function __construct(object $callerObject)
20
    {
21 23
        $callerClass = get_class($callerObject);
22
23
        /** @var string[] $callerClassParts */
24 23
        $callerClassParts = explode('\\', ltrim($callerClass, '\\'));
25 23
        $lastCallerClassPart = end($callerClassParts);
26 23
        $filepath = is_string($lastCallerClassPart) ? $lastCallerClassPart : '';
27 23
        $filename = $this->normalizeFilename($filepath);
28
29 23
        if (false !== strpos($filepath, 'anonymous')) {
30
            $callerClassParts = [
31 1
                self::MODULE_NAME_ANONYMOUS . '\\' . $filename,
32 1
                $filepath,
33
            ];
34
        }
35
36 23
        $this->callerNamespace = $this->normalizeCallerNamespace(...$callerClassParts);
37 23
        $this->callerModuleName = $this->normalizeCallerModuleName(...$callerClassParts);
38 23
    }
39
40 21
    public function getCacheKey(string $resolvableType): string
41
    {
42 21
        if (!$this->cacheKey) {
43 21
            $this->cacheKey = GlobalKey::fromClassName(sprintf(
44 21
                '\\%s\\%s',
45 21
                $this->getFullNamespace(),
46
                $resolvableType
47
            ));
48
        }
49
50 21
        return $this->cacheKey;
51
    }
52
53 17
    public function getModule(): string
54
    {
55 17
        return $this->callerModuleName;
56
    }
57
58 21
    public function getFullNamespace(): string
59
    {
60 21
        return $this->callerNamespace;
61
    }
62
63 23
    private function normalizeFilename(string $filepath): string
64
    {
65 23
        $filename = basename($filepath);
66 23
        $filename = substr($filename, 0, (int)strpos($filename, ':'));
67
68 23
        if (false === ($pos = strpos($filename, '.'))) {
69 22
            return $filename;
70
        }
71
72 1
        return substr($filename, 0, $pos);
73
    }
74
75 23
    private function normalizeCallerNamespace(string ...$callerClassParts): string
76
    {
77 23
        return implode('\\', array_slice($callerClassParts, 0, count($callerClassParts) - 1));
78
    }
79
80 23
    private function normalizeCallerModuleName(string ...$callerClassParts): string
81
    {
82 23
        return $callerClassParts[count($callerClassParts) - 2] ?? '';
83
    }
84
85 15
    public function toString(): string
86
    {
87 15
        return sprintf(
88 15
            'ClassInfo{$cacheKey:%s, $callerModuleName:%s, $callerNamespace:%s}',
89 15
            $this->cacheKey ?? 'null',
90 15
            $this->callerModuleName,
91 15
            $this->callerNamespace,
92
        );
93
    }
94
}
95