1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flagbit\Plantuml\TokenReflection; |
4
|
|
|
|
5
|
|
|
use TokenReflection\IReflectionMethod; |
6
|
|
|
use TokenReflection\IReflectionParameter; |
7
|
|
|
|
8
|
|
|
class MethodWriter extends \Flagbit\Plantuml\TokenReflection\WriterAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param \TokenReflection\IReflectionMethod $method |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
13 |
|
public function writeElement(IReflectionMethod $method) |
15
|
|
|
{ |
16
|
13 |
|
return $this->formatLine($this->writeVisibility($method) |
17
|
13 |
|
. ($method->isStatic() ? '{static}' : '') . $method->getName() |
18
|
13 |
|
. $this->writeParameters($method) . $this->writeReturnType($method)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param IReflectionMethod[] $methods |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
10 |
|
public function writeElements(array $methods) |
26
|
|
|
{ |
27
|
|
|
// see https://bugs.php.net/bug.php?id=50688 |
28
|
10 |
|
@usort($methods, function(IReflectionMethod $a, IReflectionMethod $b) { |
|
|
|
|
29
|
4 |
|
return strnatcasecmp($a->getName(), $b->getName()); |
30
|
10 |
|
}); |
31
|
|
|
|
32
|
10 |
|
$methodsString = ''; |
33
|
10 |
|
foreach ($methods as $method) { |
34
|
5 |
|
$methodsString .= $this->writeElement($method); |
35
|
10 |
|
} |
36
|
10 |
|
return $methodsString; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \TokenReflection\IReflectionMethod $method |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
13 |
|
private function writeVisibility(IReflectionMethod $method) |
44
|
|
|
{ |
45
|
13 |
|
return $method->isPrivate() ? '-' : ($method->isProtected() ? '#' : '+'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \TokenReflection\IReflectionMethod $method |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
13 |
|
private function writeParameters(IReflectionMethod $method) |
53
|
|
|
{ |
54
|
13 |
|
$parameters = array(); |
55
|
13 |
|
foreach ($method->getParameters() as $parameter) { |
56
|
|
|
/** @var $parameter \TokenReflection\IReflectionParameter */ |
57
|
5 |
|
$parameters[] = $this->writeParameter($method, $parameter); |
58
|
13 |
|
} |
59
|
13 |
|
return '(' . implode(', ' , $parameters) . ')'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param IReflectionMethod $method |
64
|
|
|
* @param IReflectionParameter $parameter |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
5 |
|
private function writeParameter(IReflectionMethod $method, IReflectionParameter $parameter) |
68
|
|
|
{ |
69
|
5 |
|
$parameterString = $parameter->getName(); |
70
|
|
|
|
71
|
5 |
|
if ($parameter->getClassName()) { |
72
|
1 |
|
$parameterString .= ' : ' . $this->formatClassName($parameter->getClassName()); |
73
|
1 |
|
} |
74
|
|
|
else { |
75
|
5 |
|
preg_match('/\*\h+@param\h+([^\h]+)\h+\$' . preg_quote($parameterString). '\s/', (string) $method->getDocComment(), $matches); |
76
|
5 |
|
if (isset($matches[1])) { |
77
|
2 |
|
$parameterString .= ' : ' . $this->formatClassName($matches[1]); |
78
|
2 |
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) { |
82
|
2 |
|
$parameterString .= ' = ' . $this->formatValue($parameter->getDefaultValue()); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
5 |
|
return $parameterString; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \TokenReflection\IReflectionMethod $method |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
13 |
View Code Duplication |
private function writeReturnType(IReflectionMethod $method) |
|
|
|
|
93
|
|
|
{ |
94
|
13 |
|
$returnType = ''; |
95
|
13 |
|
preg_match('/\*\h+@return\h+([^\h]+)/', (string) $method->getDocComment(), $matches); |
96
|
13 |
|
if (isset($matches[1])) { |
97
|
4 |
|
$returnType = $matches[1]; |
98
|
4 |
|
if ($method->getDeclaringClass()) { |
99
|
2 |
|
$returnType = $this->expandNamespaceAlias($method->getDeclaringClass(), $returnType); |
100
|
2 |
|
} |
101
|
4 |
|
$returnType = ' : ' . $this->formatClassName($returnType); |
102
|
4 |
|
} |
103
|
13 |
|
return $returnType; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: