public function enterScope(NodeTraversal $traversal) { $typeInference = new TypeInference($traversal->getControlFlowGraph(), $this->reverseInterpreter, $this->functionInterpreter, $this->methodInterpreter, $this->commentParser, $traversal->getScope(), $this->registry, $this->logger); try { $typeInference->analyze(); } catch (MaxIterationsExceededException $ex) { $scopeRoot = $traversal->getScopeRoot(); $this->logger->warning($ex->getMessage() . ' - Scope-Root: ' . get_class($scopeRoot) . ' on line ' . $scopeRoot->getLine() . ' in ' . $this->file->getName()); } }
private function inMethod($phpCode) { // Parse the body of the function. $ast = $this->parser->parse(new \PHPParser_Lexer('<?php class Foo { function foo() {' . $phpCode . '} }')); // Normalize the AST. $traverser = new \PHPParser_NodeTraverser(); $traverser->addVisitor(new \PHPParser_NodeVisitor_NameResolver()); $traverser->addvisitor(new NormalizingNodeVisitor()); $ast = $traverser->traverse($ast); $traverser = new \PHPParser_NodeTraverser(); $traverser->addVisitor(new \PHPParser_NodeVisitor_NodeConnector()); $traverser->traverse($ast); $root = $ast[0]; $node = $root->stmts[0]->stmts; // Create the scope with the assumptions. $scopeCreator = new TypedScopeCreator($this->registry); $assumedScope = $scopeCreator->createScope($node, $scopeCreator->createScope($root, null)); foreach ($this->assumptions as $symbolName => $type) { $var = $assumedScope->getVar($symbolName); if (!$var) { $assumedScope->declareVar($symbolName, $type); } else { $var->setType($type); } } // Create the control graph. $cfa = new ControlFlowAnalysis(); $cfa->process($node); $cfg = $cfa->getGraph(); // Create a simple reverse abstract interpreter. $rai = new SemanticReverseAbstractInterpreter($this->registry); $fi = new ArrayFunctionInterpreter($this->registry); $mi = $this->getMock('Scrutinizer\\PhpAnalyzer\\DataFlow\\TypeInference\\MethodInterpreter\\MethodInterpreterInterface'); $commentParser = new \Scrutinizer\PhpAnalyzer\PhpParser\DocCommentParser($this->registry); // Do the type inference by data-flow analysis. $dfa = new TypeInference($cfg, $rai, $fi, $mi, $commentParser, $assumedScope, $this->registry); $dfa->analyze(); // Get the scope of the implicit return. $this->returnScope = $cfg->getImplicitReturn()->getAttribute(DataFlowAnalysis::ATTR_FLOW_STATE_IN); $this->astGraph = \Scrutinizer\PhpAnalyzer\PhpParser\NodeUtil::dump($node); }