Passed
Push — improve-list-modules-output ( e63970 )
by Chema
04:12
created

AppModuleCreator::findFactory()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
c 1
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\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\FactoryNotFoundException;
12
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
13
use ReflectionClass;
14
15
final class AppModuleCreator
16
{
17
    /**
18
     * @param class-string $facadeClass
19
     */
20 7
    public function fromClass(string $facadeClass): AppModule
21
    {
22 7
        return new AppModule(
23 7
            $this->moduleName($facadeClass),
24 7
            $facadeClass,
25 7
            $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...
26 7
            $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...
27 7
            $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...
28 7
        );
29
    }
30
31
    /**
32
     * @param class-string $facadeClass
33
     */
34 7
    private function moduleName(string $facadeClass): string
35
    {
36 7
        $parts = explode('\\', $facadeClass);
37 7
        array_pop($parts);
38
39 7
        return (string)end($parts);
40
    }
41
42
    /**
43
     * @param class-string $facadeClass
44
     */
45 7
    private function findFactory(string $facadeClass): ?string
46
    {
47
        try {
48 7
            $resolver = (new FactoryResolver())->resolve($facadeClass);
49
50 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
51 3
                throw new FactoryNotFoundException($facadeClass);
52
            }
53
54 6
            return $resolver::class;
55 3
        } catch (FactoryNotFoundException $e) {
56 3
            return null;
57
        }
58
    }
59
60
    /**
61
     * @param class-string $facadeClass
62
     */
63 7
    private function findConfig(string $facadeClass): ?string
64
    {
65
        try {
66 7
            $resolver = (new ConfigResolver())->resolve($facadeClass);
67
68 7
            if ((new ReflectionClass($resolver))->isAnonymous()) {
69 5
                throw new ConfigNotFoundException($facadeClass);
70
            }
71
72 4
            return $resolver::class;
73 5
        } catch (ConfigNotFoundException $e) {
74 5
            return null;
75
        }
76
    }
77
78
    /**
79
     * @param class-string $facadeClass
80
     */
81 7
    private function findDependencyProvider(string $facadeClass): ?string
82
    {
83
        try {
84 7
            $resolver = (new DependencyProviderResolver())->resolve($facadeClass);
85
86 6
            if ((new ReflectionClass($resolver))->isAnonymous()) {
87
                throw new DependencyProviderNotFoundException($resolver);
88
            }
89 6
            return $resolver::class;
90 3
        } catch (DependencyProviderNotFoundException $e) {
91 3
            return null;
92
        }
93
    }
94
}
95