Passed
Push — master ( d46d5f...674e23 )
by Luis
44s queued 12s
created

ExternalDefinitionsResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveInterfaces() 0 7 2
A resolveForClass() 0 5 1
A resolveExternalParentClass() 0 8 3
A resolveTraits() 0 7 2
A resolve() 0 8 2
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.0
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Parser\Code;
9
10
use PhUml\Code\ClassDefinition;
11
use PhUml\Code\Codebase;
12
use PhUml\Code\InterfaceDefinition;
13
use PhUml\Code\Name;
14
use PhUml\Code\TraitDefinition;
15
16
/**
17
 * It checks the parent of a definition, the interfaces it implements, and the traits it uses
18
 * looking for external definitions
19
 *
20
 * An external definition is a class, trait or interface from a third party library, or a built-in class or interface
21
 */
22
final class ExternalDefinitionsResolver implements RelationshipsResolver
23
{
24
    public function resolve(Codebase $codebase): void
25
    {
26
        /** @var ClassDefinition|InterfaceDefinition|TraitDefinition $definition */
27
        foreach ($codebase->definitions() as $definition) {
28
            match (true) {
29
                $definition instanceof ClassDefinition => $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...
30
                $definition instanceof InterfaceDefinition => $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
                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
    private function resolveForClass(ClassDefinition $definition, Codebase $codebase): void
40
    {
41
        $this->resolveInterfaces($definition->interfaces(), $codebase);
42
        $this->resolveTraits($definition->traits(), $codebase);
43
        $this->resolveExternalParentClass($definition, $codebase);
44
    }
45
46
    /** @param Name[] $interfaces */
47
    private function resolveInterfaces(array $interfaces, Codebase $codebase): void
48
    {
49
        array_map(static function (Name $interface) use ($codebase): void {
50
            if (! $codebase->has($interface)) {
51
                $codebase->add(new InterfaceDefinition($interface));
52
            }
53
        }, $interfaces);
54
    }
55
56
    /** @param Name[] $traits */
57
    private function resolveTraits(array $traits, Codebase $codebase): void
58
    {
59
        array_map(static function (Name $trait) use ($codebase): void {
60
            if (! $codebase->has($trait)) {
61
                $codebase->add(new TraitDefinition($trait));
62
            }
63
        }, $traits);
64
    }
65
66
    private function resolveExternalParentClass(ClassDefinition $definition, Codebase $codebase): void
67
    {
68
        if (! $definition->hasParent()) {
69
            return;
70
        }
71
        $parent = $definition->parent();
72
        if (! $codebase->has($parent)) {
73
            $codebase->add(new ClassDefinition($parent));
74
        }
75
    }
76
}
77