diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index d570cadf..9e6e74fd 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -87,7 +87,12 @@ public static function from($from) $method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ''); $method->final = $from->isFinal(); $method->abstract = $from->isAbstract() && !$from->getDeclaringClass()->isInterface(); - $method->body = $from->isAbstract() ? FALSE : ''; + if ($method->abstract || ($m = Nette\Utils\Strings::match(implode('', array_slice(file($from->getFileName()), $startline = $from->getStartLine() - 1, $from->getEndLine() - $startline)), '#[^{]*\{(.*)\}[^\}]*\z#s')) === NULL) { + $method->body = FALSE; + } else { + list (, $whitespace) = Nette\Utils\Strings::match($m[1], '#\n([\t ]*)#'); + $method->body = Nette\Utils\Strings::replace($m[1], '#(\n)' . preg_quote($whitespace) . '#', '$1'); + } $method->returnReference = $from->returnsReference(); $method->variadic = PHP_VERSION_ID >= 50600 && $from->isVariadic(); $method->documents = preg_replace('#^\s*\* ?#m', '', trim($from->getDocComment(), "/* \r\n")); diff --git a/tests/PhpGenerator/ClassType.inheritance.expect b/tests/PhpGenerator/ClassType.inheritance.expect index b35bc9e3..42ab2d21 100644 --- a/tests/PhpGenerator/ClassType.inheritance.expect +++ b/tests/PhpGenerator/ClassType.inheritance.expect @@ -10,7 +10,7 @@ class B extends A function bar() { - + return 3; } } diff --git a/tests/PhpGenerator/Method.body.expect b/tests/PhpGenerator/Method.body.expect new file mode 100644 index 00000000..d2e1d097 --- /dev/null +++ b/tests/PhpGenerator/Method.body.expect @@ -0,0 +1,21 @@ +class A +{ + + function foo() + { + echo 'foo'; + + foreach (range(1, 2) as $no) { + if (TRUE === FALSE) { + echo $class->{'get' . \Nette\Utils\Random::generate(244, "foo{$rand}bar")}(); + } + + echo $no; + } + + return function () use ($no) { + return 3 * 5; + }; + } + +} diff --git a/tests/PhpGenerator/Method.body.phpt b/tests/PhpGenerator/Method.body.phpt new file mode 100644 index 00000000..9cc7bf82 --- /dev/null +++ b/tests/PhpGenerator/Method.body.phpt @@ -0,0 +1,34 @@ +{'get' . \Nette\Utils\Random::generate(244, "foo{$rand}bar")}(); + } + + echo $no; + } + + return function () use ($no) { + return 3 * 5; + }; + } + +} + + +Assert::matchFile(__DIR__ . '/Method.body.expect', (string) ClassType::from('A'));