|
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
|
4 |
|
public function hasMethod(ClassReflection $classReflection, string $methodName) : bool |
|
49
|
|
|
{ |
|
50
|
4 |
|
if (!array_key_exists($classReflection->getName(), static::SUPPORTED_CLASSES)) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
$methods = $this->getMethodsFromSpec(static::SUPPORTED_CLASSES[$classReflection->getName()]); |
|
55
|
4 |
|
$expectedAttribute = $this->getAttributeFromMethodName($methodName); |
|
56
|
4 |
|
return in_array($expectedAttribute, $methods, true); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function getMethod(ClassReflection $classReflection, string $methodName) : MethodReflection |
|
60
|
|
|
{ |
|
61
|
2 |
|
if (stripos($methodName, 'get') === 0) { |
|
62
|
|
|
return new AttributeGetterMethodReflection($classReflection, $methodName); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$attributeName = $this->getAttributeFromMethodName($methodName); |
|
66
|
|
|
|
|
67
|
2 |
|
return new AttributeSetterMethodReflection( |
|
68
|
2 |
|
$classReflection, |
|
69
|
2 |
|
$methodName, |
|
70
|
2 |
|
$this->getAttributeInputType($attributeName) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string[] |
|
76
|
|
|
*/ |
|
77
|
4 |
|
private function getMethodsFromSpec(string $className) : array |
|
78
|
|
|
{ |
|
79
|
4 |
|
$simpleXml = $this->getAttributesXmlDoc(); |
|
80
|
|
|
|
|
81
|
4 |
|
$elements = $simpleXml->xpath(sprintf("xsd:complexType[@name='%s']/xsd:attribute", $className)); |
|
82
|
|
|
|
|
83
|
4 |
|
if ($elements === false) { |
|
84
|
|
|
throw new InvalidArgumentException( |
|
85
|
|
|
sprintf('Class "%s" does not exist in Graphviz spec', $className) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
return array_map( |
|
90
|
|
|
static function (SimpleXMLElement $attribute) : string { |
|
91
|
4 |
|
return strtolower((string) $attribute['ref']); |
|
92
|
4 |
|
}, |
|
93
|
4 |
|
$elements |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
private function getAttributeInputType(string $ref) : Type |
|
98
|
|
|
{ |
|
99
|
2 |
|
$simpleXml = $this->getAttributesXmlDoc(); |
|
100
|
2 |
|
$attributes = $simpleXml->xpath(sprintf("xsd:attribute[@name='%s']", $ref)); |
|
101
|
|
|
|
|
102
|
2 |
|
if (empty($attributes)) { |
|
103
|
|
|
return new StringType(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
$type = $attributes[0]['type']; |
|
107
|
2 |
|
$type = str_replace('xsd:', '', $type); |
|
108
|
2 |
|
switch ($type) { |
|
109
|
2 |
|
case 'boolean': |
|
110
|
|
|
return new BooleanType(); |
|
111
|
2 |
|
case 'decimal': |
|
112
|
1 |
|
return new FloatType(); |
|
113
|
1 |
|
case 'string': |
|
114
|
|
|
default: |
|
115
|
1 |
|
return new StringType(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
private function getAttributesXmlDoc() : SimpleXMLElement |
|
120
|
|
|
{ |
|
121
|
6 |
|
$fileContent = file_get_contents(__DIR__ . '/assets/attributes.xml'); |
|
122
|
|
|
|
|
123
|
6 |
|
if ($fileContent === false) { |
|
124
|
|
|
throw new RuntimeException('Cannot read attributes spec'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
6 |
|
$xml = simplexml_load_string($fileContent); |
|
128
|
6 |
|
if ($xml === false) { |
|
129
|
|
|
throw new RuntimeException('Cannot read attributes spec'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
return $xml; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
6 |
|
private function getAttributeFromMethodName(string $methodName) : string |
|
136
|
|
|
{ |
|
137
|
6 |
|
return strtolower(substr($methodName, 3)); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|