ClassResolverExceptionTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 20
c 0
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findClassNameExample() 0 7 1
A buildMessage() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\ClassResolver;
6
7
use function sprintf;
8
9
trait ClassResolverExceptionTrait
10
{
11
    /**
12
     * @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...
13
     */
14
    private function buildMessage(object|string $caller, string $resolvableType): string
15
    {
16
        $callerClassInfo = ClassInfo::from($caller, $resolvableType);
17
18
        $message = 'ClassResolver Exception' . PHP_EOL;
19
        $message .= sprintf(
20
            'Cannot resolve the `%s` for your module `%s`',
21
            $resolvableType,
22
            $callerClassInfo->getModuleName(),
23
        ) . PHP_EOL;
24
25
        $message .= sprintf(
26
            'You can fix this by adding the missing `%s` to your module.',
27
            $resolvableType,
28
        ) . PHP_EOL;
29
30
        return $message . (sprintf(
31
            'E.g. `%s`',
32
            $this->findClassNameExample($callerClassInfo, $resolvableType),
33
        ) . PHP_EOL);
34
    }
35
36
    private function findClassNameExample(ClassInfo $classInfo, string $resolvableType): string
37
    {
38
        return sprintf(
39
            '\\%s\\%s\\%s',
40
            $classInfo->getModuleNamespace(),
41
            $classInfo->getModuleName(),
42
            $resolvableType,
43
        );
44
    }
45
}
46