Closed DarkGhostHunter closed 4 years ago
Should have a proper way of returning the exact same body.
Your code is OK. But I think, it can be better, Try this:
This code is using "Strings" class from Nette Utils
protected function getMethodBody(\ReflectionMethod $from) : string
{
if (false === $from->getFileName()) {
return '';
}
$codeLines = file($from->getFileName());
$lineOfCode = array_slice($codeLines, $from->getStartLine() + 1, $from->getEndLine() - $from->getStartLine() - 2);
$unindentedLines = array_map(function ($lineOfCode) {
// Search for 2 tabs indent, then begin from a new line
if (Strings::startsWith($lineOfCode, "\t")) {
return Strings::substring($lineOfCode, strlen("\t\t"));
}
// preserve all other lines of code in body.
return $lineOfCode;
}, $lineOfCode);
return "\n".implode('', $unindentedLines);
}
This is duplicated to #4. Because content can't be extracted 100% correctly (for example in minified code or due to bad coding style), I don't want to try. So please add this functionality in your code.
Version: 3.3.4
Bug Description
Using an existing class won't register method bodies at all.
Steps To Reproduce
Create one class with a method body, and then parse it manually.
Expected Behavior
Possible Solution
Add this method to retrieve the body inside the
\PhpGenerator\Factory::fromMethodReflection
:From there, the only thing would be to "unpad" each line.