Completed
Push — master ( 3f1b14...d7a449 )
by Luis
14:49 queued 04:53
created

Digraph   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 62
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toDotLanguage() 0 7 1
A elementsToDotLanguage() 0 7 1
A __construct() 0 7 1
A fromCodeStructure() 0 7 4
A classElementsFrom() 0 3 1
A graphId() 0 3 1
A interfaceElementsFrom() 0 3 1
1
<?php
2
/**
3
 * PHP version 7.1
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\Graphviz;
9
10
use PhUml\Code\ClassDefinition;
11
use PhUml\Code\InterfaceDefinition;
12
13
class Digraph implements HasDotRepresentation
14
{
15
    /** @var HasDotRepresentation[] */
16
    private $dotElements;
17
18
    /** @var InterfaceGraphElements */
19
    private $interfaceElements;
20
21
    /** @var ClassGraphElements */
22
    private $classElements;
23
24
    public function __construct(
25
        InterfaceGraphElements $interfaceElements,
26
        ClassGraphElements $classElements
27
    ) {
28
        $this->dotElements = [];
29
        $this->interfaceElements = $interfaceElements;
30
        $this->classElements = $classElements;
31
    }
32
33
    public function fromCodeStructure(array $structure): void
34
    {
35
        foreach ($structure as $definition) {
36
            if ($definition instanceof ClassDefinition) {
37
                $this->classElementsFrom($definition, $structure);
38
            } else if ($definition instanceof InterfaceDefinition) {
39
                $this->interfaceElementsFrom($definition);
40
            }
41
        }
42
    }
43
44
    private function classElementsFrom(ClassDefinition $class, array $structure): void
45
    {
46
        $this->dotElements = array_merge($this->dotElements, $this->classElements->extractFrom($class, $structure));
47
    }
48
49
    private function interfaceElementsFrom(InterfaceDefinition $interface): void
50
    {
51
        $this->dotElements = array_merge($this->dotElements, $this->interfaceElements->extractFrom($interface));
52
    }
53
54
    public function toDotLanguage(): string
55
    {
56
        return "digraph \"{$this->graphId()}\" {
57
splines = true;
58
overlap = false;
59
mindist = 0.6;
60
{$this->elementsToDotLanguage()}}";
61
    }
62
63
    private function elementsToDotLanguage(): string
64
    {
65
        $dotFormat = array_map(function (HasDotRepresentation $element) {
66
            return $element->toDotLanguage();
67
        }, $this->dotElements);
68
69
        return implode('', $dotFormat);
70
    }
71
72
    private function graphId(): string
73
    {
74
        return sha1(mt_rand());
0 ignored issues
show
Bug introduced by
The call to mt_rand() has too few arguments starting with min. ( Ignorable by Annotation )

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

74
        return sha1(/** @scrutinizer ignore-call */ mt_rand());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
    }
76
}
77