ExternalDefinitionsResolver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 59
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveForClass() 0 5 1
A resolveInterfaces() 0 7 2
A resolveExternalParentClass() 0 8 3
A resolveTraits() 0 7 2
A resolve() 0 9 2
A resolveForEnum() 0 4 1
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Parser\Code;
7
8
use PhUml\Code\ClassDefinition;
9
use PhUml\Code\Codebase;
10
use PhUml\Code\EnumDefinition;
11
use PhUml\Code\InterfaceDefinition;
12
use PhUml\Code\Name;
13
use PhUml\Code\TraitDefinition;
14
15
/**
16
 * It looks for external definitions from the parent of a definition, the interfaces it implements, and the traits it
17
 * uses
18
 *
19
 * An external definition is a class, trait or interface from a third party library, or a built-in class or interface
20
 */
21
final class ExternalDefinitionsResolver implements RelationshipsResolver
22
{
23 33
    public function resolve(Codebase $codebase): void
24
    {
25
        /** @var ClassDefinition|InterfaceDefinition|TraitDefinition|EnumDefinition $definition */
26 33
        foreach ($codebase->definitions() as $definition) {
27 31
            match ($definition::class) {
28 29
                ClassDefinition::class => $this->resolveForClass($definition, $codebase),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolveForClass($definition, $codebase) targeting PhUml\Parser\Code\Extern...lver::resolveForClass() 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...
29 17
                EnumDefinition::class => $this->resolveForEnum($definition, $codebase),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolveForEnum($definition, $codebase) targeting PhUml\Parser\Code\Extern...olver::resolveForEnum() 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...
30 21
                InterfaceDefinition::class => $this->resolveInterfaces($definition->parents(), $codebase),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolveInterfaces...->parents(), $codebase) targeting PhUml\Parser\Code\Extern...er::resolveInterfaces() 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...
31 15
                default => $this->resolveTraits($definition->traits(), $codebase),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolveTraits($de...n->traits(), $codebase) targeting PhUml\Parser\Code\Extern...solver::resolveTraits() 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...
32
            };
33
        }
34
    }
35
36
    /**
37
     * It resolves for its parent class, its interfaces and traits
38
     */
39 29
    private function resolveForClass(ClassDefinition $definition, Codebase $codebase): void
40
    {
41 29
        $this->resolveInterfaces($definition->interfaces(), $codebase);
42 29
        $this->resolveTraits($definition->traits(), $codebase);
43 29
        $this->resolveExternalParentClass($definition, $codebase);
44
    }
45
46 17
    private function resolveForEnum(EnumDefinition $definition, Codebase $codebase): void
47
    {
48 17
        $this->resolveInterfaces($definition->interfaces(), $codebase);
49 17
        $this->resolveTraits($definition->traits(), $codebase);
50
    }
51
52
    /** @param Name[] $interfaces */
53 31
    private function resolveInterfaces(array $interfaces, Codebase $codebase): void
54
    {
55 31
        array_map(static function (Name $interface) use ($codebase): void {
56 18
            if (! $codebase->has($interface)) {
57 2
                $codebase->add(new InterfaceDefinition($interface));
58
            }
59
        }, $interfaces);
60
    }
61
62
    /** @param Name[] $traits */
63 29
    private function resolveTraits(array $traits, Codebase $codebase): void
64
    {
65 29
        array_map(static function (Name $trait) use ($codebase): void {
66 15
            if (! $codebase->has($trait)) {
67 2
                $codebase->add(new TraitDefinition($trait));
68
            }
69
        }, $traits);
70
    }
71
72 29
    private function resolveExternalParentClass(ClassDefinition $definition, Codebase $codebase): void
73
    {
74 29
        if (! $definition->hasParent()) {
75 27
            return;
76
        }
77 14
        $parent = $definition->parent();
78 14
        if (! $codebase->has($parent)) {
79 12
            $codebase->add(new ClassDefinition($parent));
80
        }
81
    }
82
}
83