Passed
Push — feat/use-provider ( 6f868a )
by Chema
04:35
created

AppModuleCreator::findAbstractProvider()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\AllAppModules;
6
7
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
8
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
9
use Gacela\Framework\ClassResolver\Provider\ProviderNotFoundException;
10
use Gacela\Framework\ClassResolver\Provider\ProviderResolver;
11
use ReflectionClass;
12
13
use function strlen;
14
15
final class AppModuleCreator
16
{
17
    public function __construct(
18
        private readonly FactoryResolver $factoryResolver,
19
        private readonly ConfigResolver $configResolver,
20
        private readonly ProviderResolver $dependencyProviderResolver,
21
    ) {
22
    }
23
24
    /**
25
     * @param class-string $facadeClass
26
     */
27
    public function fromClass(string $facadeClass): AppModule
28
    {
29
        return new AppModule(
30
            $this->fullModuleName($facadeClass),
31
            $this->moduleName($facadeClass),
32
            $facadeClass,
33
            $this->findFactory($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findFactory($facadeClass) targeting Gacela\Console\Domain\Al...eCreator::findFactory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
34
            $this->findConfig($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findConfig($facadeClass) targeting Gacela\Console\Domain\Al...leCreator::findConfig() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
            $this->findAbstractProvider($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findAbstractProvider($facadeClass) targeting Gacela\Console\Domain\Al...:findAbstractProvider() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
        );
37
    }
38
39
    /**
40
     * @param class-string $facadeClass
41
     */
42
    private function fullModuleName(string $facadeClass): string
43
    {
44
        $moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass);
45
46
        return substr($facadeClass, 0, $moduleNameIndex);
47
    }
48
49
    /**
50
     * @param class-string $facadeClass
51
     */
52
    private function moduleName(string $facadeClass): string
53
    {
54
        $fullModuleName = $this->fullModuleName($facadeClass);
55
56
        $moduleName = strrchr($fullModuleName, '\\') ?: $fullModuleName;
57
58
        return ltrim($moduleName, '\\');
59
    }
60
61
    /**
62
     * @param class-string $facadeClass
63
     */
64
    private function findFactory(string $facadeClass): ?string
65
    {
66
        $resolver = $this->factoryResolver->resolve($facadeClass);
67
68
        if ((new ReflectionClass($resolver))->isAnonymous()) {
69
            return null;
70
        }
71
72
        return $resolver::class;
73
    }
74
75
    /**
76
     * @param class-string $facadeClass
77
     */
78
    private function findConfig(string $facadeClass): ?string
79
    {
80
        $resolver = $this->configResolver->resolve($facadeClass);
81
82
        if ((new ReflectionClass($resolver))->isAnonymous()) {
83
            return null;
84
        }
85
86
        return $resolver::class;
87
    }
88
89
    /**
90
     * @param class-string $facadeClass
91
     */
92
    private function findAbstractProvider(string $facadeClass): ?string
93
    {
94
        try {
95
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
96
97
            if ((new ReflectionClass($resolver))->isAnonymous()) {
98
                throw new ProviderNotFoundException($resolver);
99
            }
100
101
            return $resolver::class;
102
        } catch (ProviderNotFoundException) {
103
            return null;
104
        }
105
    }
106
}
107