Passed
Push — feature/remove-setup-as-array ( 3bb37a...da7ebe )
by Jesús
04:16 queued 36s
created

ClassInfo::from()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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 $callerNamespace;
22
    private string $cacheKey;
23
24 28
    public function __construct(
25
        string $callerNamespace,
26
        string $callerModuleName,
27
        string $cacheKey
28
    ) {
29 28
        $this->callerNamespace = $callerNamespace;
30 28
        $this->callerModuleName = $callerModuleName;
31 28
        $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 32
    public static function from($caller, string $resolvableType = ''): self
38
    {
39 32
        if (is_object($caller)) {
40 30
            return self::fromObject($caller, $resolvableType);
41
        }
42
43 2
        return self::fromString($caller, $resolvableType);
44
    }
45
46 35
    public function getCacheKey(): string
47
    {
48 35
        return $this->cacheKey;
49
    }
50
51 25
    public function getModule(): string
52
    {
53 25
        return $this->callerModuleName;
54
    }
55
56 23
    public function getFullNamespace(): string
57
    {
58 23
        return $this->callerNamespace;
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->callerNamespace,
68
        );
69
    }
70
71 30
    private static function fromObject(object $callerObject, string $resolvableType): self
72
    {
73 30
        $callerClass = get_class($callerObject);
74
75 30
        return self::fromString($callerClass, $resolvableType);
76
    }
77
78 32
    private static function fromString(string $callerClass, string $resolvableType): self
79
    {
80 32
        if (isset(self::$callerClassCache[$callerClass][$resolvableType])) {
81 13
            return self::$callerClassCache[$callerClass][$resolvableType];
82
        }
83
        /** @var string[] $callerClassParts */
84 23
        $callerClassParts = explode('\\', ltrim($callerClass, '\\'));
85 23
        $lastCallerClassPart = end($callerClassParts);
86 23
        $filepath = is_string($lastCallerClassPart) ? $lastCallerClassPart : '';
87 23
        $filename = self::normalizeFilename($filepath);
88
89 23
        if (strpos($filepath, 'anonymous') !== false) {
90
            $callerClassParts = [
91 2
                self::MODULE_NAME_ANONYMOUS . '\\' . $filename,
92
                $filepath,
93
            ];
94
        }
95
96 23
        $callerNamespace = implode('\\', array_slice($callerClassParts, 0, count($callerClassParts) - 1));
97 23
        $callerModuleName = $callerClassParts[count($callerClassParts) - 2] ?? '';
98 23
        $cacheKey = GlobalKey::fromClassName(
99 23
            sprintf('\\%s\\%s', $callerNamespace, $resolvableType)
100
        );
101
102 23
        $self = new self($callerNamespace, $callerModuleName, $cacheKey);
103 23
        self::$callerClassCache[$callerClass][$resolvableType] = $self;
104
105 23
        return $self;
106
    }
107
108 23
    private static function normalizeFilename(string $filepath): string
109
    {
110 23
        $filename = basename($filepath);
111 23
        $filename = substr($filename, 0, (int)strpos($filename, ':'));
112
113 23
        if (false === ($pos = strpos($filename, '.'))) {
114 21
            return $filename;
115
        }
116
117 2
        return substr($filename, 0, $pos);
118
    }
119
}
120