Passed
Push — misc/refactoring-path-finder ( 6857eb )
by Chema
09:46
created

ClassInfo   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 105
ccs 38
cts 43
cp 0.8837
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 7 1
A from() 0 7 2
A fromObject() 0 5 1
A normalizeFilename() 0 10 2
A getModuleNamespace() 0 3 1
A getModuleName() 0 3 1
A __construct() 0 8 1
A getCacheKey() 0 3 1
A fromString() 0 28 4
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
use function is_object;
11
use function is_string;
12
13
final class ClassInfo
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
    private string $callerModuleName;
21
    private string $callerModuleNamespace;
22
    private string $cacheKey;
23
24 39
    public function __construct(
25
        string $callerModuleNamespace,
26
        string $callerModuleName,
27
        string $cacheKey
28
    ) {
29 39
        $this->callerModuleNamespace = $callerModuleNamespace;
30 39
        $this->callerModuleName = $callerModuleName;
31 39
        $this->cacheKey = $cacheKey;
32
    }
33
34
    /**
35
     * @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...
36
     */
37 63
    public static function from($caller, string $resolvableType = ''): self
38
    {
39 63
        if (is_object($caller)) {
40 47
            return self::fromObject($caller, $resolvableType);
41
        }
42
43 29
        return self::fromString($caller, $resolvableType);
44
    }
45
46 62
    public function getCacheKey(): string
47
    {
48 62
        return $this->cacheKey;
49
    }
50
51 42
    public function getModuleName(): string
52
    {
53 42
        return $this->callerModuleName;
54
    }
55
56 41
    public function getModuleNamespace(): string
57
    {
58 41
        return $this->callerModuleNamespace;
59
    }
60
61
    public function toString(): string
62
    {
63
        return sprintf(
64
            'ClassInfo{cacheKey:"%s", callerModuleName:"%s", callerNamespace:"%s"}',
65
            $this->cacheKey,
66
            $this->callerModuleName,
67
            $this->callerModuleNamespace,
68
        );
69
    }
70
71 47
    private static function fromObject(object $callerObject, string $resolvableType): self
72
    {
73 47
        $callerClass = get_class($callerObject);
74
75 47
        return self::fromString($callerClass, $resolvableType);
76
    }
77
78 63
    private static function fromString(string $callerClass, string $resolvableType): self
79
    {
80 63
        if (isset(self::$callerClassCache[$callerClass][$resolvableType])) {
81 35
            return self::$callerClassCache[$callerClass][$resolvableType];
82
        }
83
        /** @var list<string> $callerClassParts */
84 34
        $callerClassParts = explode('\\', ltrim($callerClass, '\\'));
85 34
        $lastCallerClassPart = end($callerClassParts);
86 34
        $filepath = is_string($lastCallerClassPart) ? $lastCallerClassPart : '';
87 34
        $filename = self::normalizeFilename($filepath);
88
89 34
        if (strpos($filepath, 'anonymous') !== false) {
90
            $callerClassParts = [
91
                self::MODULE_NAME_ANONYMOUS . '\\' . $filename,
92
                $filepath,
93
            ];
94
        }
95
96 34
        $callerFullNamespace = implode('\\', array_slice($callerClassParts, 0, count($callerClassParts) - 1));
97
98 34
        $callerModuleNamespace = substr($callerFullNamespace, 0, (int)strrpos($callerFullNamespace, '\\'));
99 34
        $callerModuleName = $callerClassParts[count($callerClassParts) - 2] ?? '';
100 34
        $cacheKey = GlobalKey::fromClassName(sprintf('\\%s\\%s', $callerFullNamespace, $resolvableType));
101
102 34
        $self = new self($callerModuleNamespace, $callerModuleName, $cacheKey);
103 34
        self::$callerClassCache[$callerClass][$resolvableType] = $self;
104
105 34
        return $self;
106
    }
107
108 34
    private static function normalizeFilename(string $filepath): string
109
    {
110 34
        $filename = basename($filepath);
111 34
        $filename = substr($filename, 0, (int)strpos($filename, ':'));
112
113 34
        if (false === ($pos = strpos($filename, '.'))) {
114 32
            return $filename;
115
        }
116
117 4
        return substr($filename, 0, $pos);
118
    }
119
}
120