Passed
Push — refactor-FactoryResolver ( fe813f )
by Chema
03:42
created

AppModuleCreator::findFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
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
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\AllAppModules;
6
7
use Gacela\Framework\ClassResolver\Config\ConfigNotFoundException;
8
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
9
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderNotFoundException;
10
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
11
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
12
use ReflectionClass;
13
14
use function strlen;
15
16
final class AppModuleCreator
17
{
18 8
    public function __construct(
19
        private FactoryResolver $factoryResolver,
20
        private ConfigResolver $configResolver,
21
        private DependencyProviderResolver $dependencyProviderResolver,
22
    ) {
23 8
    }
24
25
    /**
26
     * @param class-string $facadeClass
27
     */
28 8
    public function fromClass(string $facadeClass): AppModule
29
    {
30 8
        return new AppModule(
31 8
            $this->fullModuleName($facadeClass),
32 8
            $this->moduleName($facadeClass),
33 8
            $facadeClass,
34 8
            $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...
35 8
            $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...
36 8
            $this->findDependencyProvider($facadeClass),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->findDependencyProvider($facadeClass) targeting Gacela\Console\Domain\Al...indDependencyProvider() 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...
37 8
        );
38
    }
39
40
    /**
41
     * @param class-string $facadeClass
42
     */
43 8
    private function fullModuleName(string $facadeClass): string
44
    {
45 8
        $moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass);
46
47 8
        return substr($facadeClass, 0, $moduleNameIndex);
48
    }
49
50
    /**
51
     * @param class-string $facadeClass
52
     */
53 8
    private function moduleName(string $facadeClass): string
54
    {
55 8
        $fullModuleName = $this->fullModuleName($facadeClass);
56
57 8
        $moduleName = strrchr($fullModuleName, '\\') ?: $fullModuleName;
58
59 8
        return ltrim($moduleName, '\\');
60
    }
61
62
    /**
63
     * @param class-string $facadeClass
64
     */
65 8
    private function findFactory(string $facadeClass): ?string
66
    {
67 8
        $resolver = $this->factoryResolver->resolve($facadeClass);
68
69 8
        if ((new ReflectionClass($resolver))->isAnonymous()) {
70 4
            return null;
71
        }
72
73 7
        return $resolver::class;
74
    }
75
76
    /**
77
     * @param class-string $facadeClass
78
     */
79 8
    private function findConfig(string $facadeClass): ?string
80
    {
81
        try {
82 8
            $resolver = $this->configResolver->resolve($facadeClass);
83
84 8
            if ((new ReflectionClass($resolver))->isAnonymous()) {
85 6
                throw new ConfigNotFoundException($facadeClass);
86
            }
87
88 5
            return $resolver::class;
89 6
        } catch (ConfigNotFoundException $e) {
90 6
            return null;
91
        }
92
    }
93
94
    /**
95
     * @param class-string $facadeClass
96
     */
97 8
    private function findDependencyProvider(string $facadeClass): ?string
98
    {
99
        try {
100 8
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
101
102 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
103
                throw new DependencyProviderNotFoundException($resolver);
104
            }
105 7
            return $resolver::class;
106 4
        } catch (DependencyProviderNotFoundException $e) {
107 4
            return null;
108
        }
109
    }
110
}
111