Passed
Push — fix/solve-windows-ci ( 003700...709697 )
by Chema
07:40 queued 03:14
created

AppModuleCreator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 88
ccs 34
cts 35
cp 0.9714
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findFactory() 0 9 2
A findDependencyProvider() 0 11 3
A fullModuleName() 0 5 2
A findConfig() 0 9 2
A moduleName() 0 7 2
A fromClass() 0 9 1
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\DependencyProvider\DependencyProviderNotFoundException;
9
use Gacela\Framework\ClassResolver\DependencyProvider\DependencyProviderResolver;
10
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
11
use ReflectionClass;
12
13
use function strlen;
14
15
final class AppModuleCreator
16
{
17 8
    public function __construct(
18
        private FactoryResolver $factoryResolver,
19
        private ConfigResolver $configResolver,
20
        private DependencyProviderResolver $dependencyProviderResolver,
21
    ) {
22 8
    }
23
24
    /**
25
     * @param class-string $facadeClass
26
     */
27 8
    public function fromClass(string $facadeClass): AppModule
28
    {
29 8
        return new AppModule(
30 8
            $this->fullModuleName($facadeClass),
31 8
            $this->moduleName($facadeClass),
32 8
            $facadeClass,
33 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...
34 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...
35 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...
36 8
        );
37
    }
38
39
    /**
40
     * @param class-string $facadeClass
41
     */
42 8
    private function fullModuleName(string $facadeClass): string
43
    {
44 8
        $moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass);
45
46 8
        return substr($facadeClass, 0, $moduleNameIndex);
47
    }
48
49
    /**
50
     * @param class-string $facadeClass
51
     */
52 8
    private function moduleName(string $facadeClass): string
53
    {
54 8
        $fullModuleName = $this->fullModuleName($facadeClass);
55
56 8
        $moduleName = strrchr($fullModuleName, '\\') ?: $fullModuleName;
57
58 8
        return ltrim($moduleName, '\\');
59
    }
60
61
    /**
62
     * @param class-string $facadeClass
63
     */
64 8
    private function findFactory(string $facadeClass): ?string
65
    {
66 8
        $resolver = $this->factoryResolver->resolve($facadeClass);
67
68 8
        if ((new ReflectionClass($resolver))->isAnonymous()) {
69 4
            return null;
70
        }
71
72 7
        return $resolver::class;
73
    }
74
75
    /**
76
     * @param class-string $facadeClass
77
     */
78 8
    private function findConfig(string $facadeClass): ?string
79
    {
80 8
        $resolver = $this->configResolver->resolve($facadeClass);
81
82 8
        if ((new ReflectionClass($resolver))->isAnonymous()) {
83 6
            return null;
84
        }
85
86 5
        return $resolver::class;
87
    }
88
89
    /**
90
     * @param class-string $facadeClass
91
     */
92 8
    private function findDependencyProvider(string $facadeClass): ?string
93
    {
94
        try {
95 8
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
96
97 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
98
                throw new DependencyProviderNotFoundException($resolver);
99
            }
100 7
            return $resolver::class;
101 4
        } catch (DependencyProviderNotFoundException $e) {
102 4
            return null;
103
        }
104
    }
105
}
106