|
11 | 11 |
|
12 | 12 |
|
13 | 13 | /**
|
14 |
| - * Class method description. |
| 14 | + * Method or function description. |
15 | 15 | */
|
16 | 16 | class Method extends Nette\Object
|
17 | 17 | {
|
@@ -60,17 +60,26 @@ class Method extends Nette\Object
|
60 | 60 | */
|
61 | 61 | public static function from($from)
|
62 | 62 | {
|
63 |
| - $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from); |
| 63 | + if (is_string($from) && strpos($from, '::')) { |
| 64 | + $from = new \ReflectionMethod($from); |
| 65 | + } elseif (is_array($from)) { |
| 66 | + $from = new \ReflectionMethod($from[0], $from[1]); |
| 67 | + } elseif (!$from instanceof \ReflectionFunctionAbstract) { |
| 68 | + $from = new \ReflectionFunction($from); |
| 69 | + } |
| 70 | + |
64 | 71 | $method = new static;
|
65 |
| - $method->name = $from->getName(); |
| 72 | + $method->name = $from->isClosure() ? NULL : $from->getName(); |
66 | 73 | foreach ($from->getParameters() as $param) {
|
67 | 74 | $method->parameters[$param->getName()] = Parameter::from($param);
|
68 | 75 | }
|
69 |
| - $method->static = $from->isStatic(); |
70 |
| - $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); |
71 |
| - $method->final = $from->isFinal(); |
72 |
| - $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); |
73 |
| - $method->body = $from->isAbstract() ? FALSE : ''; |
| 76 | + if ($from instanceof \ReflectionMethod) { |
| 77 | + $method->static = $from->isStatic(); |
| 78 | + $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); |
| 79 | + $method->final = $from->isFinal(); |
| 80 | + $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); |
| 81 | + $method->body = $from->isAbstract() ? FALSE : ''; |
| 82 | + } |
74 | 83 | $method->returnReference = $from->returnsReference();
|
75 | 84 | $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
|
76 | 85 | $method->documents = $from->getDocComment() ? [preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))] : [];
|
@@ -108,14 +117,15 @@ public function __toString()
|
108 | 117 | $returnType = !$this->namespace || in_array($this->returnType, $builtinTypes, TRUE)
|
109 | 118 | ? $this->returnType
|
110 | 119 | : $this->namespace->unresolveName($this->returnType);
|
| 120 | + |
111 | 121 | return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '')
|
112 | 122 | . ($this->abstract ? 'abstract ' : '')
|
113 | 123 | . ($this->final ? 'final ' : '')
|
114 | 124 | . ($this->visibility ? $this->visibility . ' ' : '')
|
115 | 125 | . ($this->static ? 'static ' : '')
|
116 | 126 | . 'function'
|
117 | 127 | . ($this->returnReference ? ' &' : '')
|
118 |
| - . ($this->name ? ' ' . $this->name : '') |
| 128 | + . ' ' . $this->name |
119 | 129 | . '(' . implode(', ', $parameters) . ')'
|
120 | 130 | . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
|
121 | 131 | . ($returnType ? ': ' . $returnType : '')
|
|
0 commit comments