Created
June 9, 2014 14:11
-
-
Save nikic/ffd019ef78b72934c7cc to your computer and use it in GitHub Desktop.
Script for finding variable-variable usages potentially affected by uniform variable syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PhpParser\Node; | |
use PhpParser\Node\Expr; | |
error_reporting(E_ALL); | |
ini_set('memory_limit', -1); | |
//$dir = __DIR__ . '/../../Symfony_2.3'; | |
$dir = __DIR__ . '/../../ZendFramework-2.0.2'; | |
require_once __DIR__ . '/../lib/bootstrap.php'; | |
class AnalyzingVisitor extends PhpParser\NodeVisitorAbstract { | |
private $file; | |
private $code; | |
public function setFile($file, $code) { | |
$this->file = realpath($file); | |
$this->code = $code; | |
} | |
public function printNodeContext(Node $node) { | |
$lineNo = $node->getLine(); | |
$lineCode = explode("\n", $this->code)[$lineNo-1]; | |
echo "$this->file @ $lineNo:\n$lineCode\n"; | |
} | |
} | |
class VarVarVisitor extends AnalyzingVisitor { | |
public function enterNode(Node $node) { | |
if (!$node instanceof Expr\Variable | |
&& !$node instanceof Expr\PropertyFetch && !$node instanceof Expr\StaticPropertyFetch | |
&& !$node instanceof Expr\MethodCall && !$node instanceof Expr\StaticCall | |
) { | |
return; | |
} | |
if ($node->name instanceof Expr\ArrayDimFetch) { | |
$this->printNodeContext($node); | |
} | |
} | |
} | |
$parser = new PhpParser\Parser(new PhpParser\Lexer); | |
$visitor = new VarVarVisitor; | |
$traverser = new PhpParser\NodeTraverser; | |
$traverser->addVisitor($visitor); | |
foreach (new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($dir), | |
RecursiveIteratorIterator::LEAVES_ONLY) | |
as $file | |
) { | |
if (!preg_match('/\.php$/', $file)) continue; | |
$code = file_get_contents($file); | |
$visitor->setFile($file, $code); | |
try { | |
$traverser->traverse($parser->parse(file_get_contents($file))); | |
} catch (PhpParser\Error $e) { | |
echo $file, "\n", $e->getMessage(), "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment