/** * Visits a file node. * * @param \PDepend\Source\AST\ASTCompilationUnit $compilationUnit * @return void */ public function visitCompilationUnit(ASTCompilationUnit $compilationUnit) { // Skip for dummy files if ($compilationUnit->getFileName() === null) { return; } // Check for initial file $id = $compilationUnit->getId(); if (isset($this->metrics[$id])) { return; } $this->fireStartFile($compilationUnit); if ($this->restoreFromCache($compilationUnit)) { $this->updateProjectMetrics($id); return $this->fireEndFile($compilationUnit); } list($cloc, $eloc, $lloc) = $this->linesOfCode($compilationUnit->getTokens()); $loc = $compilationUnit->getEndLine(); $ncloc = $loc - $cloc; $this->metrics[$id] = array(self::M_LINES_OF_CODE => $loc, self::M_COMMENT_LINES_OF_CODE => $cloc, self::M_EXECUTABLE_LINES_OF_CODE => $eloc, self::M_LOGICAL_LINES_OF_CODE => $lloc, self::M_NON_COMMENT_LINES_OF_CODE => $ncloc); $this->updateProjectMetrics($id); $this->fireEndFile($compilationUnit); }
/** * Parses the contents of the tokenizer and generates a node tree based on * the found tokens. * * @return void */ public function parse() { $this->compilationUnit = $this->tokenizer->getSourceFile(); $this->compilationUnit->setCache($this->cache)->setId($this->idBuilder->forFile($this->compilationUnit)); $hash = md5_file($this->compilationUnit->getFileName()); if ($this->cache->restore($this->compilationUnit->getId(), $hash)) { return; } $this->cache->remove($this->compilationUnit->getId()); $this->setUpEnvironment(); $this->tokenStack->push(); Log::debug('Processing file ' . $this->compilationUnit); $tokenType = $this->tokenizer->peek(); while ($tokenType !== Tokenizer::T_EOF) { switch ($tokenType) { case Tokens::T_COMMENT: $this->consumeToken(Tokens::T_COMMENT); break; case Tokens::T_DOC_COMMENT: $comment = $this->consumeToken(Tokens::T_DOC_COMMENT)->image; $this->packageName = $this->parsePackageAnnotation($comment); $this->docComment = $comment; break; case Tokens::T_USE: // Parse a use statement. This method has no return value but it // creates a new entry in the symbol map. $this->parseUseDeclarations(); break; case Tokens::T_NAMESPACE: $this->parseNamespaceDeclaration(); break; case Tokens::T_NO_PHP: case Tokens::T_OPEN_TAG: case Tokens::T_OPEN_TAG_WITH_ECHO: $this->consumeToken($tokenType); $this->reset(); break; case Tokens::T_CLOSE_TAG: $this->parseNonePhpCode(); $this->reset(); break; default: if (null === $this->parseOptionalStatement()) { // Consume whatever token $this->consumeToken($tokenType); } break; } $tokenType = $this->tokenizer->peek(); } $this->compilationUnit->setTokens($this->tokenStack->pop()); $this->cache->store($this->compilationUnit->getId(), $this->compilationUnit, $hash); $this->tearDownEnvironment(); }
/** * testGetIdReturnsInjectedIdValue * * @return void */ public function testGetIdReturnsInjectedIdValue() { $compilationUnit = new ASTCompilationUnit(__FILE__); $compilationUnit->setId(__FUNCTION__); $this->assertEquals(__FUNCTION__, $compilationUnit->getId()); }