Passed
Push — feature/refactor-DocBlockResol... ( 6c9508...d6f8f5 )
by Chema
03:23
created

DocBlockResolvable::createClassNameCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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->createClassNameCache();
53
54 13
        if (!$cache->has($cacheKey)) {
55 11
            $className = $this->getClassFromDoc($method);
56 9
            $cache->put($cacheKey, $className);
57
        }
58
59 11
        return $cache->get($cacheKey);
60
    }
61
62 11
    public function normalizeResolvableType(string $resolvableType): string
63
    {
64
        /** @var list<string> $resolvableTypeParts */
65 11
        $resolvableTypeParts = explode('\\', ltrim($resolvableType, '\\'));
66 11
        $normalizedResolvableType = end($resolvableTypeParts);
67
68 11
        return is_string($normalizedResolvableType)
69 11
            ? $normalizedResolvableType
70 11
            : $resolvableType;
71
    }
72
73
    public function hasParentClass(): bool
74
    {
75
        /** @psalm-suppress ArgumentTypeCoercion */
76
        return $this->callerParentClass !== ''
77
            && method_exists($this->callerParentClass, '__call');
78
    }
79
80 13
    private function generateCacheKey(string $method): string
81
    {
82 13
        return $this->callerClass . '::' . $method;
83
    }
84
85 13
    private function createClassNameCache(): ClassNameCacheInterface
86
    {
87 13
        if (!$this->isProjectCacheEnabled()) {
88 4
            return new InMemoryCache(CustomServicesCache::class);
89
        }
90
91 9
        return new CustomServicesCache(
92 9
            Config::getInstance()->getCacheDir()
93
        );
94
    }
95
96 13
    private function isProjectCacheEnabled(): bool
97
    {
98 13
        return (new GacelaCache(Config::getInstance()))
99 13
            ->isProjectCacheEnabled();
100
    }
101
102
    /**
103
     * @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...
104
     */
105 11
    private function getClassFromDoc(string $method): string
106
    {
107 11
        $reflectionClass = new ReflectionClass($this->callerClass);
108 11
        $className = $this->searchClassOverDocBlock($reflectionClass, $method);
109 11
        if (class_exists($className)) {
110
            return $className;
111
        }
112 11
        $className = $this->searchClassOverUseStatements($reflectionClass, $className);
113 11
        if (class_exists($className)) {
114 9
            return $className;
115
        }
116 2
        throw MissingClassDefinitionException::missingDefinition($this->callerClass, $method, $className);
117
    }
118
119 11
    private function searchClassOverDocBlock(ReflectionClass $reflectionClass, string $method): string
120
    {
121 11
        $docBlock = (string)$reflectionClass->getDocComment();
122
123 11
        return (new DocBlockParser())->getClassFromMethod($docBlock, $method);
124
    }
125
126
    /**
127
     * Look the uses, to find the fully-qualified class name for the className.
128
     */
129 11
    private function searchClassOverUseStatements(ReflectionClass $reflectionClass, string $className): string
130
    {
131 11
        $fileName = (string)$reflectionClass->getFileName();
132 11
        if (!isset(self::$fileContentCache[$fileName])) {
133 9
            self::$fileContentCache[$fileName] = (string)file_get_contents($fileName);
134
        }
135 11
        $phpFile = self::$fileContentCache[$fileName];
136
137 11
        return (new UseBlockParser())->getUseStatement($className, $phpFile);
138
    }
139
}
140