|
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 | {
|
@@ -57,17 +57,26 @@ class Method extends Nette\Object
|
57 | 57 | */
|
58 | 58 | public static function from($from)
|
59 | 59 | {
|
60 |
| - $from = $from instanceof \ReflectionMethod ? $from : new \ReflectionMethod($from); |
| 60 | + if (is_string($from) && strpos($from, '::')) { |
| 61 | + $from = new \ReflectionMethod($from); |
| 62 | + } elseif (is_array($from)) { |
| 63 | + $from = new \ReflectionMethod($from[0], $from[1]); |
| 64 | + } elseif (!$from instanceof \ReflectionFunctionAbstract) { |
| 65 | + $from = new \ReflectionFunction($from); |
| 66 | + } |
| 67 | + |
61 | 68 | $method = new static;
|
62 |
| - $method->name = $from->getName(); |
| 69 | + $method->name = $from->isClosure() ? NULL : $from->getName(); |
63 | 70 | foreach ($from->getParameters() as $param) {
|
64 | 71 | $method->parameters[$param->getName()] = Parameter::from($param);
|
65 | 72 | }
|
66 |
| - $method->static = $from->isStatic(); |
67 |
| - $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); |
68 |
| - $method->final = $from->isFinal(); |
69 |
| - $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); |
70 |
| - $method->body = $from->isAbstract() ? FALSE : ''; |
| 73 | + if ($from instanceof \ReflectionMethod) { |
| 74 | + $method->static = $from->isStatic(); |
| 75 | + $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : NULL); |
| 76 | + $method->final = $from->isFinal(); |
| 77 | + $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); |
| 78 | + $method->body = $from->isAbstract() ? FALSE : ''; |
| 79 | + } |
71 | 80 | $method->returnReference = $from->returnsReference();
|
72 | 81 | $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic();
|
73 | 82 | $method->documents = $from->getDocComment() ? array(preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n\t"))) : array();
|
@@ -98,14 +107,15 @@ public function __toString()
|
98 | 107 | foreach ($this->uses as $param) {
|
99 | 108 | $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
|
100 | 109 | }
|
| 110 | + |
101 | 111 | return ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", $this->documents)) . "\n */\n" : '')
|
102 | 112 | . ($this->abstract ? 'abstract ' : '')
|
103 | 113 | . ($this->final ? 'final ' : '')
|
104 | 114 | . ($this->visibility ? $this->visibility . ' ' : '')
|
105 | 115 | . ($this->static ? 'static ' : '')
|
106 | 116 | . 'function'
|
107 | 117 | . ($this->returnReference ? ' &' : '')
|
108 |
| - . ($this->name ? ' ' . $this->name : '') |
| 118 | + . ' ' . $this->name |
109 | 119 | . '(' . implode(', ', $parameters) . ')'
|
110 | 120 | . ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
|
111 | 121 | . ($this->abstract || $this->body === FALSE ? ';'
|
|
0 commit comments