ClassInfo::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 is_object;
10
use function is_string;
11
use function sprintf;
12
13
final class ClassInfo implements ClassInfoInterface
14
{
15
    public const MODULE_NAME_ANONYMOUS = 'module-name@anonymous';
16
17
    /** @var array<string,array<string,self>> */
18
    private static array $callerClassCache;
19
20 49
    public function __construct(
21
        private readonly string $callerModuleNamespace,
22
        private readonly string $callerModuleName,
23
        private readonly string $cacheKey,
24
        private readonly string $resolvableType = '',
25
    ) {
26 49
    }
27
28
    /**
29
     * @param object|class-string $caller
0 ignored issues
show
Documentation Bug introduced by
The doc comment object|class-string at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in object|class-string.
Loading history...
30
     */
31 82
    public static function from(object|string $caller, string $resolvableType = ''): self
32
    {
33 82
        if (is_object($caller)) {
34 37
            return self::fromObject($caller, $resolvableType);
35
        }
36
37 73
        return self::fromString($caller, $resolvableType);
38
    }
39
40 72
    public function getModuleNamespace(): string
41
    {
42 72
        return $this->callerModuleNamespace;
43
    }
44
45 71
    public function getModuleName(): string
46
    {
47 71
        return $this->callerModuleName;
48
    }
49
50 81
    public function getCacheKey(): string
51
    {
52 81
        return $this->cacheKey;
53
    }
54
55
    public function getResolvableType(): string
56
    {
57
        return $this->resolvableType;
58
    }
59
60
    public function toString(): string
61
    {
62
        return sprintf(
63
            '{callerModuleNamespace:"%s", callerModuleName:"%s", resolvableType:"%s", cacheKey:"%s"}',
64
            $this->callerModuleNamespace,
65
            $this->callerModuleName,
66
            $this->resolvableType,
67
            $this->cacheKey,
68
        );
69
    }
70
71 37
    private static function fromObject(object $callerObject, string $resolvableType): self
72
    {
73 37
        $callerClass = $callerObject::class;
74
75 37
        return self::fromString($callerClass, $resolvableType);
76
    }
77
78 82
    private static function fromString(string $callerClass, string $resolvableType): self
79
    {
80 82
        if (isset(self::$callerClassCache[$callerClass][$resolvableType])) {
81 49
            return self::$callerClassCache[$callerClass][$resolvableType];
82
        }
83
84 44
        /** @var list<string> $callerClassParts */
85 44
        $callerClassParts = explode('\\', ltrim($callerClass, '\\'));
86 44
        $lastCallerClassPart = end($callerClassParts);
87 44
        $filepath = is_string($lastCallerClassPart) ? $lastCallerClassPart : '';
88
        $filename = self::normalizeFilename($filepath);
89 44
90 4
        if (str_contains($callerClass, 'anonymous')) {
91 4
            $callerClassParts = [
92 4
                self::MODULE_NAME_ANONYMOUS . '\\' . $filename,
93 4
                $filepath,
94
            ];
95
        }
96 44
97
        $callerFullNamespace = implode('\\', array_slice($callerClassParts, 0, count($callerClassParts) - 1));
98 44
99
        $callerModuleNamespace = substr($callerFullNamespace, 0, (int)strrpos($callerFullNamespace, '\\'));
100
        /**
101
         * @psalm-suppress InvalidArrayOffset
102
         *
103
         * @var string $callerModuleName
104 44
         */
105 44
        $callerModuleName = $callerClassParts[count($callerClassParts) - 2] ?? '';
106
        $cacheKey = GlobalKey::fromClassName(sprintf('\\%s\\%s', $callerFullNamespace, $resolvableType));
107 44
108 44
        $self = new self($callerModuleNamespace, $callerModuleName, $cacheKey, $resolvableType);
109
        self::$callerClassCache[$callerClass][$resolvableType] = $self;
110 44
111
        return $self;
112
    }
113 44
114
    private static function normalizeFilename(string $filepath): string
115 44
    {
116 44
        $filename = basename($filepath);
117
        $filename = substr($filename, 0, (int)strpos($filename, ':'));
118 44
119 41
        if (false === ($pos = strpos($filename, '.'))) {
120
            return $filename;
121
        }
122 4
123
        return substr($filename, 0, $pos);
124
    }
125
}
126