Passed
Push — bugfix/support-windows ( 517fb4...0e99e7 )
by Chema
04:47
created

ClassInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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