1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* phpDocumentor |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\GraphViz\PHPStan; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use phpDocumentor\GraphViz\Edge; |
18
|
|
|
use phpDocumentor\GraphViz\Graph; |
19
|
|
|
use phpDocumentor\GraphViz\Node; |
20
|
|
|
use PHPStan\Reflection\ClassReflection; |
21
|
|
|
use PHPStan\Reflection\MethodReflection; |
22
|
|
|
use PHPStan\Reflection\MethodsClassReflectionExtension; |
23
|
|
|
use PHPStan\Type\BooleanType; |
24
|
|
|
use PHPStan\Type\FloatType; |
25
|
|
|
use PHPStan\Type\StringType; |
26
|
|
|
use PHPStan\Type\Type; |
27
|
|
|
use RuntimeException; |
28
|
|
|
use SimpleXMLElement; |
29
|
|
|
use function array_key_exists; |
30
|
|
|
use function array_map; |
31
|
|
|
use function file_get_contents; |
32
|
|
|
use function in_array; |
33
|
|
|
use function simplexml_load_string; |
34
|
|
|
use function sprintf; |
35
|
|
|
use function str_replace; |
36
|
|
|
use function stripos; |
37
|
|
|
use function strtolower; |
38
|
|
|
use function substr; |
39
|
|
|
|
40
|
|
|
final class MethodReflectionExtension implements MethodsClassReflectionExtension |
41
|
|
|
{ |
42
|
|
|
private const SUPPORTED_CLASSES = [ |
43
|
|
|
Node::class => 'node', |
44
|
|
|
Graph::class => 'graph', |
45
|
|
|
Edge::class => 'edge', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
public function hasMethod(ClassReflection $classReflection, string $methodName) : bool |
49
|
|
|
{ |
50
|
|
|
if (!array_key_exists($classReflection->getName(), static::SUPPORTED_CLASSES)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$methods = $this->getMethodsFromSpec(static::SUPPORTED_CLASSES[$classReflection->getName()]); |
55
|
|
|
$expectedAttribute = $this->getAttributeFromMethodName($methodName); |
56
|
|
|
return in_array($expectedAttribute, $methods, true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getMethod(ClassReflection $classReflection, string $methodName) : MethodReflection |
60
|
|
|
{ |
61
|
|
|
if (stripos($methodName, 'get') === 0) { |
62
|
|
|
return new AttributeGetterMethodReflection($classReflection, $methodName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$attributeName = $this->getAttributeFromMethodName($methodName); |
66
|
|
|
|
67
|
|
|
return new AttributeSetterMethodReflection( |
68
|
|
|
$classReflection, |
69
|
|
|
$methodName, |
70
|
|
|
$this->getAttributeInputType($attributeName) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string[] |
76
|
|
|
*/ |
77
|
|
|
private function getMethodsFromSpec(string $className) : array |
78
|
|
|
{ |
79
|
|
|
$simpleXml = $this->getAttributesXmlDoc(); |
80
|
|
|
|
81
|
|
|
$elements = $simpleXml->xpath(sprintf("xsd:complexType[@name='%s']/xsd:attribute", $className)); |
82
|
|
|
|
83
|
|
|
if ($elements === false) { |
84
|
|
|
throw new InvalidArgumentException( |
85
|
|
|
sprintf('Class "%s" does not exist in Graphviz spec', $className) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return array_map( |
90
|
|
|
static function (SimpleXMLElement $attribute) : string { |
91
|
|
|
return strtolower((string) $attribute['ref']); |
92
|
|
|
}, |
93
|
|
|
$elements |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getAttributeInputType(string $ref) : Type |
98
|
|
|
{ |
99
|
|
|
$simpleXml = $this->getAttributesXmlDoc(); |
100
|
|
|
$attributes = $simpleXml->xpath(sprintf("xsd:attribute[@name='%s']", $ref)); |
101
|
|
|
|
102
|
|
|
if (empty($attributes)) { |
103
|
|
|
return new StringType(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$type = $attributes[0]['type']; |
107
|
|
|
$type = str_replace('xsd:', '', $type); |
108
|
|
|
switch ($type) { |
109
|
|
|
case 'boolean': |
110
|
|
|
return new BooleanType(); |
111
|
|
|
case 'decimal': |
112
|
|
|
return new FloatType(); |
113
|
|
|
case 'string': |
114
|
|
|
default: |
115
|
|
|
return new StringType(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getAttributesXmlDoc() : SimpleXMLElement |
120
|
|
|
{ |
121
|
|
|
$fileContent = file_get_contents(__DIR__ . '/assets/attributes.xml'); |
122
|
|
|
|
123
|
|
|
if ($fileContent === false) { |
124
|
|
|
throw new RuntimeException('Cannot read attributes spec'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$xml = simplexml_load_string($fileContent); |
128
|
|
|
if ($xml === false) { |
129
|
|
|
throw new RuntimeException('Cannot read attributes spec'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $xml; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function getAttributeFromMethodName(string $methodName) : string |
136
|
|
|
{ |
137
|
|
|
return strtolower(substr($methodName, 3)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|