Skip to content

Commit

Permalink
- Merge [964].
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 6, 2007
1 parent 8fe0658 commit de22c3e
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 115 deletions.
64 changes: 57 additions & 7 deletions PHPUnit/Util/Class.php
Expand Up @@ -64,6 +64,7 @@ class PHPUnit_Util_Class
{
protected static $buffer = array();
protected static $fileClassMap = array();
protected static $fileFunctionMap = array();

/**
* Starts the collection of loaded classes.
Expand Down Expand Up @@ -143,10 +144,9 @@ public static function getClassesInFile($filename, $commonPath = '', $clearCache

if (empty(self::$fileClassMap)) {
$classes = array_merge(get_declared_classes(), get_declared_interfaces());
$count = count($classes);

for ($i = 0; $i < $count; $i++) {
$class = new ReflectionClass($classes[$i]);

foreach ($classes as $className) {
$class = new ReflectionClass($className);

if ($class->isUserDefined()) {
$file = $class->getFileName();
Expand All @@ -167,6 +167,51 @@ public static function getClassesInFile($filename, $commonPath = '', $clearCache
return isset(self::$fileClassMap[$filename]) ? self::$fileClassMap[$filename] : array();
}

/**
* Returns the names of the classes declared in a sourcefile.
*
* @param string $filename
* @param string $commonPath
* @param boolean $clearCache
* @return array
* @access public
* @static
* @since Class available since Release 3.2.0
* @todo Find a better place for this method.
*/
public static function getFunctionsInFile($filename, $commonPath = '', $clearCache = FALSE)
{
if ($commonPath != '') {
$filename = str_replace($commonPath, '', $filename);
}

if ($clearCache) {
self::$fileFunctionMap = array();
}

if (empty(self::$fileFunctionMap)) {
$functions = get_defined_functions();

foreach ($functions['user'] as $functionName) {
$function = new ReflectionFunction($functionName);

$file = $function->getFileName();

if ($commonPath != '') {
$file = str_replace($commonPath, '', $file);
}

if (!isset(self::$fileFunctionMap[$file])) {
self::$fileFunctionMap[$file] = array($function);
} else {
self::$fileFunctionMap[$file][] = $function;
}
}
}

return isset(self::$fileFunctionMap[$filename]) ? self::$fileFunctionMap[$filename] : array();
}

/**
* Returns the class hierarchy for a given class.
*
Expand Down Expand Up @@ -205,11 +250,16 @@ public static function getHierarchy($className)
*/
public static function getMethodSource($className, $methodName)
{
$method = new ReflectionMethod($className, $methodName);
$file = file($method->getFileName());
if ($className != 'global') {
$function = new ReflectionMethod($className, $methodName);
} else {
$function = new ReflectionFunction($methodName);
}

$file = file($function->getFileName());
$result = '';

for ($line = $method->getStartLine() - 1; $line <= $method->getEndLine() - 1; $line++) {
for ($line = $function->getStartLine() - 1; $line <= $function->getEndLine() - 1; $line++) {
$result .= $file[$line];
}

Expand Down
218 changes: 133 additions & 85 deletions PHPUnit/Util/Log/CodeCoverage/Database.php
Expand Up @@ -237,39 +237,100 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $runId,
$stmt->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmt->execute();

foreach ($fileMetrics->getClasses() as $classMetrics) {
$className = $classMetrics->getClass()->getName();
$stmtSelectFunctionId = $this->dbh->prepare(
'SELECT code_function_id
FROM code_file, code_function
WHERE code_function.code_file_id = code_file.code_file_id
AND code_file.revision = :revision
AND code_function.code_function_name = :functionName;'
);

$stmt = $this->dbh->prepare(
'SELECT code_class_id
FROM code_file, code_class
WHERE code_class.code_file_id = code_file.code_file_id
AND code_file.revision = :revision
AND code_class.code_class_name = :className;'
);
$stmtInsertFunction = $this->dbh->prepare(
'INSERT INTO metrics_function
(run_id, code_function_id, metrics_function_coverage,
metrics_function_loc, metrics_function_loc_executable, metrics_function_loc_executed,
metrics_function_ccn, metrics_function_crap)
VALUES(:runId, :functionId, :coverage, :loc,
:locExecutable, :locExecuted, :ccn, :crap);'
);

$stmt->bindParam(':className', $className, PDO::PARAM_STR);
$stmt->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmt->execute();
$stmtSelectClassId = $this->dbh->prepare(
'SELECT code_class_id
FROM code_file, code_class
WHERE code_class.code_file_id = code_file.code_file_id
AND code_file.revision = :revision
AND code_class.code_class_name = :className;'
);

$classId = (int)$stmt->fetchColumn();
$stmtInsertClass = $this->dbh->prepare(
'INSERT INTO metrics_class
(run_id, code_class_id, metrics_class_coverage,
metrics_class_loc, metrics_class_loc_executable, metrics_class_loc_executed,
metrics_class_aif, metrics_class_ahf,
metrics_class_cis, metrics_class_csz, metrics_class_dit,
metrics_class_impl, metrics_class_mif, metrics_class_mhf,
metrics_class_noc, metrics_class_pf, metrics_class_vars,
metrics_class_varsnp, metrics_class_varsi,
metrics_class_wmc, metrics_class_wmcnp, metrics_class_wmci)
VALUES(:runId, :classId, :coverage, :loc, :locExecutable,
:locExecuted, :aif, :ahf, :cis, :csz, :dit, :impl,
:mif, :mhf, :noc, :pf, :vars, :varsnp, :varsi,
:wmc, :wmcnp, :wmci);'
);

$stmt = $this->dbh->prepare(
'INSERT INTO metrics_class
(run_id, code_class_id, metrics_class_coverage,
metrics_class_loc, metrics_class_loc_executable, metrics_class_loc_executed,
metrics_class_aif, metrics_class_ahf,
metrics_class_cis, metrics_class_csz, metrics_class_dit,
metrics_class_impl, metrics_class_mif, metrics_class_mhf,
metrics_class_noc, metrics_class_pf, metrics_class_vars,
metrics_class_varsnp, metrics_class_varsi,
metrics_class_wmc, metrics_class_wmcnp, metrics_class_wmci)
VALUES(:runId, :classId, :coverage, :loc, :locExecutable,
:locExecuted, :aif, :ahf, :cis, :csz, :dit, :impl,
:mif, :mhf, :noc, :pf, :vars, :varsnp, :varsi,
:wmc, :wmcnp, :wmci);'
);
$stmtSelectMethodId = $this->dbh->prepare(
'SELECT code_method_id
FROM code_file, code_class, code_method
WHERE code_class.code_file_id = code_file.code_file_id
AND code_class.code_class_id = code_method.code_class_id
AND code_file.revision = :revision
AND code_class.code_class_name = :className
AND code_method.code_method_name = :methodName;'
);

$stmtInsertMethod = $this->dbh->prepare(
'INSERT INTO metrics_method
(run_id, code_method_id, metrics_method_coverage,
metrics_method_loc, metrics_method_loc_executable, metrics_method_loc_executed,
metrics_method_ccn, metrics_method_crap)
VALUES(:runId, :methodId, :coverage, :loc,
:locExecutable, :locExecuted, :ccn, :crap);'
);

foreach ($fileMetrics->getFunctions() as $functionMetrics) {
$functionName = $functionMetrics->getFunction()->getName();

$stmtSelectFunctionId->bindParam(':functionName', $functionName, PDO::PARAM_STR);
$stmtSelectFunctionId->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmtSelectFunctionId->execute();

$functionId = (int)$stmtSelectFunctionId->fetchColumn();
$coverage = $functionMetrics->getCoverage();
$loc = $functionMetrics->getLoc();
$locExecutable = $functionMetrics->getLocExecutable();
$locExecuted = $functionMetrics->getLocExecuted();
$ccn = $functionMetrics->getCCN();
$crap = $functionMetrics->getCrapIndex();

$stmtInsertFunction->bindParam(':runId', $runId, PDO::PARAM_INT);
$stmtInsertFunction->bindParam(':functionId', $functionId, PDO::PARAM_INT);
$stmtInsertFunction->bindParam(':coverage', $coverage);
$stmtInsertFunction->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmtInsertFunction->bindParam(':locExecutable', $locExecutable, PDO::PARAM_INT);
$stmtInsertFunction->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmtInsertFunction->bindParam(':ccn', $ccn);
$stmtInsertFunction->bindParam(':crap', $crap);
$stmtInsertFunction->execute();
}

foreach ($fileMetrics->getClasses() as $classMetrics) {
$className = $classMetrics->getClass()->getName();

$stmtSelectClassId->bindParam(':className', $className, PDO::PARAM_STR);
$stmtSelectClassId->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmtSelectClassId->execute();

$classId = (int)$stmtSelectClassId->fetchColumn();
$coverage = $classMetrics->getCoverage();
$loc = $classMetrics->getLoc();
$locExecutable = $classMetrics->getLocExecutable();
Expand All @@ -291,78 +352,65 @@ public function storeCodeCoverage(PHPUnit_Framework_TestResult $result, $runId,
$wmcnp = $classMetrics->getWMCnp();
$wmci = $classMetrics->getWMCi();

$stmt->bindParam(':runId', $runId, PDO::PARAM_INT);
$stmt->bindParam(':classId', $classId, PDO::PARAM_INT);
$stmt->bindParam(':coverage', $coverage);
$stmt->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmt->bindParam(':locExecutable', $locExecutable, PDO::PARAM_INT);
$stmt->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmt->bindParam(':aif', $aif);
$stmt->bindParam(':ahf', $ahf);
$stmt->bindParam(':cis', $cis, PDO::PARAM_INT);
$stmt->bindParam(':csz', $csz, PDO::PARAM_INT);
$stmt->bindParam(':dit', $dit, PDO::PARAM_INT);
$stmt->bindParam(':impl', $impl, PDO::PARAM_INT);
$stmt->bindParam(':mif', $mif);
$stmt->bindParam(':mhf', $mhf);
$stmt->bindParam(':noc', $noc, PDO::PARAM_INT);
$stmt->bindParam(':pf', $pf);
$stmt->bindParam(':vars', $vars, PDO::PARAM_INT);
$stmt->bindParam(':varsnp', $varsnp, PDO::PARAM_INT);
$stmt->bindParam(':varsi', $varsi, PDO::PARAM_INT);
$stmt->bindParam(':wmc', $wmc, PDO::PARAM_INT);
$stmt->bindParam(':wmcnp', $wmcnp, PDO::PARAM_INT);
$stmt->bindParam(':wmci', $wmci, PDO::PARAM_INT);
$stmt->execute();
$stmtInsertClass->bindParam(':runId', $runId, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':classId', $classId, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':coverage', $coverage);
$stmtInsertClass->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':locExecutable', $locExecutable, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':aif', $aif);
$stmtInsertClass->bindParam(':ahf', $ahf);
$stmtInsertClass->bindParam(':cis', $cis, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':csz', $csz, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':dit', $dit, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':impl', $impl, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':mif', $mif);
$stmtInsertClass->bindParam(':mhf', $mhf);
$stmtInsertClass->bindParam(':noc', $noc, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':pf', $pf);
$stmtInsertClass->bindParam(':vars', $vars, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':varsnp', $varsnp, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':varsi', $varsi, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':wmc', $wmc, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':wmcnp', $wmcnp, PDO::PARAM_INT);
$stmtInsertClass->bindParam(':wmci', $wmci, PDO::PARAM_INT);
$stmtInsertClass->execute();

foreach ($classMetrics->getMethods() as $methodMetrics) {
$methodName = $methodMetrics->getMethod()->getName();

$stmt = $this->dbh->prepare(
'SELECT code_method_id
FROM code_file, code_class, code_method
WHERE code_class.code_file_id = code_file.code_file_id
AND code_class.code_class_id = code_method.code_class_id
AND code_file.revision = :revision
AND code_class.code_class_name = :className
AND code_method.code_method_name = :methodName;'
);

$stmt->bindParam(':className', $className, PDO::PARAM_STR);
$stmt->bindParam(':methodName', $methodName, PDO::PARAM_STR);
$stmt->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmt->execute();

$methodId = (int)$stmt->fetchColumn();

$stmt = $this->dbh->prepare(
'INSERT INTO metrics_method
(run_id, code_method_id, metrics_method_coverage,
metrics_method_loc, metrics_method_loc_executable, metrics_method_loc_executed,
metrics_method_ccn, metrics_method_crap)
VALUES(:runId, :methodId, :coverage, :loc,
:locExecutable, :locExecuted, :ccn, :crap);'
);
$stmtSelectMethodId->bindParam(':className', $className, PDO::PARAM_STR);
$stmtSelectMethodId->bindParam(':methodName', $methodName, PDO::PARAM_STR);
$stmtSelectMethodId->bindParam(':revision', $revision, PDO::PARAM_INT);
$stmtSelectMethodId->execute();

$methodId = (int)$stmtSelectMethodId->fetchColumn();
$coverage = $methodMetrics->getCoverage();
$loc = $methodMetrics->getLoc();
$locExecutable = $methodMetrics->getLocExecutable();
$locExecuted = $methodMetrics->getLocExecuted();
$ccn = $methodMetrics->getCCN();
$crap = $methodMetrics->getCrapIndex();

$stmt->bindParam(':runId', $runId, PDO::PARAM_INT);
$stmt->bindParam(':methodId', $methodId, PDO::PARAM_INT);
$stmt->bindParam(':coverage', $coverage);
$stmt->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmt->bindParam(':locExecutable', $locExecutable, PDO::PARAM_INT);
$stmt->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmt->bindParam(':ccn', $ccn);
$stmt->bindParam(':crap', $crap);
$stmt->execute();
$stmtInsertMethod->bindParam(':runId', $runId, PDO::PARAM_INT);
$stmtInsertMethod->bindParam(':methodId', $methodId, PDO::PARAM_INT);
$stmtInsertMethod->bindParam(':coverage', $coverage);
$stmtInsertMethod->bindParam(':loc', $loc, PDO::PARAM_INT);
$stmtInsertMethod->bindParam(':locExecutable', $locExecutable, PDO::PARAM_INT);
$stmtInsertMethod->bindParam(':locExecuted', $locExecuted, PDO::PARAM_INT);
$stmtInsertMethod->bindParam(':ccn', $ccn);
$stmtInsertMethod->bindParam(':crap', $crap);
$stmtInsertMethod->execute();
}
}

unset($stmtSelectFunctionId);
unset($stmtInsertFunction);
unset($stmtSelectClassId);
unset($stmtInsertClass);
unset($stmtSelectMethodId);
unset($stmtInsertMethod);

$stmt = $this->dbh->prepare(
'SELECT code_line_id, code_line_covered
FROM code_line
Expand Down
24 changes: 24 additions & 0 deletions PHPUnit/Util/Log/Database/MySQL.sql
Expand Up @@ -70,6 +70,16 @@ CREATE TABLE IF NOT EXISTS code_file(
revision INTEGER UNSIGNED NOT NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS code_function(
code_file_id INTEGER UNSIGNED NOT NULL REFERENCES code_file.code_file_id,
code_function_id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
code_function_name CHAR(255),
code_function_start_line INTEGER UNSIGNED NOT NULL,
code_function_end_line INTEGER UNSIGNED NOT NULL,

INDEX (code_file_id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS code_class(
code_file_id INTEGER UNSIGNED NOT NULL REFERENCES code_file.code_file_id,
code_class_id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
Expand Down Expand Up @@ -135,6 +145,20 @@ CREATE TABLE IF NOT EXISTS metrics_file(
INDEX (code_file_id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS metrics_function(
run_id INTEGER UNSIGNED NOT NULL,
code_function_id INTEGER UNSIGNED NOT NULL REFERENCES code_method.code_function_id,
metrics_function_coverage FLOAT UNSIGNED NOT NULL,
metrics_function_loc INTEGER UNSIGNED NOT NULL,
metrics_function_loc_executable INTEGER UNSIGNED NOT NULL,
metrics_function_loc_executed INTEGER UNSIGNED NOT NULL,
metrics_function_ccn INTEGER UNSIGNED NOT NULL,
metrics_function_crap FLOAT UNSIGNED NOT NULL,

INDEX (run_id),
INDEX (code_function_id)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS metrics_class(
run_id INTEGER UNSIGNED NOT NULL,
code_class_id INTEGER UNSIGNED NOT NULL REFERENCES code_class.code_class_id,
Expand Down

0 comments on commit de22c3e

Please sign in to comment.