First Commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function file_get_contents;
|
||||
use SebastianBergmann\CodeCoverage\Filter;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class CacheWarmer
|
||||
{
|
||||
/**
|
||||
* @return array{cacheHits: non-negative-int, cacheMisses: non-negative-int}
|
||||
*/
|
||||
public function warmCache(string $cacheDirectory, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode, Filter $filter): array
|
||||
{
|
||||
$analyser = new CachingSourceAnalyser(
|
||||
$cacheDirectory,
|
||||
new ParsingSourceAnalyser,
|
||||
);
|
||||
|
||||
foreach ($filter->files() as $file) {
|
||||
$analyser->analyse(
|
||||
$file,
|
||||
file_get_contents($file),
|
||||
$useAnnotationsForIgnoringCode,
|
||||
$ignoreDeprecatedCode,
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'cacheHits' => $analyser->cacheHits(),
|
||||
'cacheMisses' => $analyser->cacheMisses(),
|
||||
];
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use function file_get_contents;
|
||||
use function file_put_contents;
|
||||
use function hash;
|
||||
use function implode;
|
||||
use function is_file;
|
||||
use function serialize;
|
||||
use function unserialize;
|
||||
use SebastianBergmann\CodeCoverage\Util\Filesystem;
|
||||
use SebastianBergmann\CodeCoverage\Version;
|
||||
|
||||
/**
|
||||
* @internal This interface is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final class CachingSourceAnalyser implements SourceAnalyser
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private readonly string $directory;
|
||||
private readonly SourceAnalyser $sourceAnalyser;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $cacheHits = 0;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $cacheMisses = 0;
|
||||
|
||||
public function __construct(string $directory, SourceAnalyser $sourceAnalyser)
|
||||
{
|
||||
Filesystem::createDirectory($directory);
|
||||
|
||||
$this->directory = $directory;
|
||||
$this->sourceAnalyser = $sourceAnalyser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $sourceCodeFile
|
||||
*/
|
||||
public function analyse(string $sourceCodeFile, string $sourceCode, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode): AnalysisResult
|
||||
{
|
||||
$cacheFile = $this->cacheFile(
|
||||
$sourceCode,
|
||||
$useAnnotationsForIgnoringCode,
|
||||
$ignoreDeprecatedCode,
|
||||
);
|
||||
|
||||
$cachedAnalysisResult = $this->read($cacheFile);
|
||||
|
||||
if ($cachedAnalysisResult !== false) {
|
||||
$this->cacheHits++;
|
||||
|
||||
return $cachedAnalysisResult;
|
||||
}
|
||||
|
||||
$this->cacheMisses++;
|
||||
|
||||
$analysisResult = $this->sourceAnalyser->analyse(
|
||||
$sourceCodeFile,
|
||||
$sourceCode,
|
||||
$useAnnotationsForIgnoringCode,
|
||||
$ignoreDeprecatedCode,
|
||||
);
|
||||
|
||||
$this->write($cacheFile, $analysisResult);
|
||||
|
||||
return $analysisResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function cacheHits(): int
|
||||
{
|
||||
return $this->cacheHits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function cacheMisses(): int
|
||||
{
|
||||
return $this->cacheMisses;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $cacheFile
|
||||
*/
|
||||
private function read(string $cacheFile): AnalysisResult|false
|
||||
{
|
||||
if (!is_file($cacheFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return unserialize(
|
||||
file_get_contents($cacheFile),
|
||||
[
|
||||
'allowed_classes' => [
|
||||
AnalysisResult::class,
|
||||
Class_::class,
|
||||
Function_::class,
|
||||
Interface_::class,
|
||||
LinesOfCode::class,
|
||||
Method::class,
|
||||
Trait_::class,
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $cacheFile
|
||||
*/
|
||||
private function write(string $cacheFile, AnalysisResult $result): void
|
||||
{
|
||||
file_put_contents(
|
||||
$cacheFile,
|
||||
serialize($result),
|
||||
);
|
||||
}
|
||||
|
||||
private function cacheFile(string $source, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode): string
|
||||
{
|
||||
$cacheKey = hash(
|
||||
'sha256',
|
||||
implode(
|
||||
"\0",
|
||||
[
|
||||
$source,
|
||||
Version::id(),
|
||||
$useAnnotationsForIgnoringCode,
|
||||
$ignoreDeprecatedCode,
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
return $this->directory . DIRECTORY_SEPARATOR . $cacheKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function file_get_contents;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final class FileAnalyser
|
||||
{
|
||||
private readonly SourceAnalyser $sourceAnalyser;
|
||||
private readonly bool $useAnnotationsForIgnoringCode;
|
||||
private readonly bool $ignoreDeprecatedCode;
|
||||
|
||||
/**
|
||||
* @var array<non-empty-string, AnalysisResult>
|
||||
*/
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct(SourceAnalyser $sourceAnalyser, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode)
|
||||
{
|
||||
$this->sourceAnalyser = $sourceAnalyser;
|
||||
$this->useAnnotationsForIgnoringCode = $useAnnotationsForIgnoringCode;
|
||||
$this->ignoreDeprecatedCode = $ignoreDeprecatedCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param non-empty-string $sourceCodeFile
|
||||
*/
|
||||
public function analyse(string $sourceCodeFile): AnalysisResult
|
||||
{
|
||||
if (isset($this->cache[$sourceCodeFile])) {
|
||||
return $this->cache[$sourceCodeFile];
|
||||
}
|
||||
|
||||
$this->cache[$sourceCodeFile] = $this->sourceAnalyser->analyse(
|
||||
$sourceCodeFile,
|
||||
file_get_contents($sourceCodeFile),
|
||||
$this->useAnnotationsForIgnoringCode,
|
||||
$this->ignoreDeprecatedCode,
|
||||
);
|
||||
|
||||
return $this->cache[$sourceCodeFile];
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use const T_COMMENT;
|
||||
use const T_DOC_COMMENT;
|
||||
use function array_merge;
|
||||
use function array_unique;
|
||||
use function assert;
|
||||
use function is_array;
|
||||
use function max;
|
||||
use function range;
|
||||
use function sort;
|
||||
use function sprintf;
|
||||
use function substr_count;
|
||||
use function token_get_all;
|
||||
use function trim;
|
||||
use PhpParser\Error;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitor\NameResolver;
|
||||
use PhpParser\ParserFactory;
|
||||
use SebastianBergmann\CodeCoverage\ParserException;
|
||||
use SebastianBergmann\LinesOfCode\LineCountingVisitor;
|
||||
|
||||
/**
|
||||
* @internal This interface is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class ParsingSourceAnalyser implements SourceAnalyser
|
||||
{
|
||||
/**
|
||||
* @param non-empty-string $sourceCodeFile
|
||||
*/
|
||||
public function analyse(string $sourceCodeFile, string $sourceCode, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode): AnalysisResult
|
||||
{
|
||||
$linesOfCode = max(substr_count($sourceCode, "\n") + 1, substr_count($sourceCode, "\r") + 1);
|
||||
|
||||
if ($linesOfCode === 0 && $sourceCode !== '') {
|
||||
$linesOfCode = 1;
|
||||
}
|
||||
|
||||
assert($linesOfCode > 0);
|
||||
|
||||
$parser = (new ParserFactory)->createForHostVersion();
|
||||
|
||||
try {
|
||||
$nodes = $parser->parse($sourceCode);
|
||||
|
||||
assert($nodes !== null);
|
||||
|
||||
$traverser = new NodeTraverser;
|
||||
$codeUnitFindingVisitor = new CodeUnitFindingVisitor($sourceCodeFile);
|
||||
$lineCountingVisitor = new LineCountingVisitor($linesOfCode);
|
||||
$ignoredLinesFindingVisitor = new IgnoredLinesFindingVisitor($useAnnotationsForIgnoringCode, $ignoreDeprecatedCode);
|
||||
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($sourceCode);
|
||||
|
||||
$traverser->addVisitor(new NameResolver);
|
||||
$traverser->addVisitor(new AttributeParentConnectingVisitor);
|
||||
$traverser->addVisitor($codeUnitFindingVisitor);
|
||||
$traverser->addVisitor($lineCountingVisitor);
|
||||
$traverser->addVisitor($ignoredLinesFindingVisitor);
|
||||
$traverser->addVisitor($executableLinesFindingVisitor);
|
||||
|
||||
/* @noinspection UnusedFunctionResultInspection */
|
||||
$traverser->traverse($nodes);
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Error $error) {
|
||||
throw new ParserException(
|
||||
sprintf(
|
||||
'Cannot parse %s: %s',
|
||||
$sourceCodeFile,
|
||||
$error->getMessage(),
|
||||
),
|
||||
$error->getCode(),
|
||||
$error,
|
||||
);
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
$ignoredLines = array_unique(
|
||||
array_merge(
|
||||
$this->findLinesIgnoredByLineBasedAnnotations(
|
||||
$sourceCodeFile,
|
||||
$sourceCode,
|
||||
$useAnnotationsForIgnoringCode,
|
||||
),
|
||||
$ignoredLinesFindingVisitor->ignoredLines(),
|
||||
),
|
||||
);
|
||||
|
||||
sort($ignoredLines);
|
||||
|
||||
return new AnalysisResult(
|
||||
$codeUnitFindingVisitor->interfaces(),
|
||||
$codeUnitFindingVisitor->classes(),
|
||||
$codeUnitFindingVisitor->traits(),
|
||||
$codeUnitFindingVisitor->functions(),
|
||||
new LinesOfCode(
|
||||
$lineCountingVisitor->result()->linesOfCode(),
|
||||
$lineCountingVisitor->result()->commentLinesOfCode(),
|
||||
$lineCountingVisitor->result()->nonCommentLinesOfCode(),
|
||||
),
|
||||
$executableLinesFindingVisitor->executableLinesGroupedByBranch(),
|
||||
$ignoredLines,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, int>
|
||||
*/
|
||||
private function findLinesIgnoredByLineBasedAnnotations(string $filename, string $source, bool $useAnnotationsForIgnoringCode): array
|
||||
{
|
||||
if (!$useAnnotationsForIgnoringCode) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
$start = false;
|
||||
|
||||
foreach (token_get_all($source) as $token) {
|
||||
if (!is_array($token) ||
|
||||
!(T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$comment = trim($token[1]);
|
||||
|
||||
if ($comment === '// @codeCoverageIgnore' ||
|
||||
$comment === '//@codeCoverageIgnore') {
|
||||
$result[] = $token[2];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($comment === '// @codeCoverageIgnoreStart' ||
|
||||
$comment === '//@codeCoverageIgnoreStart') {
|
||||
$start = $token[2];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($comment === '// @codeCoverageIgnoreEnd' ||
|
||||
$comment === '//@codeCoverageIgnoreEnd') {
|
||||
if (false === $start) {
|
||||
$start = $token[2];
|
||||
}
|
||||
|
||||
$result = array_merge(
|
||||
$result,
|
||||
range($start, $token[2]),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This interface is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
interface SourceAnalyser
|
||||
{
|
||||
/**
|
||||
* @param non-empty-string $sourceCodeFile
|
||||
*/
|
||||
public function analyse(string $sourceCodeFile, string $sourceCode, bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecatedCode): AnalysisResult;
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @phpstan-type LinesType array<int, int>
|
||||
*
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class AnalysisResult
|
||||
{
|
||||
/**
|
||||
* @var array<string, Interface_>
|
||||
*/
|
||||
private array $interfaces;
|
||||
|
||||
/**
|
||||
* @var array<string, Class_>
|
||||
*/
|
||||
private array $classes;
|
||||
|
||||
/**
|
||||
* @var array<string, Trait_>
|
||||
*/
|
||||
private array $traits;
|
||||
|
||||
/**
|
||||
* @var array<string, Function_>
|
||||
*/
|
||||
private array $functions;
|
||||
private LinesOfCode $linesOfCode;
|
||||
|
||||
/**
|
||||
* @var LinesType
|
||||
*/
|
||||
private array $executableLines;
|
||||
|
||||
/**
|
||||
* @var LinesType
|
||||
*/
|
||||
private array $ignoredLines;
|
||||
|
||||
/**
|
||||
* @param array<string, Interface_> $interfaces
|
||||
* @param array<string, Class_> $classes
|
||||
* @param array<string, Trait_> $traits
|
||||
* @param array<string, Function_> $functions
|
||||
* @param LinesType $executableLines
|
||||
* @param LinesType $ignoredLines
|
||||
*/
|
||||
public function __construct(array $interfaces, array $classes, array $traits, array $functions, LinesOfCode $linesOfCode, array $executableLines, array $ignoredLines)
|
||||
{
|
||||
$this->interfaces = $interfaces;
|
||||
$this->classes = $classes;
|
||||
$this->traits = $traits;
|
||||
$this->functions = $functions;
|
||||
$this->linesOfCode = $linesOfCode;
|
||||
$this->executableLines = $executableLines;
|
||||
$this->ignoredLines = $ignoredLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Interface_>
|
||||
*/
|
||||
public function interfaces(): array
|
||||
{
|
||||
return $this->interfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Class_>
|
||||
*/
|
||||
public function classes(): array
|
||||
{
|
||||
return $this->classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Trait_>
|
||||
*/
|
||||
public function traits(): array
|
||||
{
|
||||
return $this->traits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Function_>
|
||||
*/
|
||||
public function functions(): array
|
||||
{
|
||||
return $this->functions;
|
||||
}
|
||||
|
||||
public function linesOfCode(): LinesOfCode
|
||||
{
|
||||
return $this->linesOfCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LinesType
|
||||
*/
|
||||
public function executableLines(): array
|
||||
{
|
||||
return $this->executableLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LinesType
|
||||
*/
|
||||
public function ignoredLines(): array
|
||||
{
|
||||
return $this->ignoredLines;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class Class_
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $namespacedName;
|
||||
private string $namespace;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $file;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $startLine;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $endLine;
|
||||
|
||||
/**
|
||||
* @var ?non-empty-string
|
||||
*/
|
||||
private ?string $parentClass;
|
||||
|
||||
/**
|
||||
* @var list<non-empty-string>
|
||||
*/
|
||||
private array $interfaces;
|
||||
|
||||
/**
|
||||
* @var list<non-empty-string>
|
||||
*/
|
||||
private array $traits;
|
||||
|
||||
/**
|
||||
* @var array<non-empty-string, Method>
|
||||
*/
|
||||
private array $methods;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $name
|
||||
* @param non-empty-string $namespacedName
|
||||
* @param non-empty-string $file
|
||||
* @param non-negative-int $startLine
|
||||
* @param non-negative-int $endLine
|
||||
* @param ?non-empty-string $parentClass
|
||||
* @param list<non-empty-string> $interfaces
|
||||
* @param list<non-empty-string> $traits
|
||||
* @param array<non-empty-string, Method> $methods
|
||||
*/
|
||||
public function __construct(string $name, string $namespacedName, string $namespace, string $file, int $startLine, int $endLine, ?string $parentClass, array $interfaces, array $traits, array $methods)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->namespacedName = $namespacedName;
|
||||
$this->namespace = $namespace;
|
||||
$this->file = $file;
|
||||
$this->startLine = $startLine;
|
||||
$this->endLine = $endLine;
|
||||
$this->parentClass = $parentClass;
|
||||
$this->interfaces = $interfaces;
|
||||
$this->traits = $traits;
|
||||
$this->methods = $methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function namespacedName(): string
|
||||
{
|
||||
return $this->namespacedName;
|
||||
}
|
||||
|
||||
public function isNamespaced(): bool
|
||||
{
|
||||
return $this->namespace !== '';
|
||||
}
|
||||
|
||||
public function namespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function file(): string
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function startLine(): int
|
||||
{
|
||||
return $this->startLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function endLine(): int
|
||||
{
|
||||
return $this->endLine;
|
||||
}
|
||||
|
||||
public function hasParent(): bool
|
||||
{
|
||||
return $this->parentClass !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?non-empty-string
|
||||
*/
|
||||
public function parentClass(): ?string
|
||||
{
|
||||
return $this->parentClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<non-empty-string>
|
||||
*/
|
||||
public function interfaces(): array
|
||||
{
|
||||
return $this->interfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<non-empty-string>
|
||||
*/
|
||||
public function traits(): array
|
||||
{
|
||||
return $this->traits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<non-empty-string, Method>
|
||||
*/
|
||||
public function methods(): array
|
||||
{
|
||||
return $this->methods;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class Function_
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $namespacedName;
|
||||
private string $namespace;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $startLine;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $endLine;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $signature;
|
||||
|
||||
/**
|
||||
* @var positive-int
|
||||
*/
|
||||
private int $cyclomaticComplexity;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $name
|
||||
* @param non-empty-string $namespacedName
|
||||
* @param non-negative-int $startLine
|
||||
* @param non-negative-int $endLine
|
||||
* @param non-empty-string $signature
|
||||
* @param positive-int $cyclomaticComplexity
|
||||
*/
|
||||
public function __construct(string $name, string $namespacedName, string $namespace, int $startLine, int $endLine, string $signature, int $cyclomaticComplexity)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->namespacedName = $namespacedName;
|
||||
$this->namespace = $namespace;
|
||||
$this->startLine = $startLine;
|
||||
$this->endLine = $endLine;
|
||||
$this->signature = $signature;
|
||||
$this->cyclomaticComplexity = $cyclomaticComplexity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function namespacedName(): string
|
||||
{
|
||||
return $this->namespacedName;
|
||||
}
|
||||
|
||||
public function isNamespaced(): bool
|
||||
{
|
||||
return $this->namespace !== '';
|
||||
}
|
||||
|
||||
public function namespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function startLine(): int
|
||||
{
|
||||
return $this->startLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function endLine(): int
|
||||
{
|
||||
return $this->endLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function signature(): string
|
||||
{
|
||||
return $this->signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return positive-int
|
||||
*/
|
||||
public function cyclomaticComplexity(): int
|
||||
{
|
||||
return $this->cyclomaticComplexity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class Interface_
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $namespacedName;
|
||||
private string $namespace;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $startLine;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $endLine;
|
||||
|
||||
/**
|
||||
* @var list<non-empty-string>
|
||||
*/
|
||||
private array $parentInterfaces;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $name
|
||||
* @param non-empty-string $namespacedName
|
||||
* @param non-negative-int $startLine
|
||||
* @param non-negative-int $endLine
|
||||
* @param list<non-empty-string> $parentInterfaces
|
||||
*/
|
||||
public function __construct(string $name, string $namespacedName, string $namespace, int $startLine, int $endLine, array $parentInterfaces)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->namespacedName = $namespacedName;
|
||||
$this->namespace = $namespace;
|
||||
$this->startLine = $startLine;
|
||||
$this->endLine = $endLine;
|
||||
$this->parentInterfaces = $parentInterfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function namespacedName(): string
|
||||
{
|
||||
return $this->namespacedName;
|
||||
}
|
||||
|
||||
public function isNamespaced(): bool
|
||||
{
|
||||
return $this->namespace !== '';
|
||||
}
|
||||
|
||||
public function namespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function startLine(): int
|
||||
{
|
||||
return $this->startLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function endLine(): int
|
||||
{
|
||||
return $this->endLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<non-empty-string>
|
||||
*/
|
||||
public function parentInterfaces(): array
|
||||
{
|
||||
return $this->parentInterfaces;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class LinesOfCode
|
||||
{
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $linesOfCode;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $commentLinesOfCode;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $nonCommentLinesOfCode;
|
||||
|
||||
/**
|
||||
* @param non-negative-int $linesOfCode
|
||||
* @param non-negative-int $commentLinesOfCode
|
||||
* @param non-negative-int $nonCommentLinesOfCode
|
||||
*/
|
||||
public function __construct(int $linesOfCode, int $commentLinesOfCode, int $nonCommentLinesOfCode)
|
||||
{
|
||||
$this->linesOfCode = $linesOfCode;
|
||||
$this->commentLinesOfCode = $commentLinesOfCode;
|
||||
$this->nonCommentLinesOfCode = $nonCommentLinesOfCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function linesOfCode(): int
|
||||
{
|
||||
return $this->linesOfCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function commentLinesOfCode(): int
|
||||
{
|
||||
return $this->commentLinesOfCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function nonCommentLinesOfCode(): int
|
||||
{
|
||||
return $this->nonCommentLinesOfCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class Method
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $startLine;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $endLine;
|
||||
private Visibility $visibility;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $signature;
|
||||
|
||||
/**
|
||||
* @var positive-int
|
||||
*/
|
||||
private int $cyclomaticComplexity;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $name
|
||||
* @param non-negative-int $startLine
|
||||
* @param non-negative-int $endLine
|
||||
* @param non-empty-string $signature
|
||||
* @param positive-int $cyclomaticComplexity
|
||||
*/
|
||||
public function __construct(string $name, int $startLine, int $endLine, string $signature, Visibility $visibility, int $cyclomaticComplexity)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->startLine = $startLine;
|
||||
$this->endLine = $endLine;
|
||||
$this->signature = $signature;
|
||||
$this->visibility = $visibility;
|
||||
$this->cyclomaticComplexity = $cyclomaticComplexity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function startLine(): int
|
||||
{
|
||||
return $this->startLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function endLine(): int
|
||||
{
|
||||
return $this->endLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function signature(): string
|
||||
{
|
||||
return $this->signature;
|
||||
}
|
||||
|
||||
public function visibility(): Visibility
|
||||
{
|
||||
return $this->visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return positive-int
|
||||
*/
|
||||
public function cyclomaticComplexity(): int
|
||||
{
|
||||
return $this->cyclomaticComplexity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final readonly class Trait_
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $namespacedName;
|
||||
private string $namespace;
|
||||
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $file;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $startLine;
|
||||
|
||||
/**
|
||||
* @var non-negative-int
|
||||
*/
|
||||
private int $endLine;
|
||||
|
||||
/**
|
||||
* @var list<non-empty-string>
|
||||
*/
|
||||
private array $traits;
|
||||
|
||||
/**
|
||||
* @var array<non-empty-string, Method>
|
||||
*/
|
||||
private array $methods;
|
||||
|
||||
/**
|
||||
* @param non-empty-string $name
|
||||
* @param non-empty-string $namespacedName
|
||||
* @param non-empty-string $file
|
||||
* @param non-negative-int $startLine
|
||||
* @param non-negative-int $endLine
|
||||
* @param list<non-empty-string> $traits
|
||||
* @param array<non-empty-string, Method> $methods
|
||||
*/
|
||||
public function __construct(string $name, string $namespacedName, string $namespace, string $file, int $startLine, int $endLine, array $traits, array $methods)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->namespacedName = $namespacedName;
|
||||
$this->namespace = $namespace;
|
||||
$this->file = $file;
|
||||
$this->startLine = $startLine;
|
||||
$this->endLine = $endLine;
|
||||
$this->traits = $traits;
|
||||
$this->methods = $methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function namespacedName(): string
|
||||
{
|
||||
return $this->namespacedName;
|
||||
}
|
||||
|
||||
public function isNamespaced(): bool
|
||||
{
|
||||
return $this->namespace !== '';
|
||||
}
|
||||
|
||||
public function namespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-empty-string
|
||||
*/
|
||||
public function file(): string
|
||||
{
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function startLine(): int
|
||||
{
|
||||
return $this->startLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non-negative-int
|
||||
*/
|
||||
public function endLine(): int
|
||||
{
|
||||
return $this->endLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<non-empty-string>
|
||||
*/
|
||||
public function traits(): array
|
||||
{
|
||||
return $this->traits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<non-empty-string, Method>
|
||||
*/
|
||||
public function methods(): array
|
||||
{
|
||||
return $this->methods;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
/**
|
||||
* @internal This enumeration is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
enum Visibility: string
|
||||
{
|
||||
case Public = 'public';
|
||||
case Protected = 'protected';
|
||||
case Private = 'private';
|
||||
}
|
||||
Vendored
+63
@@ -0,0 +1,63 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function array_pop;
|
||||
use function count;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitor;
|
||||
|
||||
/**
|
||||
* Visitor that connects a child node to its parent node optimized for Attribute nodes.
|
||||
*
|
||||
* On the child node, the parent node can be accessed through
|
||||
* <code>$node->getAttribute('parent')</code>.
|
||||
*
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final class AttributeParentConnectingVisitor implements NodeVisitor
|
||||
{
|
||||
/**
|
||||
* @var Node[]
|
||||
*/
|
||||
private array $stack = [];
|
||||
|
||||
public function beforeTraverse(array $nodes): null
|
||||
{
|
||||
$this->stack = [];
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): null
|
||||
{
|
||||
if ($this->stack !== [] &&
|
||||
($node instanceof Node\Attribute || $node instanceof Node\AttributeGroup)) {
|
||||
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
|
||||
}
|
||||
|
||||
$this->stack[] = $node;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): null
|
||||
{
|
||||
array_pop($this->stack);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function afterTraverse(array $nodes): null
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+435
@@ -0,0 +1,435 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function assert;
|
||||
use function implode;
|
||||
use function rtrim;
|
||||
use function trim;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\ComplexType;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\IntersectionType;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Enum_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PhpParser\Node\UnionType;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use SebastianBergmann\Complexity\CyclomaticComplexityCalculatingVisitor;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final class CodeUnitFindingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
/**
|
||||
* @var non-empty-string
|
||||
*/
|
||||
private string $file;
|
||||
|
||||
/**
|
||||
* @var array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Interface_>
|
||||
*/
|
||||
private array $interfaces = [];
|
||||
|
||||
/**
|
||||
* @var array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Class_>
|
||||
*/
|
||||
private array $classes = [];
|
||||
|
||||
/**
|
||||
* @var array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_>
|
||||
*/
|
||||
private array $traits = [];
|
||||
|
||||
/**
|
||||
* @var array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Function_>
|
||||
*/
|
||||
private array $functions = [];
|
||||
|
||||
/**
|
||||
* @param non-empty-string $file
|
||||
*/
|
||||
public function __construct(string $file)
|
||||
{
|
||||
$this->file = $file;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): null
|
||||
{
|
||||
if ($node instanceof Interface_) {
|
||||
$this->processInterface($node);
|
||||
}
|
||||
|
||||
if ($node instanceof Class_) {
|
||||
if ($node->isAnonymous()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->processClass($node);
|
||||
}
|
||||
|
||||
if ($node instanceof Enum_) {
|
||||
$this->processClass($node);
|
||||
}
|
||||
|
||||
if ($node instanceof Trait_) {
|
||||
$this->processTrait($node);
|
||||
}
|
||||
|
||||
if (!$node instanceof Function_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->processFunction($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): null
|
||||
{
|
||||
if ($node instanceof Class_ && $node->isAnonymous()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$node instanceof Class_ && !$node instanceof Enum_ && !$node instanceof Trait_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$traits = [];
|
||||
|
||||
foreach ($node->getTraitUses() as $traitUse) {
|
||||
foreach ($traitUse->traits as $trait) {
|
||||
$traits[] = $trait->toString();
|
||||
}
|
||||
}
|
||||
|
||||
if ($traits === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->postProcessClassOrTrait($node, $traits);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Interface_>
|
||||
*/
|
||||
public function interfaces(): array
|
||||
{
|
||||
return $this->interfaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Class_>
|
||||
*/
|
||||
public function classes(): array
|
||||
{
|
||||
return $this->classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_>
|
||||
*/
|
||||
public function traits(): array
|
||||
{
|
||||
return $this->traits;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \SebastianBergmann\CodeCoverage\StaticAnalysis\Function_>
|
||||
*/
|
||||
public function functions(): array
|
||||
{
|
||||
return $this->functions;
|
||||
}
|
||||
|
||||
private function cyclomaticComplexity(ClassMethod|Function_ $node): int
|
||||
{
|
||||
$nodes = $node->getStmts();
|
||||
|
||||
if ($nodes === null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$traverser = new NodeTraverser;
|
||||
|
||||
$cyclomaticComplexityCalculatingVisitor = new CyclomaticComplexityCalculatingVisitor;
|
||||
|
||||
$traverser->addVisitor($cyclomaticComplexityCalculatingVisitor);
|
||||
|
||||
/* @noinspection UnusedFunctionResultInspection */
|
||||
$traverser->traverse($nodes);
|
||||
|
||||
return $cyclomaticComplexityCalculatingVisitor->cyclomaticComplexity();
|
||||
}
|
||||
|
||||
private function signature(ClassMethod|Function_ $node): string
|
||||
{
|
||||
$signature = ($node->returnsByRef() ? '&' : '') . $node->name->toString() . '(';
|
||||
$parameters = [];
|
||||
|
||||
foreach ($node->getParams() as $parameter) {
|
||||
assert(isset($parameter->var->name));
|
||||
|
||||
$parameterAsString = '';
|
||||
|
||||
if ($parameter->type !== null) {
|
||||
$parameterAsString = $this->type($parameter->type) . ' ';
|
||||
}
|
||||
|
||||
$parameterAsString .= '$' . $parameter->var->name;
|
||||
|
||||
/* @todo Handle default values */
|
||||
|
||||
$parameters[] = $parameterAsString;
|
||||
}
|
||||
|
||||
$signature .= implode(', ', $parameters) . ')';
|
||||
|
||||
$returnType = $node->getReturnType();
|
||||
|
||||
if ($returnType !== null) {
|
||||
$signature .= ': ' . $this->type($returnType);
|
||||
}
|
||||
|
||||
return $signature;
|
||||
}
|
||||
|
||||
private function type(ComplexType|Identifier|Name $type): string
|
||||
{
|
||||
if ($type instanceof NullableType) {
|
||||
return '?' . $type->type;
|
||||
}
|
||||
|
||||
if ($type instanceof UnionType) {
|
||||
return $this->unionTypeAsString($type);
|
||||
}
|
||||
|
||||
if ($type instanceof IntersectionType) {
|
||||
return $this->intersectionTypeAsString($type);
|
||||
}
|
||||
|
||||
return $type->toString();
|
||||
}
|
||||
|
||||
private function visibility(ClassMethod $node): Visibility
|
||||
{
|
||||
if ($node->isPrivate()) {
|
||||
return Visibility::Private;
|
||||
}
|
||||
|
||||
if ($node->isProtected()) {
|
||||
return Visibility::Protected;
|
||||
}
|
||||
|
||||
return Visibility::Public;
|
||||
}
|
||||
|
||||
private function processInterface(Interface_ $node): void
|
||||
{
|
||||
$name = $node->name->toString();
|
||||
$namespacedName = $node->namespacedName->toString();
|
||||
$parentInterfaces = [];
|
||||
|
||||
foreach ($node->extends as $parentInterface) {
|
||||
$parentInterfaces[] = $parentInterface->toString();
|
||||
}
|
||||
|
||||
$this->interfaces[$namespacedName] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Interface_(
|
||||
$name,
|
||||
$namespacedName,
|
||||
$this->namespace($namespacedName, $name),
|
||||
$node->getStartLine(),
|
||||
$node->getEndLine(),
|
||||
$parentInterfaces,
|
||||
);
|
||||
}
|
||||
|
||||
private function processClass(Class_|Enum_ $node): void
|
||||
{
|
||||
$name = $node->name->toString();
|
||||
$namespacedName = $node->namespacedName->toString();
|
||||
$parentClass = null;
|
||||
$interfaces = [];
|
||||
|
||||
if (!$node instanceof Enum_) {
|
||||
if ($node->extends instanceof Name) {
|
||||
$parentClass = $node->extends->toString();
|
||||
}
|
||||
|
||||
foreach ($node->implements as $interface) {
|
||||
$interfaces[] = $interface->toString();
|
||||
}
|
||||
}
|
||||
|
||||
$this->classes[$namespacedName] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Class_(
|
||||
$name,
|
||||
$namespacedName,
|
||||
$this->namespace($namespacedName, $name),
|
||||
$this->file,
|
||||
$node->getStartLine(),
|
||||
$node->getEndLine(),
|
||||
$parentClass,
|
||||
$interfaces,
|
||||
[],
|
||||
$this->processMethods($node->getMethods()),
|
||||
);
|
||||
}
|
||||
|
||||
private function processTrait(Trait_ $node): void
|
||||
{
|
||||
$name = $node->name->toString();
|
||||
$namespacedName = $node->namespacedName->toString();
|
||||
|
||||
$this->traits[$namespacedName] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_(
|
||||
$name,
|
||||
$namespacedName,
|
||||
$this->namespace($namespacedName, $name),
|
||||
$this->file,
|
||||
$node->getStartLine(),
|
||||
$node->getEndLine(),
|
||||
[],
|
||||
$this->processMethods($node->getMethods()),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<ClassMethod> $nodes
|
||||
*
|
||||
* @return array<non-empty-string, Method>
|
||||
*/
|
||||
private function processMethods(array $nodes): array
|
||||
{
|
||||
$methods = [];
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
$methods[$node->name->toString()] = new Method(
|
||||
$node->name->toString(),
|
||||
$node->getStartLine(),
|
||||
$node->getEndLine(),
|
||||
$this->signature($node),
|
||||
$this->visibility($node),
|
||||
$this->cyclomaticComplexity($node),
|
||||
);
|
||||
}
|
||||
|
||||
return $methods;
|
||||
}
|
||||
|
||||
private function processFunction(Function_ $node): void
|
||||
{
|
||||
assert(isset($node->name));
|
||||
assert(isset($node->namespacedName));
|
||||
assert($node->namespacedName instanceof Name);
|
||||
|
||||
$name = $node->name->toString();
|
||||
$namespacedName = $node->namespacedName->toString();
|
||||
|
||||
$this->functions[$namespacedName] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Function_(
|
||||
$name,
|
||||
$namespacedName,
|
||||
$this->namespace($namespacedName, $name),
|
||||
$node->getStartLine(),
|
||||
$node->getEndLine(),
|
||||
$this->signature($node),
|
||||
$this->cyclomaticComplexity($node),
|
||||
);
|
||||
}
|
||||
|
||||
private function namespace(string $namespacedName, string $name): string
|
||||
{
|
||||
return trim(rtrim($namespacedName, $name), '\\');
|
||||
}
|
||||
|
||||
private function unionTypeAsString(UnionType $node): string
|
||||
{
|
||||
$types = [];
|
||||
|
||||
foreach ($node->types as $type) {
|
||||
if ($type instanceof IntersectionType) {
|
||||
$types[] = '(' . $this->intersectionTypeAsString($type) . ')';
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$types[] = $this->typeAsString($type);
|
||||
}
|
||||
|
||||
return implode('|', $types);
|
||||
}
|
||||
|
||||
private function intersectionTypeAsString(IntersectionType $node): string
|
||||
{
|
||||
$types = [];
|
||||
|
||||
foreach ($node->types as $type) {
|
||||
$types[] = $this->typeAsString($type);
|
||||
}
|
||||
|
||||
return implode('&', $types);
|
||||
}
|
||||
|
||||
private function typeAsString(Identifier|Name $node): string
|
||||
{
|
||||
if ($node instanceof Name) {
|
||||
return $node->toCodeString();
|
||||
}
|
||||
|
||||
return $node->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<non-empty-string> $traits
|
||||
*/
|
||||
private function postProcessClassOrTrait(Class_|Enum_|Trait_ $node, array $traits): void
|
||||
{
|
||||
$name = $node->namespacedName->toString();
|
||||
|
||||
if ($node instanceof Class_ || $node instanceof Enum_) {
|
||||
assert(isset($this->classes[$name]));
|
||||
|
||||
$this->classes[$name] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Class_(
|
||||
$this->classes[$name]->name(),
|
||||
$this->classes[$name]->namespacedName(),
|
||||
$this->classes[$name]->namespace(),
|
||||
$this->classes[$name]->file(),
|
||||
$this->classes[$name]->startLine(),
|
||||
$this->classes[$name]->endLine(),
|
||||
$this->classes[$name]->parentClass(),
|
||||
$this->classes[$name]->interfaces(),
|
||||
$traits,
|
||||
$this->classes[$name]->methods(),
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
assert(isset($this->traits[$name]));
|
||||
|
||||
$this->traits[$name] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_(
|
||||
$this->traits[$name]->name(),
|
||||
$this->traits[$name]->namespacedName(),
|
||||
$this->traits[$name]->namespace(),
|
||||
$this->traits[$name]->file(),
|
||||
$this->traits[$name]->startLine(),
|
||||
$this->traits[$name]->endLine(),
|
||||
$traits,
|
||||
$this->traits[$name]->methods(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+408
@@ -0,0 +1,408 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function array_diff_key;
|
||||
use function assert;
|
||||
use function count;
|
||||
use function current;
|
||||
use function end;
|
||||
use function explode;
|
||||
use function max;
|
||||
use function preg_match;
|
||||
use function preg_quote;
|
||||
use function range;
|
||||
use function reset;
|
||||
use function sprintf;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*
|
||||
* @phpstan-import-type LinesType from AnalysisResult
|
||||
*/
|
||||
final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
private int $nextBranch = 0;
|
||||
private readonly string $source;
|
||||
|
||||
/**
|
||||
* @var LinesType
|
||||
*/
|
||||
private array $executableLinesGroupedByBranch = [];
|
||||
|
||||
/**
|
||||
* @var array<int, bool>
|
||||
*/
|
||||
private array $unsets = [];
|
||||
|
||||
/**
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private array $commentsToCheckForUnset = [];
|
||||
|
||||
public function __construct(string $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): null
|
||||
{
|
||||
foreach ($node->getComments() as $comment) {
|
||||
$commentLine = $comment->getStartLine();
|
||||
|
||||
if (!isset($this->executableLinesGroupedByBranch[$commentLine])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (explode("\n", $comment->getText()) as $text) {
|
||||
$this->commentsToCheckForUnset[$commentLine] = $text;
|
||||
$commentLine++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Scalar\String_ ||
|
||||
$node instanceof Node\Scalar\EncapsedStringPart) {
|
||||
$startLine = $node->getStartLine() + 1;
|
||||
$endLine = $node->getEndLine() - 1;
|
||||
|
||||
if ($startLine <= $endLine) {
|
||||
foreach (range($startLine, $endLine) as $line) {
|
||||
unset($this->executableLinesGroupedByBranch[$line]);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Interface_ ||
|
||||
$node instanceof Node\Attribute
|
||||
) {
|
||||
foreach (range($node->getStartLine(), $node->getEndLine()) as $line) {
|
||||
$this->unsets[$line] = true;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Declare_ ||
|
||||
$node instanceof Node\Stmt\DeclareDeclare ||
|
||||
$node instanceof Node\Stmt\Else_ ||
|
||||
$node instanceof Node\Stmt\EnumCase ||
|
||||
$node instanceof Node\Stmt\Finally_ ||
|
||||
$node instanceof Node\Stmt\GroupUse ||
|
||||
$node instanceof Node\Stmt\Label ||
|
||||
$node instanceof Node\Stmt\Namespace_ ||
|
||||
$node instanceof Node\Stmt\Nop ||
|
||||
$node instanceof Node\Stmt\Switch_ ||
|
||||
$node instanceof Node\Stmt\TryCatch ||
|
||||
$node instanceof Node\Stmt\Use_ ||
|
||||
$node instanceof Node\Stmt\UseUse ||
|
||||
$node instanceof Node\Expr\ConstFetch ||
|
||||
$node instanceof Node\Expr\Variable ||
|
||||
$node instanceof Node\Expr\Throw_ ||
|
||||
$node instanceof Node\ComplexType ||
|
||||
$node instanceof Node\Const_ ||
|
||||
$node instanceof Node\Identifier ||
|
||||
$node instanceof Node\Name ||
|
||||
$node instanceof Node\Param ||
|
||||
$node instanceof Node\Scalar) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\Match_) {
|
||||
foreach ($node->arms as $arm) {
|
||||
$this->setLineBranch(
|
||||
$arm->body->getStartLine(),
|
||||
$arm->body->getEndLine(),
|
||||
++$this->nextBranch,
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Expression && $node->expr instanceof Node\Expr\Throw_) {
|
||||
$this->setLineBranch($node->expr->expr->getEndLine(), $node->expr->expr->getEndLine(), ++$this->nextBranch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Enum_ ||
|
||||
$node instanceof Node\Stmt\Function_ ||
|
||||
$node instanceof Node\Stmt\Class_ ||
|
||||
$node instanceof Node\Stmt\ClassMethod ||
|
||||
$node instanceof Node\Expr\Closure ||
|
||||
$node instanceof Node\Stmt\Trait_) {
|
||||
if ($node instanceof Node\Stmt\ClassMethod && $node->isAbstract()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) {
|
||||
$unsets = [];
|
||||
|
||||
foreach ($node->getParams() as $param) {
|
||||
foreach (range($param->getStartLine(), $param->getEndLine()) as $line) {
|
||||
$unsets[$line] = true;
|
||||
}
|
||||
}
|
||||
|
||||
unset($unsets[$node->getEndLine()]);
|
||||
|
||||
$this->unsets += $unsets;
|
||||
}
|
||||
|
||||
$isConcreteClassLike = $node instanceof Node\Stmt\Enum_ || $node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Trait_;
|
||||
|
||||
if (null !== $node->stmts) {
|
||||
foreach ($node->stmts as $stmt) {
|
||||
if ($stmt instanceof Node\Stmt\Nop) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (range($stmt->getStartLine(), $stmt->getEndLine()) as $line) {
|
||||
unset($this->executableLinesGroupedByBranch[$line]);
|
||||
|
||||
if (
|
||||
$isConcreteClassLike &&
|
||||
!$stmt instanceof Node\Stmt\ClassMethod
|
||||
) {
|
||||
$this->unsets[$line] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($isConcreteClassLike) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$hasEmptyBody = [] === $node->stmts ||
|
||||
null === $node->stmts ||
|
||||
(
|
||||
1 === count($node->stmts) &&
|
||||
$node->stmts[0] instanceof Node\Stmt\Nop
|
||||
);
|
||||
|
||||
if ($hasEmptyBody) {
|
||||
if ($node->getEndLine() === $node->getStartLine() && isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setLineBranch($node->getEndLine(), $node->getEndLine(), ++$this->nextBranch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\ArrowFunction) {
|
||||
$startLine = max(
|
||||
$node->getStartLine() + 1,
|
||||
$node->expr->getStartLine(),
|
||||
);
|
||||
|
||||
$endLine = $node->expr->getEndLine();
|
||||
|
||||
if ($endLine < $startLine) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setLineBranch($startLine, $endLine, ++$this->nextBranch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\Ternary) {
|
||||
if (null !== $node->if &&
|
||||
$node->getStartLine() !== $node->if->getEndLine()) {
|
||||
$this->setLineBranch($node->if->getStartLine(), $node->if->getEndLine(), ++$this->nextBranch);
|
||||
}
|
||||
|
||||
if ($node->getStartLine() !== $node->else->getEndLine()) {
|
||||
$this->setLineBranch($node->else->getStartLine(), $node->else->getEndLine(), ++$this->nextBranch);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\BinaryOp\Coalesce) {
|
||||
if ($node->getStartLine() !== $node->getEndLine()) {
|
||||
$this->setLineBranch($node->getEndLine(), $node->getEndLine(), ++$this->nextBranch);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\If_ ||
|
||||
$node instanceof Node\Stmt\ElseIf_ ||
|
||||
$node instanceof Node\Stmt\Case_) {
|
||||
if (null === $node->cond) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setLineBranch(
|
||||
$node->cond->getStartLine(),
|
||||
$node->cond->getStartLine(),
|
||||
++$this->nextBranch,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\For_) {
|
||||
$startLine = null;
|
||||
$endLine = null;
|
||||
|
||||
if ([] !== $node->init) {
|
||||
$startLine = $node->init[0]->getStartLine();
|
||||
|
||||
end($node->init);
|
||||
|
||||
$endLine = current($node->init)->getEndLine();
|
||||
|
||||
reset($node->init);
|
||||
}
|
||||
|
||||
if ([] !== $node->cond) {
|
||||
if (null === $startLine) {
|
||||
$startLine = $node->cond[0]->getStartLine();
|
||||
}
|
||||
|
||||
end($node->cond);
|
||||
|
||||
$endLine = current($node->cond)->getEndLine();
|
||||
|
||||
reset($node->cond);
|
||||
}
|
||||
|
||||
if ([] !== $node->loop) {
|
||||
if (null === $startLine) {
|
||||
$startLine = $node->loop[0]->getStartLine();
|
||||
}
|
||||
|
||||
end($node->loop);
|
||||
|
||||
$endLine = current($node->loop)->getEndLine();
|
||||
|
||||
reset($node->loop);
|
||||
}
|
||||
|
||||
if (null === $startLine || null === $endLine) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setLineBranch(
|
||||
$startLine,
|
||||
$endLine,
|
||||
++$this->nextBranch,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Foreach_) {
|
||||
$this->setLineBranch(
|
||||
$node->expr->getStartLine(),
|
||||
$node->valueVar->getEndLine(),
|
||||
++$this->nextBranch,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\While_ ||
|
||||
$node instanceof Node\Stmt\Do_) {
|
||||
$this->setLineBranch(
|
||||
$node->cond->getStartLine(),
|
||||
$node->cond->getEndLine(),
|
||||
++$this->nextBranch,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Stmt\Catch_) {
|
||||
assert([] !== $node->types);
|
||||
$startLine = $node->types[0]->getStartLine();
|
||||
end($node->types);
|
||||
$endLine = current($node->types)->getEndLine();
|
||||
|
||||
$this->setLineBranch(
|
||||
$startLine,
|
||||
$endLine,
|
||||
++$this->nextBranch,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\CallLike) {
|
||||
if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
|
||||
$branch = $this->executableLinesGroupedByBranch[$node->getStartLine()];
|
||||
} else {
|
||||
$branch = ++$this->nextBranch;
|
||||
}
|
||||
|
||||
$this->setLineBranch($node->getStartLine(), $node->getEndLine(), $branch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->setLineBranch($node->getStartLine(), $node->getEndLine(), ++$this->nextBranch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function afterTraverse(array $nodes): null
|
||||
{
|
||||
$lines = explode("\n", $this->source);
|
||||
|
||||
foreach ($lines as $lineNumber => $line) {
|
||||
$lineNumber++;
|
||||
|
||||
if (1 === preg_match('/^\s*$/', $line) ||
|
||||
(
|
||||
isset($this->commentsToCheckForUnset[$lineNumber]) &&
|
||||
1 === preg_match(sprintf('/^\s*%s\s*$/', preg_quote($this->commentsToCheckForUnset[$lineNumber], '/')), $line)
|
||||
)) {
|
||||
unset($this->executableLinesGroupedByBranch[$lineNumber]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->executableLinesGroupedByBranch = array_diff_key(
|
||||
$this->executableLinesGroupedByBranch,
|
||||
$this->unsets,
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LinesType
|
||||
*/
|
||||
public function executableLinesGroupedByBranch(): array
|
||||
{
|
||||
return $this->executableLinesGroupedByBranch;
|
||||
}
|
||||
|
||||
private function setLineBranch(int $start, int $end, int $branch): void
|
||||
{
|
||||
foreach (range($start, $end) as $line) {
|
||||
$this->executableLinesGroupedByBranch[$line] = $branch;
|
||||
}
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-code-coverage.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
|
||||
|
||||
use function assert;
|
||||
use function str_contains;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Attribute;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Enum_;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use PhpParser\Node\Stmt\Trait_;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
|
||||
/**
|
||||
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
||||
*/
|
||||
final class IgnoredLinesFindingVisitor extends NodeVisitorAbstract
|
||||
{
|
||||
/**
|
||||
* @var array<int>
|
||||
*/
|
||||
private array $ignoredLines = [];
|
||||
private readonly bool $useAnnotationsForIgnoringCode;
|
||||
private readonly bool $ignoreDeprecated;
|
||||
|
||||
public function __construct(bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecated)
|
||||
{
|
||||
$this->useAnnotationsForIgnoringCode = $useAnnotationsForIgnoringCode;
|
||||
$this->ignoreDeprecated = $ignoreDeprecated;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): null
|
||||
{
|
||||
if (!$node instanceof Class_ &&
|
||||
!$node instanceof Trait_ &&
|
||||
!$node instanceof Interface_ &&
|
||||
!$node instanceof Enum_ &&
|
||||
!$node instanceof ClassMethod &&
|
||||
!$node instanceof Function_ &&
|
||||
!$node instanceof Attribute) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Class_ && $node->isAnonymous()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Class_ ||
|
||||
$node instanceof Trait_ ||
|
||||
$node instanceof Interface_ ||
|
||||
$node instanceof Attribute) {
|
||||
$this->ignoredLines[] = $node->getStartLine();
|
||||
|
||||
assert($node->name !== null);
|
||||
|
||||
// Workaround for https://github.com/nikic/PHP-Parser/issues/886
|
||||
$this->ignoredLines[] = $node->name->getStartLine();
|
||||
}
|
||||
|
||||
if (!$this->useAnnotationsForIgnoringCode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Interface_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node instanceof Attribute &&
|
||||
$node->name->toString() === 'PHPUnit\Framework\Attributes\CodeCoverageIgnore') {
|
||||
$attributeGroup = $node->getAttribute('parent');
|
||||
$attributedNode = $attributeGroup->getAttribute('parent');
|
||||
|
||||
for ($line = $attributedNode->getStartLine(); $line <= $attributedNode->getEndLine(); $line++) {
|
||||
$this->ignoredLines[] = $line;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->processDocComment($node);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int>
|
||||
*/
|
||||
public function ignoredLines(): array
|
||||
{
|
||||
return $this->ignoredLines;
|
||||
}
|
||||
|
||||
private function processDocComment(Node $node): void
|
||||
{
|
||||
$docComment = $node->getDocComment();
|
||||
|
||||
if ($docComment === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (str_contains($docComment->getText(), '@codeCoverageIgnore')) {
|
||||
for ($line = $node->getStartLine(); $line <= $node->getEndLine(); $line++) {
|
||||
$this->ignoredLines[] = $line;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->ignoreDeprecated && str_contains($docComment->getText(), '@deprecated')) {
|
||||
for ($line = $node->getStartLine(); $line <= $node->getEndLine(); $line++) {
|
||||
$this->ignoredLines[] = $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user