Пример #1
0
 /**
  * Generates an identifier for the given file instance.
  *
  * @param PHP_Depend_Code_File $file The context source file instance.
  *
  * @return string
  */
 public function forFile(PHP_Depend_Code_File $file)
 {
     return $this->hash($file->getFileName());
 }
Пример #2
0
 /**
  * Visits a file node.
  *
  * @param PHP_Depend_Code_File $file The current file node.
  *
  * @return void
  * @see PHP_Depend_VisitorI::visitFile()
  */
 public function visitFile(PHP_Depend_Code_File $file)
 {
     $metricsXml = end($this->_xmlStack);
     $document = $metricsXml->ownerDocument;
     $xpath = new DOMXPath($document);
     $result = $xpath->query("/metrics/file[@name='{$file->getFileName()}']");
     // Only add a new file
     if ($result->length === 0) {
         // Create a new file element
         $fileXml = $document->createElement('file');
         // Set source file name
         $fileXml->setAttribute('name', $file->getFileName());
         // Append all metrics
         $this->_appendMetrics($fileXml, $file, $this->_additionalFileMetrics);
         // Append file to metrics xml
         $metricsXml->appendChild($fileXml);
         // Update project file counter
         ++$this->_files;
     } else {
         $fileXml = $result->item(0);
     }
     // Add file to stack
     array_push($this->_xmlStack, $fileXml);
 }
Пример #3
0
 /**
  * Visits a file node.
  *
  * @param PHP_Depend_Code_File $file The current file node.
  *
  * @return void
  * @see PHP_Depend_Visitor_AbstractVisitor::visitFile()
  */
 public function visitFile(PHP_Depend_Code_File $file)
 {
     // Skip for dummy files
     if ($file->getFileName() === null) {
         return;
     }
     // Check for initial file
     $uuid = $file->getUUID();
     if (isset($this->_nodeMetrics[$uuid])) {
         return;
     }
     $this->fireStartFile($file);
     list($cloc, $eloc, $lloc) = $this->_linesOfCode($file->getTokens());
     $loc = count($file->getLoc());
     $ncloc = $loc - $cloc;
     $this->_nodeMetrics[$uuid] = 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);
     // Update project metrics
     $this->_projectMetrics[self::M_LINES_OF_CODE] += $loc;
     $this->_projectMetrics[self::M_COMMENT_LINES_OF_CODE] += $cloc;
     $this->_projectMetrics[self::M_EXECUTABLE_LINES_OF_CODE] += $eloc;
     $this->_projectMetrics[self::M_LOGICAL_LINES_OF_CODE] += $lloc;
     $this->_projectMetrics[self::M_NON_COMMENT_LINES_OF_CODE] += $ncloc;
     $this->fireEndFile($file);
 }
Пример #4
0
 /**
  * Parse a statement.
  *
  * @return PHP_Depend_Code_ASTNode
  * @throws PHP_Depend_Parser_UnexpectedTokenException
  * @since 1.0.0
  */
 private function _parseStatement()
 {
     if (null === ($stmt = $this->_parseOptionalStatement())) {
         throw new PHP_Depend_Parser_UnexpectedTokenException($this->tokenizer->next(), $this->_sourceFile->getFileName());
     }
     return $stmt;
 }
Пример #5
0
 /**
  * Visits a file node.
  *
  * @param PHP_Depend_Code_File $file The current file node.
  *
  * @return void
  * @see PHP_Depend_Visitor_AbstractVisitor::visitFile()
  */
 public function visitFile(PHP_Depend_Code_File $file)
 {
     // Skip for dummy files
     if ($file->getFileName() === null) {
         return;
     }
     // Check for initial file
     $uuid = $file->getUuid();
     if (isset($this->metrics[$uuid])) {
         return;
     }
     $this->fireStartFile($file);
     if ($this->restoreFromCache($file)) {
         $this->updateProjectMetrics($uuid);
         return $this->fireEndFile($file);
     }
     list($cloc, $eloc, $lloc) = $this->linesOfCode($file->getTokens());
     $loc = $file->getEndLine();
     $ncloc = $loc - $cloc;
     $this->metrics[$uuid] = 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($uuid);
     $this->fireEndFile($file);
 }
Пример #6
0
 /**
  * testGetFileNameReturnsTheFileName
  *
  * @return void
  * @group pdepend
  * @group pdepend::code
  * @group unittest
  */
 public function testGetFileNameReturnsTheFileName()
 {
     $file = new PHP_Depend_Code_File(__FILE__);
     self::assertEquals(__FILE__, $file->getFileName());
 }
Пример #7
0
 /**
  * Parses the contents of the tokenizer and generates a node tree based on
  * the found tokens.
  *
  * @return void
  */
 public function parse()
 {
     // Get currently parsed source file
     $this->_sourceFile = $this->tokenizer->getSourceFile();
     $this->_sourceFile->setCache($this->cache)->setUUID($this->_uuidBuilder->forFile($this->_sourceFile));
     $hash = md5_file($this->_sourceFile->getFileName());
     if ($this->cache->restore($this->_sourceFile->getUUID(), $hash)) {
         return;
     }
     $this->cache->remove($this->_sourceFile->getUUID());
     $this->setUpEnvironment();
     $this->_tokenStack->push();
     // Debug currently parsed source file.
     PHP_Depend_Util_Log::debug('Processing file ' . $this->_sourceFile);
     $tokenType = $this->tokenizer->peek();
     while ($tokenType !== self::T_EOF) {
         switch ($tokenType) {
             case self::T_COMMENT:
                 $this->consumeToken(self::T_COMMENT);
                 break;
             case self::T_DOC_COMMENT:
                 $comment = $this->consumeToken(self::T_DOC_COMMENT)->image;
                 $this->_packageName = $this->_parsePackageAnnotation($comment);
                 $this->_docComment = $comment;
                 break;
             case self::T_INTERFACE:
                 $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName());
                 $package->addType($interface = $this->_parseInterfaceDeclaration());
                 $this->_builder->restoreInterface($interface);
                 $this->_sourceFile->addChild($interface);
                 break;
             case self::T_CLASS:
             case self::T_FINAL:
             case self::T_ABSTRACT:
                 $package = $this->_builder->buildPackage($this->_getNamespaceOrPackageName());
                 $package->addType($class = $this->_parseClassDeclaration());
                 $this->_builder->restoreClass($class);
                 $this->_sourceFile->addChild($class);
                 break;
             case self::T_FUNCTION:
                 $callable = $this->_parseFunctionOrClosureDeclaration();
                 $this->_sourceFile->addChild($callable);
                 break;
             case self::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 self::T_NAMESPACE:
                 $this->_parseNamespaceDeclaration();
                 break;
             default:
                 // Consume whatever token
                 $this->consumeToken($tokenType);
                 $this->reset();
                 break;
         }
         $tokenType = $this->tokenizer->peek();
     }
     $this->_sourceFile->setTokens($this->_tokenStack->pop());
     $this->cache->store($this->_sourceFile->getUUID(), $this->_sourceFile, $hash);
     $this->tearDownEnvironment();
 }