Skip to content
This repository has been archived by the owner on Oct 24, 2018. It is now read-only.

Commit

Permalink
transpile code inside anonymous classes
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Mar 26, 2016
1 parent b280dbd commit 3fdb6de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getPhp5Code()
/**
* @return \PhpParser\NodeTraverser
*/
protected function getTraverser()
public static function getTraverser()
{
$traverser = new NodeTraverser();

Expand Down
17 changes: 17 additions & 0 deletions src/NodeVisitors/AnonymousClassReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\NodeVisitorAbstract;
use Spatie\Php7to5\Converter;
use Spatie\Php7to5\Exceptions\InvalidPhpCode;

class AnonymousClassReplacer extends NodeVisitorAbstract
Expand Down Expand Up @@ -57,6 +58,8 @@ public function afterTraverse(array $nodes)

$anonymousClassStatements = $this->anonymousClassNodes;

$anonymousClassStatements = $this->convertToPhp5Statements($anonymousClassStatements);

$hookIndex = $this->getAnonymousClassHookIndex($nodes);

$nodes = $this->moveAnonymousClassesToHook($nodes, $hookIndex, $anonymousClassStatements);
Expand Down Expand Up @@ -108,4 +111,18 @@ protected function moveAnonymousClassesToHook(array $nodes, $hookIndex, $anonymo

return array_merge($preStatements, $anonymousClassStatements, $postStatements);
}

/**
* @param array $php7statements
*
* @return \PhpParser\Node[]
*/
public function convertToPhp5Statements(array $php7statements)
{
$converter = Converter::getTraverser($php7statements);

$php5Statements = $converter->traverse($php7statements);

return $php5Statements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
use Import;
class AnonymousClass0
{
public function method()
public function method($parameter = '')
{
return true;
return isset($parameter) ? $parameter : 'no parameter set';
}
}
class AnonymousClass1
{
public function anotherMethod()
public function anotherMethod($integer)
{
return false;
return $integer;
}
}
class Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ class Test
public function test()
{
$class = new class() {
public function method() {
return true;
public function method(string $parameter = '') : string {
return $parameter ?? 'no parameter set';
}
};

$class->method();

$anotherClass = new class() {
public function anotherMethod() {
return false;
public function anotherMethod(int $integer) {
return $integer;
}
};
}
Expand Down

0 comments on commit 3fdb6de

Please sign in to comment.