Skip to content

Commit

Permalink
Use class_alias to load php5 or php7 code
Browse files Browse the repository at this point in the history
UFOMelkor committed Jul 9, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b8a4673 commit 8289946
Showing 3 changed files with 58 additions and 44 deletions.
48 changes: 4 additions & 44 deletions src/Hal/Component/Ast/NodeTraverser.php
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ public function __construct(Mother $traverser, $stopCondition = null)
{
if(null === $stopCondition) {
$stopCondition = function($node) {
if($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
return false;
}

@@ -96,47 +96,7 @@ public function traverseArray(array $nodes, array $visitors) {
}

if (PHP_VERSION_ID >= 70000) {
class NodeTraverser extends Mother
{
/** @var Traverser */
private $traverser;

public function __construct($cloneNodes = false, $stopCondition = null)
{
parent::__construct();
$this->traverser = new Traverser($this, $stopCondition);
}

public function traverseNode(Node $node): Node
{
return parent::traverseNode($node);
}

protected function traverseArray(array $nodes): array
{
return $this->traverser->traverseArray($nodes, $this->visitors);
}
}
class_alias(Php7NodeTraverser::class, __NAMESPACE__ . '\\NodeTraverser');
} else {
class NodeTraverser extends Mother
{
/** @var Traverser */
private $traverser;

public function __construct($cloneNodes = false, $stopCondition = null)
{
parent::__construct();
$this->traverser = new Traverser($this, $stopCondition);
}

public function traverseNode(Node $node)
{
return parent::traverseNode($node);
}

protected function traverseArray(array $nodes)
{
return $this->traverser->traverseArray($nodes, $this->visitors);
}
}
}
class_alias(Php5NodeTraverser::class, __NAMESPACE__ . '\\NodeTraverser');
}
27 changes: 27 additions & 0 deletions src/Hal/Component/Ast/Php5NodeTraverser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Hal\Component\Ast;

use PhpParser\Node;
use PhpParser\NodeTraverser as Mother;

class Php5NodeTraverser extends Mother
{
/** @var Traverser */
private $traverser;

public function __construct($cloneNodes = false, $stopCondition = null)
{
parent::__construct();
$this->traverser = new Traverser($this, $stopCondition);
}

public function traverseNode(Node $node)
{
return parent::traverseNode($node);
}

protected function traverseArray(array $nodes)
{
return $this->traverser->traverseArray($nodes, $this->visitors);
}
}
27 changes: 27 additions & 0 deletions src/Hal/Component/Ast/Php7NodeTraverser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Hal\Component\Ast;

use PhpParser\Node;
use PhpParser\NodeTraverser as Mother;

class Php7NodeTraverser extends Mother
{
/** @var Traverser */
private $traverser;

public function __construct($cloneNodes = false, $stopCondition = null)
{
parent::__construct();
$this->traverser = new Traverser($this, $stopCondition);
}

public function traverseNode(Node $node): Node
{
return parent::traverseNode($node);
}

protected function traverseArray(array $nodes): array
{
return $this->traverser->traverseArray($nodes, $this->visitors);
}
}

0 comments on commit 8289946

Please sign in to comment.