Passed
Push — add-with-all-to-modules-list ( 82b81d...0edb38 )
by Chema
03:40
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 9
    public function __construct(
18
        private FactoryResolver $factoryResolver,
19
        private ConfigResolver $configResolver,
20
        private DependencyProviderResolver $dependencyProviderResolver,
21
    ) {
22 9
    }
23
24
    /**
25
     * @param class-string $facadeClass
26
     */
27 9
    public function fromClass(string $facadeClass): AppModule
28
    {
29 9
        return new AppModule(
30 9
            $this->fullModuleName($facadeClass),
31 9
            $this->moduleName($facadeClass),
32 9
            $facadeClass,
33 9
            $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 9
            $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 9
            $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 9
        );
37
    }
38
39
    /**
40
     * @param class-string $facadeClass
41
     */
42 9
    private function fullModuleName(string $facadeClass): string
43
    {
44 9
        $moduleNameIndex = strrpos($facadeClass, '\\') ?: strlen($facadeClass);
45
46 9
        return substr($facadeClass, 0, $moduleNameIndex);
47
    }
48
49
    /**
50
     * @param class-string $facadeClass
51
     */
52 9
    private function moduleName(string $facadeClass): string
53
    {
54 9
        $fullModuleName = $this->fullModuleName($facadeClass);
55
56 9
        $moduleName = strrchr($fullModuleName, '\\') ?: $fullModuleName;
57
58 9
        return ltrim($moduleName, '\\');
59
    }
60
61
    /**
62
     * @param class-string $facadeClass
63
     */
64 9
    private function findFactory(string $facadeClass): ?string
65
    {
66 9
        $resolver = $this->factoryResolver->resolve($facadeClass);
67
68 9
        if ((new ReflectionClass($resolver))->isAnonymous()) {
69 5
            return null;
70
        }
71
72 8
        return $resolver::class;
73
    }
74
75
    /**
76
     * @param class-string $facadeClass
77
     */
78 9
    private function findConfig(string $facadeClass): ?string
79
    {
80 9
        $resolver = $this->configResolver->resolve($facadeClass);
81
82 9
        if ((new ReflectionClass($resolver))->isAnonymous()) {
83 7
            return null;
84
        }
85
86 6
        return $resolver::class;
87
    }
88
89
    /**
90
     * @param class-string $facadeClass
91
     */
92 9
    private function findDependencyProvider(string $facadeClass): ?string
93
    {
94
        try {
95 9
            $resolver = $this->dependencyProviderResolver->resolve($facadeClass);
96
97 8
            if ((new ReflectionClass($resolver))->isAnonymous()) {
98
                throw new DependencyProviderNotFoundException($resolver);
99
            }
100 8
            return $resolver::class;
101 5
        } catch (DependencyProviderNotFoundException $e) {
102 5
            return null;
103
        }
104
    }
105
}
106