GraphvizProcessor::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Processors;
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\TraitDefinition;
13
use PhUml\Graphviz\Builders\ClassGraphBuilder;
14
use PhUml\Graphviz\Builders\EnumGraphBuilder;
15
use PhUml\Graphviz\Builders\InterfaceGraphBuilder;
16
use PhUml\Graphviz\Builders\TraitGraphBuilder;
17
use PhUml\Graphviz\Digraph;
18
use PhUml\Graphviz\DigraphPrinter;
19
use PhUml\Templates\TemplateEngine;
20
21
/**
22
 * It creates a digraph from a `Codebase` and returns it as a string in DOT format
23
 */
24
final class GraphvizProcessor implements Processor
25
{
26 18
    public static function fromConfiguration(GraphvizConfiguration $configuration): GraphvizProcessor
27
    {
28 18
        $style = $configuration->digraphStyle();
29 18
        $associationsBuilder = $configuration->edgesBuilder();
30
31 18
        return new GraphvizProcessor(
32 18
            new ClassGraphBuilder($associationsBuilder),
33 18
            new InterfaceGraphBuilder(),
34 18
            new TraitGraphBuilder(),
35 18
            new EnumGraphBuilder(),
36 18
            new DigraphPrinter(new TemplateEngine(), $style)
37
        );
38
    }
39
40 18
    private function __construct(
41
        private readonly ClassGraphBuilder $classBuilder,
42
        private readonly InterfaceGraphBuilder $interfaceBuilder,
43
        private readonly TraitGraphBuilder $traitBuilder,
44
        private readonly EnumGraphBuilder $enumBuilder,
45
        private readonly DigraphPrinter $printer
46
    ) {
47
    }
48
49 11
    public function name(): string
50
    {
51 11
        return 'Graphviz';
52
    }
53
54 13
    public function process(Codebase $codebase): OutputContent
55
    {
56 13
        $digraph = new Digraph();
57
        /** @var ClassDefinition|InterfaceDefinition|TraitDefinition|EnumDefinition $definition */
58 13
        foreach ($codebase->definitions() as $definition) {
59 11
            $this->extractElements($definition, $codebase, $digraph);
60
        }
61 13
        return new OutputContent($this->printer->toDot($digraph));
62
    }
63
64 11
    private function extractElements(
65
        ClassDefinition|InterfaceDefinition|TraitDefinition|EnumDefinition $definition,
66
        Codebase $codebase,
67
        Digraph $digraph
68
    ): void {
69 11
        match ($definition::class) {
70 11
            ClassDefinition::class => $digraph->add($this->classBuilder->extractFrom($definition, $codebase)),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $digraph->add($this->cla...definition, $codebase)) targeting PhUml\Graphviz\Digraph::add() 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...
Bug introduced by
It seems like $definition can also be of type PhUml\Code\EnumDefinition and PhUml\Code\InterfaceDefinition and PhUml\Code\TraitDefinition; however, parameter $class of PhUml\Graphviz\Builders\...hBuilder::extractFrom() does only seem to accept PhUml\Code\ClassDefinition, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            ClassDefinition::class => $digraph->add($this->classBuilder->extractFrom(/** @scrutinizer ignore-type */ $definition, $codebase)),
Loading history...
71 11
            InterfaceDefinition::class => $digraph->add($this->interfaceBuilder->extractFrom($definition, $codebase)),
0 ignored issues
show
Bug introduced by
It seems like $definition can also be of type PhUml\Code\ClassDefinition and PhUml\Code\EnumDefinition and PhUml\Code\TraitDefinition; however, parameter $interface of PhUml\Graphviz\Builders\...hBuilder::extractFrom() does only seem to accept PhUml\Code\InterfaceDefinition, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
            InterfaceDefinition::class => $digraph->add($this->interfaceBuilder->extractFrom(/** @scrutinizer ignore-type */ $definition, $codebase)),
Loading history...
Bug introduced by
Are you sure the usage of $digraph->add($this->int...definition, $codebase)) targeting PhUml\Graphviz\Digraph::add() 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...
72 11
            TraitDefinition::class => $digraph->add($this->traitBuilder->extractFrom($definition, $codebase)),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $digraph->add($this->tra...definition, $codebase)) targeting PhUml\Graphviz\Digraph::add() 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...
Bug introduced by
It seems like $definition can also be of type PhUml\Code\ClassDefinition and PhUml\Code\EnumDefinition and PhUml\Code\InterfaceDefinition; however, parameter $trait of PhUml\Graphviz\Builders\...hBuilder::extractFrom() does only seem to accept PhUml\Code\TraitDefinition, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            TraitDefinition::class => $digraph->add($this->traitBuilder->extractFrom(/** @scrutinizer ignore-type */ $definition, $codebase)),
Loading history...
73 11
            default => $digraph->add($this->enumBuilder->extractFrom($definition, $codebase)),
0 ignored issues
show
Bug introduced by
It seems like $definition can also be of type PhUml\Code\ClassDefinition and PhUml\Code\InterfaceDefinition and PhUml\Code\TraitDefinition; however, parameter $enum of PhUml\Graphviz\Builders\...hBuilder::extractFrom() does only seem to accept PhUml\Code\EnumDefinition, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            default => $digraph->add($this->enumBuilder->extractFrom(/** @scrutinizer ignore-type */ $definition, $codebase)),
Loading history...
Bug introduced by
Are you sure the usage of $digraph->add($this->enu...definition, $codebase)) targeting PhUml\Graphviz\Digraph::add() 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...
74
        };
75
    }
76
}
77