Passed
Push — feature/refactor-DocBlockResol... ( 6c9508 )
by Chema
04:22
created

DocBlockResolvable::generateCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\DocBlockResolver;
6
7
use Gacela\Framework\ClassResolver\Cache\GacelaCache;
8
use Gacela\Framework\ClassResolver\ClassNameCacheInterface;
9
use Gacela\Framework\ClassResolver\DocBlockService\CustomServicesCache;
10
use Gacela\Framework\ClassResolver\DocBlockService\DocBlockParser;
11
use Gacela\Framework\ClassResolver\DocBlockService\MissingClassDefinitionException;
12
use Gacela\Framework\ClassResolver\DocBlockService\UseBlockParser;
13
use Gacela\Framework\ClassResolver\InMemoryCache;
14
use Gacela\Framework\Config\Config;
15
use ReflectionClass;
16
17
use function get_class;
18
use function is_string;
19
20
final class DocBlockResolvable
21
{
22
    /** @var array<string,string> [fileName => fileContent] cached */
23
    private static array $fileContentCache = [];
24
25
    /** @var class-string */
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
26
    private string $callerClass;
27
28
    /** @var class-string|string */
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string|string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|string.
Loading history...
29
    private string $callerParentClass;
30
31
    /**
32
     * @param class-string $callerClass
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
33
     * @param class-string|string $callerParentClass
34
     */
35 13
    private function __construct(string $callerClass, string $callerParentClass)
36
    {
37 13
        $this->callerClass = $callerClass;
38 13
        $this->callerParentClass = $callerParentClass;
39
    }
40
41 13
    public static function fromCaller(object $caller): self
42
    {
43 13
        return new self(
44 13
            get_class($caller),
45 13
            get_parent_class($caller) ?: ''
46
        );
47
    }
48
49 13
    public function getClassName(string $method): string
50
    {
51 13
        $cacheKey = $this->generateCacheKey($method);
52 13
        $cache = $this->createCustomServicesCache();
53
54 13
        if (!$cache->has($cacheKey)) {
55 11
            $className = $this->getClassFromDoc($method);
56 9
            $cache->put($cacheKey, $className);
57
        }
58
59
        /** @psalm-suppress ArgumentTypeCoercion */
60 11
        return $cache->get($cacheKey);
61
    }
62
63
64
    public function hasParentClass(): bool
65
    {
66
        /** @psalm-suppress ArgumentTypeCoercion */
67
        return $this->callerParentClass !== ''
68
            && method_exists($this->callerParentClass, '__call');
69
    }
70
71 11
    public function normalizeResolvableType(string $resolvableType): string
72
    {
73
        /** @var list<string> $resolvableTypeParts */
74 11
        $resolvableTypeParts = explode('\\', ltrim($resolvableType, '\\'));
75 11
        $normalizedResolvableType = end($resolvableTypeParts);
76
77 11
        return is_string($normalizedResolvableType)
78 11
            ? $normalizedResolvableType
79 11
            : $resolvableType;
80
    }
81
82 13
    public function createCustomServicesCache(): ClassNameCacheInterface
83
    {
84 13
        if (!$this->isProjectCacheEnabled()) {
85 4
            return new InMemoryCache(CustomServicesCache::class);
86
        }
87
88 9
        return new CustomServicesCache(
89 9
            Config::getInstance()->getCacheDir()
90
        );
91
    }
92
93
    /**
94
     * @return class-string
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
95
     */
96 11
    private function getClassFromDoc(string $method): string
97
    {
98 11
        $reflectionClass = new ReflectionClass($this->callerClass);
99 11
        $className = $this->searchClassOverDocBlock($reflectionClass, $method);
100 11
        if (class_exists($className)) {
101
            return $className;
102
        }
103 11
        $className = $this->searchClassOverUseStatements($reflectionClass, $className);
104 11
        if (class_exists($className)) {
105 9
            return $className;
106
        }
107 2
        throw MissingClassDefinitionException::missingDefinition($this->callerClass, $method, $className);
108
    }
109
110 11
    private function searchClassOverDocBlock(ReflectionClass $reflectionClass, string $method): string
111
    {
112 11
        $docBlock = (string)$reflectionClass->getDocComment();
113
114 11
        return (new DocBlockParser())->getClassFromMethod($docBlock, $method);
115
    }
116
117
    /**
118
     * Look the uses, to find the fully-qualified class name for the className.
119
     */
120 11
    private function searchClassOverUseStatements(ReflectionClass $reflectionClass, string $className): string
121
    {
122 11
        $fileName = (string)$reflectionClass->getFileName();
123 11
        if (!isset(self::$fileContentCache[$fileName])) {
124 9
            self::$fileContentCache[$fileName] = (string)file_get_contents($fileName);
125
        }
126 11
        $phpFile = self::$fileContentCache[$fileName];
127
128 11
        return (new UseBlockParser())->getUseStatement($className, $phpFile);
129
    }
130
131 13
    private function generateCacheKey(string $method): string
132
    {
133 13
        return $this->callerClass . '::' . $method;
134
    }
135
136 13
    private function isProjectCacheEnabled(): bool
137
    {
138 13
        return (new GacelaCache(Config::getInstance()))
139 13
            ->isProjectCacheEnabled();
140
    }
141
}
142