Skip to content

Commit

Permalink
Factory: fix method bodies import [Closes #61] (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Zolotilin <[email protected]>
  • Loading branch information
2 people authored and dg committed Jun 19, 2020
1 parent fefc2fe commit 47aac1c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
54 changes: 48 additions & 6 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ private function loadMethodBodies(\ReflectionClass $from): array
foreach ($nodeFinder->findInstanceOf($class, Node\Stmt\ClassMethod::class) as $method) {
/** @var Node\Stmt\ClassMethod $method */
if ($method->stmts) {
$start = $method->stmts[0]->getAttribute('startFilePos');
$body = substr($code, $start, end($method->stmts)->getAttribute('endFilePos') - $start + 1);
$body = $this->extractBody($nodeFinder, $code, $method->stmts);
$bodies[$method->name->toString()] = Helpers::indentPhp($body, -2);
}
}
Expand All @@ -213,17 +212,60 @@ private function loadFunctionBody(\ReflectionFunction $from): string
}

[$code, $stmts] = $this->parse($from);

$nodeFinder = new PhpParser\NodeFinder;
/** @var Node\Stmt\Function_ $function */
$function = (new PhpParser\NodeFinder)->findFirst($stmts, function (Node $node) use ($from) {
$function = $nodeFinder->findFirst($stmts, function (Node $node) use ($from) {
return $node instanceof Node\Stmt\Function_ && $node->namespacedName->toString() === $from->name;
});

$start = $function->stmts[0]->getAttribute('startFilePos');
$body = substr($code, $start, end($function->stmts)->getAttribute('endFilePos') - $start + 1);
$body = $this->extractBody($nodeFinder, $code, $function->stmts);
return Helpers::indentPhp($body, -1);
}


/**
* @param Node[] $statements
*/
private function extractBody(PhpParser\NodeFinder $nodeFinder, string $originalCode, array $statements): string
{
$start = $statements[0]->getAttribute('startFilePos');
$body = substr($originalCode, $start, end($statements)->getAttribute('endFilePos') - $start + 1);

$replacements = [];
// name-nodes => resolved fully-qualified name
foreach ($nodeFinder->findInstanceOf($statements, Node\Name::class) as $node) {
if ($node->hasAttribute('resolvedName')
&& $node->getAttribute('resolvedName') instanceof Node\Name\FullyQualified
) {
$replacements[] = [
$node->getStartFilePos(),
$node->getEndFilePos(),
$node->getAttribute('resolvedName')->toCodeString(),
];
}
}

//sort collected resolved names by position in file
usort($replacements, function ($a, $b) {
return $a[0] <=> $b[0];
});
$correctiveOffset = -$start;
//replace changes body length so we need correct offset
foreach ($replacements as [$startPos, $endPos, $replacement]) {
$replacingStringLength = $endPos - $startPos + 1;
$body = substr_replace(
$body,
$replacement,
$correctiveOffset + $startPos,
$replacingStringLength
);
$correctiveOffset += strlen($replacement) - $replacingStringLength;
}
return $body;
}


private function parse($from): array
{
$file = $from->getFileName();
Expand All @@ -240,7 +282,7 @@ private function parse($from): array
$stmts = $parser->parse($code);

$traverser = new PhpParser\NodeTraverser;
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver(null, ['replaceNodes' => false]));
$stmts = $traverser->traverse($stmts);

return [$code, $stmts];
Expand Down
12 changes: 7 additions & 5 deletions tests/PhpGenerator/GlobalFunction.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require __DIR__ . '/../bootstrap.php';
/** global */
function func(stdClass $a, $b = null)
{
echo 'hello';
echo sprintf('hello, %s', 'world');
return 1;
}

Expand All @@ -28,13 +28,15 @@ function func(stdClass $a, $b = null)


$function = GlobalFunction::withBodyFrom('func');
same(
'/**
same(<<<'XX'
/**
* global
*/
function func(stdClass $a, $b = null)
{
echo \'hello\';
echo \sprintf('hello, %s', 'world');
return 1;
}
', (string) $function);

XX
, (string) $function);
10 changes: 5 additions & 5 deletions tests/PhpGenerator/expected/ClassType.from.bodies.expect
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ abstract class Class7

public function long()
{
if ($member instanceof Method) {
if ($member instanceof \Abc\Method) {
$s = [1, 2, 3];
}
/*
$this->methods[$member->getName()] = $member;
*/
throw new Nette\InvalidArgumentException('Argument must be Method|Property|Constant.');
throw new \Nette\InvalidArgumentException('Argument must be Method|Property|Constant.');
}


Expand All @@ -56,8 +56,8 @@ abstract class Class7
/** multi
line
comment */

if ($member instanceof Method) {
// Alias Method will not be resolved in comment
if ($member instanceof \Abc\Method) {
$s1 = '
a
b
Expand Down Expand Up @@ -87,6 +87,6 @@ a
c
<?php
}
throw new Nette\InvalidArgumentException();
throw new \Nette\InvalidArgumentException();
}
}
4 changes: 3 additions & 1 deletion tests/PhpGenerator/fixtures/class-body.phpf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare(strict_types=1);

namespace Abc;

use Nette;

abstract class Class7
{
abstract function abstractFun();
Expand Down Expand Up @@ -51,7 +53,7 @@ abstract class Class7
/** multi
line
comment */

// Alias Method will not be resolved in comment
if ($member instanceof Method) {
$s1 = '
a
Expand Down

0 comments on commit 47aac1c

Please sign in to comment.