本文整理汇总了PHP中PHP_Token_Stream_CachingFactory类的典型用法代码示例。如果您正苦于以下问题:PHP PHP_Token_Stream_CachingFactory类的具体用法?PHP PHP_Token_Stream_CachingFactory怎么用?PHP PHP_Token_Stream_CachingFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PHP_Token_Stream_CachingFactory类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: process
/**
* @param PHP_CodeCoverage $coverage
* @param string $target
* @param string $name
* @return string
*/
public function process(PHP_CodeCoverage $coverage, $target = NULL, $name = NULL)
{
$document = new DOMDocument('1.0', 'UTF-8');
$document->formatOutput = TRUE;
$root = $document->createElement('coverage');
$root->setAttribute('generated', (int) $_SERVER['REQUEST_TIME']);
$document->appendChild($root);
$project = $document->createElement('project');
$project->setAttribute('timestamp', (int) $_SERVER['REQUEST_TIME']);
if (is_string($name)) {
$project->setAttribute('name', $name);
}
$root->appendChild($project);
$files = $coverage->getSummary();
$packages = array();
$projectStatistics = array('files' => 0, 'loc' => 0, 'ncloc' => 0, 'classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0);
foreach ($files as $filename => $data) {
$namespace = 'global';
if (file_exists($filename)) {
$fileStatistics = array('classes' => 0, 'methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0);
$file = $document->createElement('file');
$file->setAttribute('name', $filename);
$tokens = PHP_Token_Stream_CachingFactory::get($filename);
$classesInFile = $tokens->getClasses();
$linesOfCode = $tokens->getLinesOfCode();
unset($tokens);
$ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored($filename);
$lines = array();
foreach ($classesInFile as $className => $_class) {
$classStatistics = array('methods' => 0, 'coveredMethods' => 0, 'conditionals' => 0, 'coveredConditionals' => 0, 'statements' => 0, 'coveredStatements' => 0);
foreach ($_class['methods'] as $methodName => $method) {
$classStatistics['methods']++;
$methodCount = 0;
$methodLines = 0;
$methodLinesCovered = 0;
for ($i = $method['startLine']; $i <= $method['endLine']; $i++) {
if (isset($ignoredLines[$i])) {
continue;
}
$add = TRUE;
$count = 0;
if (isset($files[$filename][$i])) {
if ($files[$filename][$i] != -2) {
$classStatistics['statements']++;
$methodLines++;
}
if (is_array($files[$filename][$i])) {
$classStatistics['coveredStatements']++;
$methodLinesCovered++;
$count = count($files[$filename][$i]);
} else {
if ($files[$filename][$i] == -2) {
$add = FALSE;
}
}
} else {
$add = FALSE;
}
$methodCount = max($methodCount, $count);
if ($add) {
$lines[$i] = array('count' => $count, 'type' => 'stmt');
}
}
if ($methodCount > 0) {
$classStatistics['coveredMethods']++;
}
$lines[$method['startLine']] = array('count' => $methodCount, 'crap' => PHP_CodeCoverage_Util::crap($method['ccn'], PHP_CodeCoverage_Util::percent($methodLinesCovered, $methodLines)), 'type' => 'method', 'name' => $methodName);
}
$package = PHP_CodeCoverage_Util::getPackageInformation($className, $_class['docblock']);
if (!empty($package['namespace'])) {
$namespace = $package['namespace'];
}
$class = $document->createElement('class');
$class->setAttribute('name', $className);
$class->setAttribute('namespace', $namespace);
if (!empty($package['fullPackage'])) {
$class->setAttribute('fullPackage', $package['fullPackage']);
}
if (!empty($package['category'])) {
$class->setAttribute('category', $package['category']);
}
if (!empty($package['package'])) {
$class->setAttribute('package', $package['package']);
}
if (!empty($package['subpackage'])) {
$class->setAttribute('subpackage', $package['subpackage']);
}
$file->appendChild($class);
$metrics = $document->createElement('metrics');
$metrics->setAttribute('methods', $classStatistics['methods']);
$metrics->setAttribute('coveredmethods', $classStatistics['coveredMethods']);
$metrics->setAttribute('conditionals', $classStatistics['conditionals']);
$metrics->setAttribute('coveredconditionals', $classStatistics['coveredConditionals']);
$metrics->setAttribute('statements', $classStatistics['statements']);
//.........这里部分代码省略.........
示例2: getLinesToBeIgnored
/**
* Returns the lines of a source file that should be ignored.
*
* @param string $filename
* @return array
*/
public static function getLinesToBeIgnored($filename)
{
if (!isset(self::$ignoredLines[$filename])) {
self::$ignoredLines[$filename] = array();
$ignore = FALSE;
$stop = FALSE;
$tokens = PHP_Token_Stream_CachingFactory::get($filename)->tokens();
foreach ($tokens as $token) {
switch (get_class($token)) {
case 'PHP_Token_CLASS':
case 'PHP_Token_FUNCTION':
$docblock = $token->getDocblock();
$endLine = $token->getEndLine();
if (strpos($docblock, '@codeCoverageIgnore')) {
for ($i = $token->getLine(); $i <= $endLine; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
}
break;
case 'PHP_Token_COMMENT':
$_token = trim($token);
if ($_token == '// @codeCoverageIgnoreStart' || $_token == '//@codeCoverageIgnoreStart') {
$ignore = TRUE;
} else {
if ($_token == '// @codeCoverageIgnoreEnd' || $_token == '//@codeCoverageIgnoreEnd') {
$stop = TRUE;
}
}
break;
}
if ($ignore) {
self::$ignoredLines[$filename][$token->getLine()] = TRUE;
if ($stop) {
$ignore = FALSE;
$stop = FALSE;
}
}
}
}
return self::$ignoredLines[$filename];
}
示例3: calculateStatistics
/**
* Calculates coverage statistics for the file.
*/
protected function calculateStatistics()
{
$classStack = $functionStack = [];
if ($this->cacheTokens) {
$tokens = \PHP_Token_Stream_CachingFactory::get($this->getPath());
} else {
$tokens = new \PHP_Token_Stream($this->getPath());
}
$this->processClasses($tokens);
$this->processTraits($tokens);
$this->processFunctions($tokens);
$this->linesOfCode = $tokens->getLinesOfCode();
unset($tokens);
for ($lineNumber = 1; $lineNumber <= $this->linesOfCode['loc']; $lineNumber++) {
if (isset($this->startLines[$lineNumber])) {
// Start line of a class.
if (isset($this->startLines[$lineNumber]['className'])) {
if (isset($currentClass)) {
$classStack[] =& $currentClass;
}
$currentClass =& $this->startLines[$lineNumber];
} elseif (isset($this->startLines[$lineNumber]['traitName'])) {
$currentTrait =& $this->startLines[$lineNumber];
} elseif (isset($this->startLines[$lineNumber]['methodName'])) {
$currentMethod =& $this->startLines[$lineNumber];
} elseif (isset($this->startLines[$lineNumber]['functionName'])) {
if (isset($currentFunction)) {
$functionStack[] =& $currentFunction;
}
$currentFunction =& $this->startLines[$lineNumber];
}
}
if (isset($this->coverageData[$lineNumber])) {
if (isset($currentClass)) {
$currentClass['executableLines']++;
}
if (isset($currentTrait)) {
$currentTrait['executableLines']++;
}
if (isset($currentMethod)) {
$currentMethod['executableLines']++;
}
if (isset($currentFunction)) {
$currentFunction['executableLines']++;
}
$this->numExecutableLines++;
if (count($this->coverageData[$lineNumber]) > 0) {
if (isset($currentClass)) {
$currentClass['executedLines']++;
}
if (isset($currentTrait)) {
$currentTrait['executedLines']++;
}
if (isset($currentMethod)) {
$currentMethod['executedLines']++;
}
if (isset($currentFunction)) {
$currentFunction['executedLines']++;
}
$this->numExecutedLines++;
}
}
if (isset($this->endLines[$lineNumber])) {
// End line of a class.
if (isset($this->endLines[$lineNumber]['className'])) {
unset($currentClass);
if ($classStack) {
end($classStack);
$key = key($classStack);
$currentClass =& $classStack[$key];
unset($classStack[$key]);
}
} elseif (isset($this->endLines[$lineNumber]['traitName'])) {
unset($currentTrait);
} elseif (isset($this->endLines[$lineNumber]['methodName'])) {
unset($currentMethod);
} elseif (isset($this->endLines[$lineNumber]['functionName'])) {
unset($currentFunction);
if ($functionStack) {
end($functionStack);
$key = key($functionStack);
$currentFunction =& $functionStack[$key];
unset($functionStack[$key]);
}
}
}
}
foreach ($this->traits as &$trait) {
foreach ($trait['methods'] as &$method) {
if ($method['executableLines'] > 0) {
$method['coverage'] = $method['executedLines'] / $method['executableLines'] * 100;
} else {
$method['coverage'] = 100;
}
$method['crap'] = $this->crap($method['ccn'], $method['coverage']);
$trait['ccn'] += $method['ccn'];
}
//.........这里部分代码省略.........
示例4: getLinesToBeIgnored
/**
* Returns the lines of a source file that should be ignored.
*
* @param string $filename
* @return array
* @throws PHP_CodeCoverage_Exception
* @since Method available since Release 2.0.0
*/
private function getLinesToBeIgnored($filename)
{
if (!is_string($filename)) {
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(1, 'string');
}
if (!isset($this->ignoredLines[$filename])) {
$this->ignoredLines[$filename] = array();
$ignore = false;
$stop = false;
$lines = file($filename);
$numLines = count($lines);
foreach ($lines as $index => $line) {
if (!trim($line)) {
$this->ignoredLines[$filename][] = $index + 1;
}
}
if ($this->cacheTokens) {
$tokens = PHP_Token_Stream_CachingFactory::get($filename);
} else {
$tokens = new PHP_Token_Stream($filename);
}
$classes = array_merge($tokens->getClasses(), $tokens->getTraits());
$tokens = $tokens->tokens();
foreach ($tokens as $token) {
switch (get_class($token)) {
case 'PHP_Token_COMMENT':
case 'PHP_Token_DOC_COMMENT':
$_token = trim($token);
$_line = trim($lines[$token->getLine() - 1]);
if ($_token == '// @codeCoverageIgnore' || $_token == '//@codeCoverageIgnore') {
$ignore = true;
$stop = true;
} elseif ($_token == '// @codeCoverageIgnoreStart' || $_token == '//@codeCoverageIgnoreStart') {
$ignore = true;
} elseif ($_token == '// @codeCoverageIgnoreEnd' || $_token == '//@codeCoverageIgnoreEnd') {
$stop = true;
}
if (!$ignore) {
$start = $token->getLine();
$end = $start + substr_count($token, "\n");
// Do not ignore the first line when there is a token
// before the comment
if (0 !== strpos($_token, $_line)) {
$start++;
}
for ($i = $start; $i < $end; $i++) {
$this->ignoredLines[$filename][] = $i;
}
// A DOC_COMMENT token or a COMMENT token starting with "/*"
// does not contain the final \n character in its text
if (0 === strpos($_token, '/*') && '*/' === substr(trim($lines[$i - 1]), -2)) {
$this->ignoredLines[$filename][] = $i;
}
}
break;
case 'PHP_Token_INTERFACE':
case 'PHP_Token_TRAIT':
case 'PHP_Token_CLASS':
case 'PHP_Token_FUNCTION':
$docblock = $token->getDocblock();
$this->ignoredLines[$filename][] = $token->getLine();
if (strpos($docblock, '@codeCoverageIgnore')) {
$endLine = $token->getEndLine();
for ($i = $token->getLine(); $i <= $endLine; $i++) {
$this->ignoredLines[$filename][] = $i;
}
} elseif ($token instanceof PHP_Token_INTERFACE || $token instanceof PHP_Token_TRAIT || $token instanceof PHP_Token_CLASS) {
if (empty($classes[$token->getName()]['methods'])) {
for ($i = $token->getLine(); $i <= $token->getEndLine(); $i++) {
$this->ignoredLines[$filename][] = $i;
}
} else {
$firstMethod = array_shift($classes[$token->getName()]['methods']);
do {
$lastMethod = array_pop($classes[$token->getName()]['methods']);
} while ($lastMethod !== null && substr($lastMethod['signature'], 0, 18) == 'anonymous function');
if ($lastMethod === null) {
$lastMethod = $firstMethod;
}
for ($i = $token->getLine(); $i < $firstMethod['startLine']; $i++) {
$this->ignoredLines[$filename][] = $i;
}
for ($i = $token->getEndLine(); $i > $lastMethod['endLine']; $i--) {
$this->ignoredLines[$filename][] = $i;
}
}
}
break;
case 'PHP_Token_NAMESPACE':
$this->ignoredLines[$filename][] = $token->getEndLine();
// Intentional fallthrough
// Intentional fallthrough
//.........这里部分代码省略.........
示例5: processFunctions
protected function processFunctions()
{
$tokens = PHP_Token_Stream_CachingFactory::get($this->getPath());
$functions = $tokens->getFunctions();
unset($tokens);
if (count($functions) > 0 && !isset($this->classes['*'])) {
$this->classes['*'] = array('methods' => array(), 'startLine' => 0, 'executableLines' => 0, 'executedLines' => 0, 'ccn' => 0);
}
foreach ($functions as $functionName => $function) {
$this->classes['*']['methods'][$functionName] = array('signature' => $function['signature'], 'startLine' => $function['startLine'], 'executableLines' => 0, 'executedLines' => 0, 'ccn' => $function['ccn']);
$this->startLines[$function['startLine']] =& $this->classes['*']['methods'][$functionName];
$this->endLines[$function['endLine']] =& $this->classes['*']['methods'][$functionName];
}
}
示例6: getLinesToBeIgnored
/**
* Returns the lines of a source file that should be ignored.
*
* @param string $filename
* @param boolean $cacheTokens
* @return array
* @throws PHP_CodeCoverage_Exception
*/
public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE)
{
if (!is_string($filename)) {
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(1, 'string');
}
if (!is_bool($cacheTokens)) {
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(2, 'boolean');
}
if (!isset(self::$ignoredLines[$filename])) {
self::$ignoredLines[$filename] = array();
$ignore = FALSE;
$stop = FALSE;
$lines = file($filename);
foreach ($lines as $index => $line) {
if (!trim($line)) {
self::$ignoredLines[$filename][$index + 1] = TRUE;
}
}
if ($cacheTokens) {
$tokens = PHP_Token_Stream_CachingFactory::get($filename);
} else {
$tokens = new PHP_Token_Stream($filename);
}
$classes = array_merge($tokens->getClasses(), $tokens->getTraits());
$tokens = $tokens->tokens();
foreach ($tokens as $token) {
switch (get_class($token)) {
case 'PHP_Token_COMMENT':
case 'PHP_Token_DOC_COMMENT':
$count = substr_count($token, "\n");
$line = $token->getLine();
for ($i = $line; $i < $line + $count; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
if ($token instanceof PHP_Token_DOC_COMMENT) {
// Workaround for the fact the DOC_COMMENT token
// does not include the final \n character in its
// text.
if (substr(trim($lines[$i - 1]), -2) == '*/') {
self::$ignoredLines[$filename][$i] = TRUE;
}
break;
}
$_token = trim($token);
if ($_token == '// @codeCoverageIgnore' || $_token == '//@codeCoverageIgnore') {
$ignore = TRUE;
$stop = TRUE;
} else {
if ($_token == '// @codeCoverageIgnoreStart' || $_token == '//@codeCoverageIgnoreStart') {
$ignore = TRUE;
} else {
if ($_token == '// @codeCoverageIgnoreEnd' || $_token == '//@codeCoverageIgnoreEnd') {
$stop = TRUE;
}
}
}
break;
case 'PHP_Token_INTERFACE':
case 'PHP_Token_TRAIT':
case 'PHP_Token_CLASS':
case 'PHP_Token_FUNCTION':
$docblock = $token->getDocblock();
if (strpos($docblock, '@codeCoverageIgnore')) {
$endLine = $token->getEndLine();
for ($i = $token->getLine(); $i <= $endLine; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
} else {
if ($token instanceof PHP_Token_INTERFACE || $token instanceof PHP_Token_TRAIT || $token instanceof PHP_Token_CLASS) {
if (empty($classes[$token->getName()]['methods'])) {
for ($i = $token->getLine(); $i <= $token->getEndLine(); $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
} else {
$firstMethod = array_shift($classes[$token->getName()]['methods']);
$lastMethod = array_pop($classes[$token->getName()]['methods']);
if ($lastMethod === NULL) {
$lastMethod = $firstMethod;
}
for ($i = $token->getLine(); $i < $firstMethod['startLine']; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
for ($i = $token->getEndLine(); $i > $lastMethod['endLine']; $i--) {
self::$ignoredLines[$filename][$i] = TRUE;
}
}
}
}
break;
case 'PHP_Token_INTERFACE':
$endLine = $token->getEndLine();
for ($i = $token->getLine(); $i <= $endLine; $i++) {
//.........这里部分代码省略.........
示例7: getLinesToBeIgnored
/**
* Returns the lines of a source file that should be ignored.
*
* @param string $filename
* @param boolean $cacheTokens
* @return array
* @throws InvalidArgumentException
*/
public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE)
{
if (!is_bool($cacheTokens)) {
throw new InvalidArgumentException();
}
if (!isset(self::$ignoredLines[$filename])) {
self::$ignoredLines[$filename] = array();
$ignore = FALSE;
$stop = FALSE;
if ($cacheTokens) {
$tokens = PHP_Token_Stream_CachingFactory::get($filename);
} else {
$tokens = new PHP_Token_Stream($filename);
}
$classes = $tokens->getClasses();
$tokens = $tokens->tokens();
foreach ($tokens as $token) {
switch (get_class($token)) {
case 'PHP_Token_CLASS':
case 'PHP_Token_FUNCTION':
$docblock = $token->getDocblock();
if (strpos($docblock, '@codeCoverageIgnore')) {
$endLine = $token->getEndLine();
for ($i = $token->getLine(); $i <= $endLine; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
} else {
if ($token instanceof PHP_Token_CLASS && !empty($classes[$token->getName()]['methods'])) {
$firstMethod = array_shift($classes[$token->getName()]['methods']);
for ($i = $token->getLine(); $i < $firstMethod['startLine']; $i++) {
self::$ignoredLines[$filename][$i] = TRUE;
}
}
}
break;
case 'PHP_Token_COMMENT':
$_token = trim($token);
if ($_token == '// @codeCoverageIgnoreStart' || $_token == '//@codeCoverageIgnoreStart') {
$ignore = TRUE;
} else {
if ($_token == '// @codeCoverageIgnoreEnd' || $_token == '//@codeCoverageIgnoreEnd') {
$stop = TRUE;
}
}
break;
}
if ($ignore) {
self::$ignoredLines[$filename][$token->getLine()] = TRUE;
if ($stop) {
$ignore = FALSE;
$stop = FALSE;
}
}
}
}
return self::$ignoredLines[$filename];
}
示例8: processUncoveredFilesFromWhitelist
/**
* Processes whitelisted files that are not covered.
*/
protected function processUncoveredFilesFromWhitelist()
{
$data = array();
$uncoveredFiles = array_diff($this->filter->getWhitelist(), array_keys($this->data));
foreach ($uncoveredFiles as $uncoveredFile) {
if (!file_exists($uncoveredFile)) {
continue;
}
if ($this->cacheTokens) {
$tokens = PHP_Token_Stream_CachingFactory::get($uncoveredFile);
} else {
$tokens = new PHP_Token_Stream($uncoveredFile);
}
$classes = $tokens->getClasses();
$interfaces = $tokens->getInterfaces();
$functions = $tokens->getFunctions();
unset($tokens);
foreach (array_keys($classes) as $class) {
if (class_exists($class, FALSE)) {
continue 2;
}
}
unset($classes);
foreach (array_keys($interfaces) as $interface) {
if (interface_exists($interface, FALSE)) {
continue 2;
}
}
unset($interfaces);
foreach (array_keys($functions) as $function) {
if (function_exists($function)) {
continue 2;
}
}
unset($functions);
$this->driver->start();
include_once $uncoveredFile;
$coverage = $this->driver->stop();
foreach ($coverage as $file => $fileCoverage) {
if (!isset($data[$file]) && in_array($file, $uncoveredFiles)) {
foreach (array_keys($fileCoverage) as $key) {
if ($fileCoverage[$key] == 1) {
$fileCoverage[$key] = -1;
}
}
$data[$file] = $fileCoverage;
}
}
}
$this->append($data, 'UNCOVERED_FILES_FROM_WHITELIST');
}
示例9: clear
/**
* @param string $filename
*/
public static function clear($filename = NULL)
{
if (is_string($filename)) {
unset(self::$cache[$filename]);
} else {
self::$cache = array();
}
}