private function traverse($code)
 {
     $ast = ParseUtils::parse($code);
     $nodes =& $this->nodes;
     InOrderTraversal::traverseWithCallback($ast, function ($node) use(&$nodes) {
         $nodes[] = $node;
     });
 }
 private function updateAst()
 {
     try {
         $this->ast = ParseUtils::parse($this->getContent());
         NodeTraversal::traverseWithCallback($this->ast, $this->typeInferencePass, $this->typeInferencePass->getScopeCreator());
         NodeTraversal::traverseWithCallback($this->ast, $this->typeInferencePass, $this->typeInferencePass->getScopeCreator());
     } catch (\PHPParser_Error $ex) {
         // The original content of the file is not valid PHP code. We just ignore this.
         // Passes that require an AST, have to check with hasAst() manually.
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!is_file($file = $input->getArgument('file'))) {
         throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
     }
     if (false === strpos($input->getArgument('element'), '::')) {
         throw new \InvalidArgumentException(sprintf('The element must be of the form "ClassName::methodName", but got "%s".', $input->getArgument('element')));
     }
     list($class, $method) = explode('::', $input->getArgument('element'));
     $ast = ParseUtils::parse(file_get_contents($file));
     NodeTraversal::traverseWithCallback($ast, $callback = new SearchCallback($class, $method));
     if (null === ($cfg = $callback->getCfg())) {
         throw new \RuntimeException(sprintf('The code element "%s" was not found in the given file.', $input->getArgument('element')));
     }
     $output->writeln((new GraphvizSerializer())->serialize($cfg));
 }
 private function computeUseDef($code)
 {
     $code = sprintf('<?php function foo($param1, $param2) { %s }', $code);
     $ast = \JMS\PhpManipulator\PhpParser\ParseUtils::parse($code);
     $scope = (new \Scrutinizer\PhpAnalyzer\PhpParser\Scope\SyntacticScopeCreator())->createScope($ast);
     $cfa = new \Scrutinizer\PhpAnalyzer\ControlFlow\ControlFlowAnalysis();
     $cfa->process($ast);
     $cfg = $cfa->getGraph();
     $this->analysis = new \Scrutinizer\PhpAnalyzer\DataFlow\VariableReachability\MayBeReachingUseAnalysis($cfg, $scope);
     $this->analysis->analyze();
     $this->def = null;
     $this->uses = array();
     \Scrutinizer\PhpAnalyzer\PhpParser\NodeTraversal::traverseWithCallback($ast, new \Scrutinizer\PhpAnalyzer\PhpParser\PreOrderCallback(function ($t, \PHPParser_Node $node) {
         if (!$node instanceof \PHPParser_Node_Stmt_Label) {
             return;
         }
         if ('D' === $node->name) {
             $this->def = $node->getAttribute('next');
         } else {
             if (0 === strpos($node->name, 'U')) {
                 $this->uses[] = $node->getAttribute('next');
             }
         }
     }));
     $this->assertNotNull($this->def);
     $this->assertNotEmpty($this->uses);
 }
 public function setInput($code, \PHPParser_Node $ast = null)
 {
     $this->astStream->setAst($ast ?: PhpParser\ParseUtils::parse($code));
     $this->tokenStream->setCode($code);
     $lastNode = null;
     while ($this->moveNext()) {
         if ($lastNode !== $this->node) {
             $this->node->setAttribute('start_token', $this->token);
             if (null !== $lastNode) {
                 $lastNode->setAttribute('end_token', $this->token->getPreviousToken()->get());
             }
         }
         $lastNode = $this->node;
     }
     $this->reset();
 }
 private function computeDefUse($code)
 {
     $code = sprintf('<?php function foo($param1, $param2) { %s }', $code);
     $ast = ParseUtils::parse($code);
     $scope = (new SyntacticScopeCreator())->createScope($ast);
     $cfa = new ControlFlowAnalysis();
     $cfa->process($ast);
     $cfg = $cfa->getGraph();
     $this->analysis = new MustBeReachingDefAnalysis($cfg, $scope);
     $this->analysis->analyze();
     $this->def = $this->use = null;
     NodeTraversal::traverseWithCallback($ast, new PreOrderCallback(function (NodeTraversal $t, \PHPParser_Node $node) {
         if (!$node instanceof \PHPParser_Node_Stmt_Label) {
             return;
         }
         if ('D' === $node->name) {
             $this->def = $node->getAttribute('next');
         } else {
             if ('U' === $node->name) {
                 $this->use = $node->getAttribute('next');
             } else {
                 if (0 === strpos($node->name, 'DP_')) {
                     $className = 'PHPParser_Node_' . substr($node->name, 3);
                     $parent = $node;
                     while (null !== ($parent = $parent->getAttribute('parent'))) {
                         if ($parent instanceof $className) {
                             $this->def = $parent;
                             return;
                         }
                     }
                     throw new \RuntimeException(sprintf('Did not find any parent of class "%s".', $className));
                 }
             }
         }
     }));
     $this->assertNotNull($this->def, 'Did not find "D" (definition) label in the code.');
     $this->assertNotNull($this->use, 'Did not find "U" (usage) label in the code.');
 }