parse() public method

public parse ( string $code, string $fileName )
$code string
$fileName string
Esempio n. 1
0
 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 = [];
     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);
         }
     }
     return $graphs;
 }
Esempio n. 2
0
 /** @dataProvider provideTestParseAndDump */
 public function testParseAndDump($code, $expectedDump)
 {
     $astTraverser = new PhpParser\NodeTraverser();
     $astTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver());
     $parser = new Parser(new PhpParser\Parser(new PhpParser\Lexer()), $astTraverser);
     $dumper = new Dumper();
     $block = $parser->parse($code, 'foo.php');
     $this->assertEquals($this->canonicalize($expectedDump), $this->canonicalize($dumper->dump($block)));
 }
Esempio n. 3
0
 /** @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])));
 }
Esempio n. 4
0
 /** @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));
 }