ClassInfo   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromObject() 0 5 1
A toString() 0 8 1
A from() 0 7 2
A getModuleNamespace() 0 3 1
A getCacheKey() 0 3 1
A getResolvableType() 0 3 1
A __construct() 0 6 1
A getModuleName() 0 3 1
A fromString() 0 34 4
A normalizeFilename() 0 10 2
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
    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
    }
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
    public static function from(object|string $caller, string $resolvableType = ''): self
32
    {
33
        if (is_object($caller)) {
34
            return self::fromObject($caller, $resolvableType);
35
        }
36
37
        return self::fromString($caller, $resolvableType);
38
    }
39
40
    public function getModuleNamespace(): string
41
    {
42
        return $this->callerModuleNamespace;
43
    }
44
45
    public function getModuleName(): string
46
    {
47
        return $this->callerModuleName;
48
    }
49
50
    public function getCacheKey(): string
51
    {
52
        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
    private static function fromObject(object $callerObject, string $resolvableType): self
72
    {
73
        $callerClass = $callerObject::class;
74
75
        return self::fromString($callerClass, $resolvableType);
76
    }
77
78
    private static function fromString(string $callerClass, string $resolvableType): self
79
    {
80
        if (isset(self::$callerClassCache[$callerClass][$resolvableType])) {
81
            return self::$callerClassCache[$callerClass][$resolvableType];
82
        }
83
84
        /** @var list<string> $callerClassParts */
85
        $callerClassParts = explode('\\', ltrim($callerClass, '\\'));
86
        $lastCallerClassPart = end($callerClassParts);
87
        $filepath = is_string($lastCallerClassPart) ? $lastCallerClassPart : '';
88
        $filename = self::normalizeFilename($filepath);
89
90
        if (str_contains($callerClass, 'anonymous')) {
91
            $callerClassParts = [
92
                self::MODULE_NAME_ANONYMOUS . '\\' . $filename,
93
                $filepath,
94
            ];
95
        }
96
97
        $callerFullNamespace = implode('\\', array_slice($callerClassParts, 0, count($callerClassParts) - 1));
98
99
        $callerModuleNamespace = substr($callerFullNamespace, 0, (int)strrpos($callerFullNamespace, '\\'));
100
        /**
101
         * @psalm-suppress InvalidArrayOffset
102
         *
103
         * @var string $callerModuleName
104
         */
105
        $callerModuleName = $callerClassParts[count($callerClassParts) - 2] ?? '';
106
        $cacheKey = GlobalKey::fromClassName(sprintf('\\%s\\%s', $callerFullNamespace, $resolvableType));
107
108
        $self = new self($callerModuleNamespace, $callerModuleName, $cacheKey, $resolvableType);
109
        self::$callerClassCache[$callerClass][$resolvableType] = $self;
110
111
        return $self;
112
    }
113
114
    private static function normalizeFilename(string $filepath): string
115
    {
116
        $filename = basename($filepath);
117
        $filename = substr($filename, 0, (int)strpos($filename, ':'));
118
119
        if (false === ($pos = strpos($filename, '.'))) {
120
            return $filename;
121
        }
122
123
        return substr($filename, 0, $pos);
124
    }
125
}
126