class LineCountingVisitor
Hierarchy
- class \PhpParser\NodeVisitorAbstract implements \PhpParser\NodeVisitor
- class \SebastianBergmann\LinesOfCode\LineCountingVisitor extends \PhpParser\NodeVisitorAbstract
Expanded class hierarchy of LineCountingVisitor
1 file declares its use of LineCountingVisitor
- ParsingFileAnalyser.php in vendor/
phpunit/ php-code-coverage/ src/ StaticAnalysis/ ParsingFileAnalyser.php
File
-
vendor/
sebastian/ lines-of-code/ src/ LineCountingVisitor.php, line 21
Namespace
SebastianBergmann\LinesOfCodeView source
final class LineCountingVisitor extends NodeVisitorAbstract {
/**
* @psalm-var non-negative-int
*/
private readonly int $linesOfCode;
/**
* @var Comment[]
*/
private array $comments = [];
/**
* @var int[]
*/
private array $linesWithStatements = [];
/**
* @psalm-param non-negative-int $linesOfCode
*/
public function __construct(int $linesOfCode) {
$this->linesOfCode = $linesOfCode;
}
public function enterNode(Node $node) : void {
$this->comments = array_merge($this->comments, $node->getComments());
if (!$node instanceof Expr) {
return;
}
$this->linesWithStatements[] = $node->getStartLine();
}
public function result() : LinesOfCode {
$commentLinesOfCode = 0;
foreach ($this->comments() as $comment) {
$commentLinesOfCode += $comment->getEndLine() - $comment->getStartLine() + 1;
}
$nonCommentLinesOfCode = $this->linesOfCode - $commentLinesOfCode;
$logicalLinesOfCode = count(array_unique($this->linesWithStatements));
assert($commentLinesOfCode >= 0);
assert($nonCommentLinesOfCode >= 0);
assert($logicalLinesOfCode >= 0);
return new LinesOfCode($this->linesOfCode, $commentLinesOfCode, $nonCommentLinesOfCode, $logicalLinesOfCode);
}
/**
* @return Comment[]
*/
private function comments() : array {
$comments = [];
foreach ($this->comments as $comment) {
$comments[$comment->getStartLine() . '_' . $comment->getStartTokenPos() . '_' . $comment->getEndLine() . '_' . $comment->getEndTokenPos()] = $comment;
}
return $comments;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
LineCountingVisitor::$comments | private | property | |||
LineCountingVisitor::$linesOfCode | private | property | @psalm-var non-negative-int | ||
LineCountingVisitor::$linesWithStatements | private | property | |||
LineCountingVisitor::comments | private | function | |||
LineCountingVisitor::enterNode | public | function | Called when entering a node. | Overrides NodeVisitorAbstract::enterNode | |
LineCountingVisitor::result | public | function | |||
LineCountingVisitor::__construct | public | function | @psalm-param non-negative-int $linesOfCode | ||
NodeVisitor::DONT_TRAVERSE_CHILDREN | public | constant | If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes of the current node will not be traversed for any visitors. |
||
NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN | public | constant | If NodeVisitor::enterNode() returns DONT_TRAVERSE_CURRENT_AND_CHILDREN, child nodes of the current node will not be traversed for any visitors. |
||
NodeVisitor::REMOVE_NODE | public | constant | If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs in an array, it will be removed from the array. |
||
NodeVisitor::REPLACE_WITH_NULL | public | constant | If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns REPLACE_WITH_NULL, the node will be replaced with null. This is not a legal return value if the node is part of an array, rather than another node. |
||
NodeVisitor::STOP_TRAVERSAL | public | constant | If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns STOP_TRAVERSAL, traversal is aborted. |
||
NodeVisitorAbstract::afterTraverse | public | function | Called once after traversal. | Overrides NodeVisitor::afterTraverse | 1 |
NodeVisitorAbstract::beforeTraverse | public | function | Called once before traversal. | Overrides NodeVisitor::beforeTraverse | 5 |
NodeVisitorAbstract::leaveNode | public | function | Called when leaving a node. | Overrides NodeVisitor::leaveNode | 2 |