Skip to content

Commit

Permalink
Correctly handle namespaced classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 11, 2009
1 parent 0d0fb7d commit f1be496
Showing 1 changed file with 84 additions and 11 deletions.
95 changes: 84 additions & 11 deletions PHPLOC/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* @since File available since Release 1.0.0
*/

if (!defined('T_NAMESPACE')) {
define('T_NAMESPACE', 377);
}

/**
* PHPLOC code analyser.
*
Expand Down Expand Up @@ -162,6 +166,7 @@ public function preProcessFile($file)
{
$tokens = token_get_all(file_get_contents($file));
$numTokens = count($tokens);
$namespace = FALSE;

for ($i = 0; $i < $numTokens; $i++) {
if (is_string($tokens[$i])) {
Expand All @@ -170,17 +175,27 @@ public function preProcessFile($file)

list ($token, $value) = $tokens[$i];

if ($token == T_CLASS) {
$className = $tokens[$i+2][1];

if (isset($tokens[$i+4]) && is_array($tokens[$i+4]) &&
$tokens[$i+4][0] == T_EXTENDS) {
$parent = $tokens[$i+6][1];
} else {
$parent = NULL;
switch ($token) {
case T_NAMESPACE: {
$namespace = $this->getNamespaceName($tokens, $i);
}
break;

case T_CLASS: {
$className = $this->getClassName($namespace, $tokens, $i);

if (isset($tokens[$i+4]) && is_array($tokens[$i+4]) &&
$tokens[$i+4][0] == T_EXTENDS) {
$parent = $this->getClassName(
$namespace, $tokens, $i + 4
);
} else {
$parent = NULL;
}

$this->classes[$className] = $parent;
$this->classes[$className] = $parent;
}
break;
}
}
}
Expand All @@ -204,6 +219,7 @@ public function countFile($file, $countTests)
$cloc = 0;
$blocks = array();
$currentBlock = FALSE;
$namespace = FALSE;
$className = NULL;
$functionName = NULL;
$testClass = FALSE;
Expand Down Expand Up @@ -261,9 +277,16 @@ public function countFile($file, $countTests)
}

switch ($token) {
case T_NAMESPACE: {
$namespace = $this->getNamespaceName($tokens, $i);
}
break;

case T_CLASS:
case T_INTERFACE: {
$className = $tokens[$i+2][1];
$className = $this->getClassName(
$namespace, $tokens, $i
);
$currentBlock = T_CLASS;

if ($token == T_INTERFACE) {
Expand Down Expand Up @@ -427,6 +450,55 @@ protected function countEloc($filename)
return count($lines);
}

/**
* @param array $tokens
* @param integer $i
* @return string
* @since Method available since Release 1.3.0
*/
protected function getNamespaceName(array $tokens, $i)
{
$namespace = $tokens[$i+2][1];

for ($j = $i+3; ; $j += 2) {
if (isset($tokens[$j]) && $tokens[$j][0] == T_NS_SEPARATOR) {
$namespace .= '\\' . $tokens[$j+1][1];
} else {
break;
}
}

return $namespace;
}

/**
* @param string $namespace
* @param array $tokens
* @param integer $i
* @return string
* @since Method available since Release 1.3.0
*/
protected function getClassName($namespace, array $tokens, $i)
{
$i += 2;
$namespaced = FALSE;
$className = $tokens[$i][1];

if ($className === '\\') {
$namespaced = TRUE;
}

while ($tokens[$i+1][0] !== T_WHITESPACE) {
$className .= $tokens[++$i][1];
}

if (!$namespaced && $namespace !== FALSE) {
$className = $namespace . '\\' . $className;
}

return $className;
}

/**
* @param string $className
* @return boolean
Expand All @@ -439,7 +511,8 @@ protected function isTestClass($className)

// Check ancestry for PHPUnit_Framework_TestCase.
while ($parent !== NULL) {
if ($parent == 'PHPUnit_Framework_TestCase') {
if ($parent == 'PHPUnit_Framework_TestCase' ||
$parent == '\\PHPUnit_Framework_TestCase') {
$result = TRUE;
break;
}
Expand Down

0 comments on commit f1be496

Please sign in to comment.