Skip to content

Commit

Permalink
Improve current function, class recording
Browse files Browse the repository at this point in the history
  • Loading branch information
monque committed Aug 26, 2016
1 parent a1f842e commit e67ab5b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/Changes/v5dot3/IncompCallFromGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function enterNode($node)
// Populate
if ($node instanceof Expr\FuncCall &&
$this->funcTable->has($node->name) &&
!$this->visitor->inMethod() &&
!$this->visitor->inFunction()) {
$this->emitSpot($node);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Changes/v7dot0/Deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function leaveNode($node)
*
* @see http://php.net/manual/en/migration70.deprecated.php#migration70.deprecated.php4-constructors
*/
if ($node instanceof Stmt\ClassMethod && $this->visitor->inClass()) {
if ($node->name == $this->visitor->getClassname()) {
if ($node instanceof Stmt\ClassMethod) {
if ($node->name == $this->visitor->getClassName()) {
$this->addSpot('DEPRECATED', true, 'PHP 4 style constructor is deprecated');
}

Expand Down
59 changes: 28 additions & 31 deletions src/CheckVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;

class CheckVisitor extends NodeVisitorAbstract
Expand All @@ -36,24 +37,29 @@ class CheckVisitor extends NodeVisitorAbstract
protected $code;

/**
* Current class, interface, trait
* Current node
*/
protected $node;

/**
* Current class-like node (class, interface, trait)
*/
protected $class;

/**
* Current method in class
* Stack for record current class-like node
*/
protected $method;
protected $classStack;

/**
* Current function
* Current funciton-like node (function, method, closure)
*/
protected $function;

/**
* Current node
* Stack for record current function-like node
*/
protected $node;
protected $funcStack;

/**
* Empty spots, current state and save the Changes
Expand All @@ -75,7 +81,7 @@ public function callChange($name, $method, $args)
}

foreach ($this->changes as $change) {
if ($name == substr(get_class($change), -strlen($name))) {
if ('PhpMigration\Changes\\'.$name == get_class($change)) {
return call_user_func_array(array($change, $method), $args);
}
}
Expand Down Expand Up @@ -106,26 +112,16 @@ public function getClass()
return $this->class;
}

public function getClassname()
public function getClassName()
{
return is_null($this->class) ? null : $this->class->name;
return is_null($this->class) ? null : $this->class->migName;
}

public function inClass()
{
return !is_null($this->class);
}

public function getMethod()
{
return $this->method;
}

public function inMethod()
{
return !is_null($this->method);
}

public function getFunction()
{
return $this->function;
Expand All @@ -146,6 +142,8 @@ public function prepare()

public function beforeTraverse(array $nodes)
{
$this->classStack = $this->funcStack = array();

foreach ($this->changes as $change) {
$change->beforeTraverse($nodes);
}
Expand All @@ -158,15 +156,16 @@ public function enterNode(Node $node)
// Record current
if ($node instanceof Stmt\ClassLike) {
/**
* Class, Interface, Trait are stored in one same HashTable (zend_executor_globals.class_table).
* Their name will be conflict if duplicated (eg, class Demo {} and Interface Demo {})
* So, we treat all these class-like's name as Class name.
* Class, Interface, Trait are stored in one same HashTable
* (zend_executor_globals.class_table). Their name will be conflict
* if duplicated (eg, class Demo {} and Interface Demo {}). So, we
* treat all these class-like's name as Class name.
*/
$this->class = $node;
} elseif ($node instanceof Stmt\ClassMethod) {
$this->method = $node;
} elseif ($node instanceof Stmt\Function_) {
$this->classStack[] = $node;
} elseif ($node instanceof FunctionLike) {
$this->function = $node;
$this->funcStack[] = $node;
}

foreach ($this->changes as $change) {
Expand All @@ -184,13 +183,11 @@ public function leaveNode(Node $node)
$change->leaveNode($node);
}

// Clear current
// Pop current stack
if ($node instanceof Stmt\ClassLike) {
$this->class = null;
} elseif ($node instanceof Stmt\ClassMethod) {
$this->method = null;
} elseif ($node instanceof Stmt\Function_) {
$this->function = null;
$this->class = array_pop($this->classStack);
} elseif ($node instanceof FunctionLike) {
$this->function = array_pop($this->funcStack);
}

$this->node = null;
Expand Down

0 comments on commit e67ab5b

Please sign in to comment.