/** * @param string $filepath * @param Parser $parser * @param Context $context */ protected function parserFile($filepath, Parser $parser, Context $context) { $context->setFilepath($filepath); $astTraverser = new \PhpParser\NodeTraverser(); $astTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver()); try { if (!is_readable($filepath)) { throw new RuntimeException('File ' . $filepath . ' is not readable'); } $context->debug('<comment>Precompile: ' . $filepath . '.</comment>'); $code = file_get_contents($filepath); $astTree = $parser->parse($code); $astTraverser->traverse($astTree); $aliasManager = new AliasManager(); $namespace = null; /** * Step 1 Precompile */ foreach ($astTree as $topStatement) { if ($topStatement instanceof Node\Stmt\Namespace_) { /** * Namespace block can be created without NS name */ if ($topStatement->name) { $namespace = $topStatement->name->toString(); $aliasManager->setNamespace($namespace); } if ($topStatement->stmts) { $this->parseTopDefinitions($topStatement->stmts, $aliasManager, $filepath); } } else { $this->parseTopDefinitions($topStatement, $aliasManager, $filepath); } } /** * Another Traverser to handler Analyzer Passe(s) */ $analyzeTraverser = new AstTraverser([new \PHPSA\Node\Visitor\FunctionCall()], $context); $analyzeTraverser->traverse($astTree); $context->clear(); } catch (\PhpParser\Error $e) { $context->sytaxError($e, $filepath); } catch (Exception $e) { $context->output->writeln("<error>{$e->getMessage()}</error>"); } }
/** * @covers \PHPSA\AliasManager::getNamespace * @covers \PHPSA\AliasManager::setNamespace * @depends testConstructorWithNamespace */ public function testNamespaceGetterSetter() { $manager = new AliasManager('\\Some\\Initial\\Namespace'); $this->assertNull($manager->setNamespace('\\New\\GoneWithTheOld')); $this->assertEquals('\\New\\GoneWithTheOld', $manager->getNamespace()); }