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