Skip to content

Commit

Permalink
Load php7 code in php7 only
Browse files Browse the repository at this point in the history
Fixes #360
  • Loading branch information
UFOMelkor committed Jul 9, 2018
1 parent a74f2fc commit 8dda876
Show file tree
Hide file tree
Showing 4 changed files with 71 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
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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');
}
26 changes: 26 additions & 0 deletions src/Hal/Component/Ast/Php5NodeTraverser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Hal\Component\Ast;

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);
}
}
14 changes: 14 additions & 0 deletions tests/Component/Ast/NodeTraverserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Test\Hal\Component\Ast;

use Hal\Component\Ast\NodeTraverser;
use PhpParser\NodeTraverser as BaseTraverser;
use PHPUnit_Framework_TestCase;

class NodeTraverserTest extends PHPUnit_Framework_TestCase
{
public function testItCanBeInstantiated()
{
$this->assertInstanceOf(BaseTraverser::class, new NodeTraverser());
}
}

0 comments on commit 8dda876

Please sign in to comment.