private function load() { $traverser = new Traverser(); $declarations = new Visitor\DeclarationFinder(); $calls = new Visitor\CallFinder(); $variables = new Visitor\VariableFinder(); $traverser->addVisitor($declarations); $traverser->addVisitor($calls); $traverser->addVisitor($variables); for ($i = 0; $i < count($this->blocks); $i++) { $this->blocks[$i] = $traverser->traverse($this->blocks[$i]); } $this->variables = $variables->getVariables(); $this->constants = $declarations->getConstants(); $this->traits = $declarations->getTraits(); $this->classes = $declarations->getClasses(); $this->interfaces = $declarations->getInterfaces(); $this->methods = $declarations->getMethods(); $this->functions = $declarations->getFunctions(); $this->functionLookup = $this->buildFunctionLookup($declarations->getFunctions()); $this->callFinder = $calls; $this->methodCalls = $this->findMethodCalls(); $this->newCalls = $this->findNewCalls(); $this->computeTypeMatrix(); }
/** @dataProvider provideTestParseAndDump */ public function testParseAndDump($name, $code, $expectedDump) { $astTraverser = new PhpParser\NodeTraverser(); $astTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver()); $parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $astTraverser); $block = $parser->parse($code, 'foo.php'); $traverser = new Traverser(); $traverser->addVisitor(new Visitor\Simplifier()); $traverser->traverse($block); $printer = new Printer\Text(); $this->assertEquals($this->canonicalize($expectedDump), $this->canonicalize($printer->printCfg([$block]))); }
/** @dataProvider provideTestParseAndDump */ public function testParseAndDump($code, $expectedDump) { $astTraverser = new PhpParser\NodeTraverser(); $astTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver()); $parser = new Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $astTraverser); $traverser = new Traverser(); $traverser->addVisitor(new Visitor\Simplifier()); $printer = new Printer\Text(); try { $script = $parser->parse($code, 'foo.php'); $traverser->traverse($script); $result = $printer->printScript($script); } catch (\RuntimeException $e) { $result = $e->getMessage(); } $this->assertEquals($this->canonicalize($expectedDump), $this->canonicalize($result)); }
protected function getGraphsFromFiles(array $files, array $exclude, CFGParser $parser) { $excludeParts = []; foreach ($exclude as $part) { $excludeParts[] = preg_quote($part); } $part = implode('|', $excludeParts); $excludeRegex = "(((\\.({$part})(\$|/))|((^|/)({$part})(\$|/))))"; $graphs = []; $traverser = new Traverser(); $traverser->addVisitor(new Visitor\Simplifier()); foreach ($files as $file) { if (is_file($file)) { $local = [$file]; } elseif (is_dir($file)) { $it = new \CallbackFilterIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($file)), function (\SplFileInfo $file) use($excludeRegex) { if (preg_match($excludeRegex, $file->getPathName())) { return false; } return $file->isFile(); }); $local = []; foreach ($it as $file) { $local[] = $file->getPathName(); // since __toString would be too difficult... } } else { throw new \RuntimeException("Error: {$file} is not a file or directory"); } foreach ($local as $file) { echo "Analyzing {$file}\n"; $graphs[$file] = $parser->parse(file_get_contents($file), $file); $traverser->traverse($graphs[$file]); } } return $graphs; }
protected function preProcess(array $blocks) { $traverser = new Traverser(); $declarations = new Visitor\DeclarationFinder(); $calls = new Visitor\CallFinder(); $variables = new Visitor\VariableFinder(); $dagComputer = new Visitor\VariableDagComputer(); $traverser->addVisitor(new Visitor\Simplifier()); $traverser->addVisitor($dagComputer); $traverser->addVisitor($declarations); $traverser->addVisitor($calls); $traverser->addVisitor($variables); foreach ($blocks as $block) { $traverser->traverse($block); } return ["cfg" => $blocks, "constants" => $declarations->getConstants(), "traits" => $declarations->getTraits(), "classes" => $declarations->getClasses(), "methods" => $declarations->getMethods(), "functions" => $declarations->getFunctions(), "functionLookup" => $this->buildFunctionLookup($declarations->getFunctions()), "interfaces" => $declarations->getInterfaces(), "variables" => $variables->getVariables(), "callResolver" => $calls, "methodCalls" => $this->findMethodCalls($blocks), "internalTypeInfo" => new InternalArgInfo()]; }