Example #1
0
 /**
  * Sets a new php source file.
  *
  * @param string $sourceFile A php source file.
  *
  * @return void
  */
 public function setSourceFile($sourceFile)
 {
     $storage = PHP_Depend_StorageRegistry::get(PHP_Depend::PARSER_STORAGE);
     $id = '$Id$-@package_version@';
     $key = md5_file($sourceFile);
     $group = get_class($this->_tokenizer);
     $tokens = $storage->restore($key, $group, $id);
     if (is_array($tokens)) {
         $this->_sourceFile = new PHP_Depend_Code_File($sourceFile);
         $this->_sourceFile->setTokens($tokens);
     } else {
         $this->_tokenizer->setSourceFile($sourceFile);
         $this->_sourceFile = $this->_tokenizer->getSourceFile();
         $tokens = $this->_sourceFile->getTokens();
         $storage->store($tokens, $key, $group, $id);
     }
     $this->_tokens = $tokens;
     $this->_index = 0;
     $this->_count = count($tokens);
 }
Example #2
0
File: Xml.php Project: kingsj/core
 /**
  * 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);
 }
Example #3
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());
 }
Example #4
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);
 }
Example #5
0
 /**
  * Extracts the @package information from the given comment.
  *
  * @param string $comment A doc comment block.
  *
  * @return string
  */
 private function _parsePackageAnnotation($comment)
 {
     $package = self::DEFAULT_PACKAGE;
     if (preg_match('#\\*\\s*@package\\s+(\\S+)#', $comment, $match)) {
         $package = trim($match[1]);
         if (preg_match('#\\*\\s*@subpackage\\s+(\\S+)#', $comment, $match)) {
             $package .= self::PACKAGE_SEPARATOR . trim($match[1]);
         }
     }
     // Check for doc level comment
     if ($this->_globalPackageName === self::DEFAULT_PACKAGE && $this->isFileComment() === true) {
         $this->_globalPackageName = $package;
         $this->_sourceFile->setDocComment($comment);
     }
     return $package;
 }
Example #6
0
 /**
  * Sets the source file for this item.
  *
  * @param PHP_Depend_Code_File $sourceFile The item source file.
  *
  * @return void
  */
 public function setSourceFile(PHP_Depend_Code_File $sourceFile)
 {
     if ($this->sourceFile === null || $this->sourceFile->getName() === null) {
         $this->sourceFile = $sourceFile;
     }
 }
Example #7
0
 /**
  * Tokenizes the content of the source file with {@link token_get_all()} and
  * filters this token stream.
  *
  * @return void
  */
 private function _tokenize()
 {
     $this->tokens = array();
     $this->index = 0;
     $this->count = 0;
     // Replace short open tags, short open tags will produce invalid results
     // in all environments with disabled short open tags.
     $source = $this->sourceFile->getSource();
     $source = preg_replace(array('(<\\?=)', '(<\\?(\\s))'), array('<?php echo ', '<?php\\1'), $source);
     if (version_compare(phpversion(), '5.3.0alpha3') < 0) {
         $tokens = PHP_Depend_Tokenizer_PHP52Helper::tokenize($source);
     } else {
         $tokens = token_get_all($source);
     }
     $tokens = $this->_substituteTokens($tokens);
     // Is the current token between an opening and a closing php tag?
     $inTag = false;
     // The current line number
     $startLine = 1;
     $startColumn = 1;
     $endColumn = 1;
     $literalMap = self::$literalMap;
     $tokenMap = self::$tokenMap;
     // Previous found type
     $previousType = null;
     while ($token = current($tokens)) {
         $type = null;
         $image = null;
         if (is_string($token)) {
             $token = array(null, $token);
         }
         if ($token[0] === T_OPEN_TAG) {
             $type = $tokenMap[$token[0]];
             $image = $token[1];
             $inTag = true;
         } else {
             if ($token[0] === T_CLOSE_TAG) {
                 $type = $tokenMap[$token[0]];
                 $image = $token[1];
                 $inTag = false;
             } else {
                 if ($inTag === false) {
                     $type = self::T_NO_PHP;
                     $image = $this->_consumeNonePhpTokens($tokens);
                 } else {
                     if ($token[0] === T_WHITESPACE) {
                         // Count newlines in token
                         $lines = substr_count($token[1], "\n");
                         if ($lines === 0) {
                             $startColumn += strlen($token[1]);
                         } else {
                             $startColumn = strlen(substr($token[1], strrpos($token[1], "\n") + 1)) + 1;
                         }
                         $startLine += $lines;
                     } else {
                         $value = strtolower($token[1]);
                         if (isset($literalMap[$value])) {
                             // Fetch literal type
                             $type = $literalMap[$value];
                             // Check for a context sensitive alternative
                             if (isset(self::$alternativeMap[$type][$previousType])) {
                                 $type = self::$alternativeMap[$type][$previousType];
                             }
                             $image = $token[1];
                         } else {
                             if (isset($tokenMap[$token[0]])) {
                                 $type = $tokenMap[$token[0]];
                                 // Check for a context sensitive alternative
                                 if (isset(self::$alternativeMap[$type][$previousType])) {
                                     $type = self::$alternativeMap[$type][$previousType];
                                 }
                                 $image = $token[1];
                             } else {
                                 // This should never happen
                                 // @codeCoverageIgnoreStart
                                 list($type, $image) = $this->_generateUnknownToken($token[1]);
                                 // @codeCoverageIgnoreEnd
                             }
                         }
                     }
                 }
             }
         }
         if ($type) {
             $rtrim = rtrim($image);
             $lines = substr_count($rtrim, "\n");
             if ($lines === 0) {
                 $endColumn = $startColumn + strlen($rtrim) - 1;
             } else {
                 $endColumn = strlen(substr($rtrim, strrpos($rtrim, "\n") + 1));
             }
             $endLine = $startLine + $lines;
             $token = new PHP_Depend_Token($type, $rtrim, $startLine, $endLine, $startColumn, $endColumn);
             // Store token in internal list
             $this->tokens[] = $token;
             // Count newlines in token
             $lines = substr_count($image, "\n");
             if ($lines === 0) {
                 $startColumn += strlen($image);
             } else {
                 $startColumn = strlen(substr($image, strrpos($image, "\n") + 1)) + 1;
             }
             $startLine += $lines;
             // Store current type
             if ($type !== self::T_COMMENT && $type !== self::T_DOC_COMMENT) {
                 $previousType = $type;
             }
         }
         next($tokens);
     }
     $this->count = count($this->tokens);
 }
Example #8
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);
 }
Example #9
0
 /**
  * testBuilderCreatesCaseInSensitiveMethodIdentifiers
  *
  * @return void
  */
 public function testBuilderCreatesCaseInSensitiveMethodIdentifiers()
 {
     $file = new PHP_Depend_Code_File(__FILE__);
     $file->setUuid(__FUNCTION__);
     $class = new PHP_Depend_Code_Class(__FUNCTION__);
     $class->setSourceFile($file);
     $method0 = new PHP_Depend_Code_Method(__FUNCTION__);
     $method0->setParent($class);
     $method1 = new PHP_Depend_Code_Method(strtolower(__FUNCTION__));
     $method1->setParent($class);
     $builder0 = new PHP_Depend_Util_UuidBuilder();
     $builder1 = new PHP_Depend_Util_UuidBuilder();
     self::assertEquals($builder0->forMethod($method0), $builder1->forMethod($method1));
 }
Example #10
0
 /**
  * Tests the {@link PHP_Depend_Code_File#getSource()} method.
  *
  * @return void
  * @group pdepend
  * @group pdepend::code
  * @group unittest
  */
 public function testGetSourceReturnsOriginalFileContents()
 {
     $file = new PHP_Depend_Code_File(self::createCodeResourceUriForTest());
     $actual = $file->getSource();
     $expected = file_get_contents(self::createCodeResourceUriForTest());
     self::assertEquals($expected, $actual);
 }
Example #11
0
 /**
  * testBuilderCreatesExpectedIdentifierForSecondFunction
  *
  * @return void
  * @covers PHP_Depend_Util_UuidBuilder
  * @group pdepend
  * @group pdepend::util
  * @group unittest
  */
 public function testBuilderCreatesExpectedIdentifierForSecondFunction()
 {
     $file = new PHP_Depend_Code_File(__FILE__);
     $file->setUUID('FooBar');
     $function1 = new PHP_Depend_Code_Function(__FUNCTION__);
     $function1->setSourceFile($file);
     $function2 = new PHP_Depend_Code_Function(__CLASS__);
     $function2->setSourceFile($file);
     $builder = new PHP_Depend_Util_UuidBuilder();
     $builder->forFunction($function1);
     $this->assertRegExp('/^FooBar\\-[a-z0-9]{11}\\-00$/', $builder->forFunction($function2));
 }
Example #12
0
 /**
  * testAnalyzerIgnoresFilesWithoutFileName
  *
  * @return void
  */
 public function testAnalyzerIgnoresFilesWithoutFileName()
 {
     $file = new PHP_Depend_Code_File(null);
     $file->setUUID(42);
     $analyzer = $this->_createAnalyzer();
     $analyzer->visitFile($file);
     $metrics = $analyzer->getNodeMetrics($file);
     self::assertEquals(array(), $metrics);
 }
Example #13
0
 /**
  * Tests the {@link PHP_Depend_Code_File#getLoc()} method.
  *
  * @return void
  */
 public function testGetLoc()
 {
     $file = new PHP_Depend_Code_File(dirname(__FILE__) . '/../_code/mixed_code.php');
     $this->assertType('array', $file->getLoc());
     $this->assertEquals(62, count($file->getLoc()));
 }