/
MethodSymbol.php
159 lines (147 loc) · 5.1 KB
/
MethodSymbol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Concrete\Core\Support\Symbol\ClassSymbol\MethodSymbol;
use Concrete\Core\Support\Symbol\ClassSymbol\ClassSymbol;
class MethodSymbol
{
/**
* @var \ReflectionMethod
*/
protected $reflectionMethod;
/**
* @var ClassSymbol
*/
protected $classSymbol;
/**
* The method handle.
*
* @var string
*/
protected $handle;
/**
* The parameters.
*
* @var \ReflectionParameter[]
*/
protected $parameters = array();
/**
* The docblock.
*
* @var string
*/
protected $comment;
/**
* @param \ReflectionMethod $method
*/
public function __construct(ClassSymbol $class, \ReflectionMethod $method)
{
$this->classSymbol = $class;
$this->reflectionMethod = $method;
$this->parameters = $method->getParameters();
$this->handle = $method->getName();
}
/**
* Render the Method.
*
* @param string $eol
* @param string $padding
*
* @return string
*/
public function render($eol = "\n", $padding = ' ')
{
$method = $this->reflectionMethod;
if ($method->isPrivate() || substr($method->getName(), 0, 2) === '__' || $method->isAbstract()) {
return '';
}
$rendered = '';
$comment = $method->getDocComment();
if ($comment !== false) {
$comment = trim($comment);
if ($comment !== '') {
$rendered .= str_replace($eol . '*', $eol . ' *', implode($eol, array_map('trim', explode("\n", $comment)))) . $eol;
}
}
$visibility = \Reflection::getModifierNames($method->getModifiers());
$isStatic = $method->isStatic();
if ((!$isStatic) && $this->classSymbol->isFacade()) {
$visibility[] = 'static';
}
$rendered .= implode(' ', array_unique($visibility)) . ' function ' . $this->handle . '(';
$params = array();
$calling_params = array();
foreach ($this->parameters as $parameter) {
$param = '';
if ($parameter->isArray()) {
$param .= 'array ';
} /*else if ($parameter->isCallable()) { // This should be enabled for php 5.4
$param .= 'callable ';
} */ else {
try {
if (is_object($parameter->getClass())) {
$param .= $parameter->getClass()->getName() . ' ';
}
} catch (\ReflectionException $e) {
$class = $this->reflectionMethod->getDeclaringClass()->getName();
echo "Invalid type hint in {$class}::{$this->handle}\n";
}
}
if ($parameter->isPassedByReference()) {
$param .= "&";
}
$param .= '$' . $parameter->getName();
if ($parameter->isOptional()) {
$defaultValue = null;
if (method_exists($parameter, 'getDefaultValueConstantName')) {
$defaultValue = $parameter->getDefaultValueConstantName();
}
if ($defaultValue) {
// Strip out wrong namespaces.
if (preg_match('/.\\\\(\\w+)$/', $defaultValue, $matches) && defined($matches[1])) {
$defaultValue = $matches[1];
}
} else {
$v = $parameter->getDefaultValue();
switch (gettype($v)) {
case 'boolean':
case 'integer':
case 'double':
case 'NULL':
$defaultValue = json_encode($v);
break;
case 'string':
$defaultValue = '"' . addslashes($v) . '"';
break;
case 'array':
if (count($v)) {
$defaultValue = trim(var_export($v, true));
} else {
$defaultValue = 'array()';
}
break;
case 'object':
case 'resource':
default:
$defaultValue = trim(var_export($v, true));
break;
}
}
$param .= ' = ' . $defaultValue;
}
$params[] = $param;
$calling_params[] = '$' . $parameter->getName();
}
$rendered .= implode(', ', $params) . "){$eol}{{$eol}";
$class_name = $method->getDeclaringClass()->getName();
$rendered .= $padding . 'return ';
if ($isStatic) {
$rendered .= $class_name . '::' . $method->getName();
} elseif ($this->classSymbol->isFacade()) {
$rendered .= 'static::$instance->' . $method->getName();
} else {
$rendered .= 'parent::' . $method->getName();
}
$rendered .= '(' . implode(', ', $calling_params) . ');' . $eol;
$rendered .= '}' . $eol;
return $rendered;
}
}