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
|
|
|
class plDigraph implements plHasDotRepresentation |
9
|
|
|
{ |
10
|
|
|
/** @var plHasDotRepresentation[] */ |
11
|
|
|
private $dotElements; |
12
|
|
|
|
13
|
|
|
/** @var plInterfaceGraphElements */ |
14
|
|
|
private $interfaceElements; |
15
|
|
|
|
16
|
|
|
/** @var plClassGraphElements */ |
17
|
|
|
private $classElements; |
18
|
|
|
|
19
|
|
|
public function __construct( |
20
|
|
|
plInterfaceGraphElements $interfaceElements, |
21
|
|
|
plClassGraphElements $classElements |
22
|
|
|
) { |
23
|
|
|
$this->dotElements = []; |
24
|
|
|
$this->interfaceElements = $interfaceElements; |
25
|
|
|
$this->classElements = $classElements; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function fromCodeStructure(array $structure): void |
29
|
|
|
{ |
30
|
|
|
foreach ($structure as $definition) { |
31
|
|
|
if ($definition instanceof plPhpClass) { |
32
|
|
|
$this->classElementsFrom($definition, $structure); |
33
|
|
|
} else if ($definition instanceof plPhpInterface) { |
34
|
|
|
$this->interfaceElementsFrom($definition); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function classElementsFrom(plPhpClass $class, array $structure): void |
40
|
|
|
{ |
41
|
|
|
$this->dotElements = array_merge($this->dotElements, $this->classElements->extractFrom($class, $structure)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function interfaceElementsFrom(plPhpInterface $interface): void |
45
|
|
|
{ |
46
|
|
|
$this->dotElements = array_merge($this->dotElements, $this->interfaceElements->extractFrom($interface)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function toDotLanguage(): string |
50
|
|
|
{ |
51
|
|
|
return "digraph \"{$this->graphId()}\" { |
52
|
|
|
splines = true; |
53
|
|
|
overlap = false; |
54
|
|
|
mindist = 0.6; |
55
|
|
|
{$this->elementsToDotLanguage()}}"; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function elementsToDotLanguage(): string |
59
|
|
|
{ |
60
|
|
|
$dotFormat = array_map(function (plHasDotRepresentation $element) { |
61
|
|
|
return $element->toDotLanguage(); |
62
|
|
|
}, $this->dotElements); |
63
|
|
|
|
64
|
|
|
return implode('', $dotFormat); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function graphId(): string |
68
|
|
|
{ |
69
|
|
|
return sha1(mt_rand()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.