Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"ext-tokenizer": "*",
"entropy/entropy": "^0.4.23",
"nikic/php-parser": "^5.9",
"sebastian/lines-of-code": "^4.0",
"webmozart/assert": "^2.4"
},
"require-dev": {
Expand Down
16 changes: 7 additions & 9 deletions src/Lines/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpParser\NodeTraverser;
use PhpParser\Parser;
use Rector\SwissKnife\Lines\NodeVisitor\CommentLineCountingNodeVisitor;
use Rector\SwissKnife\Lines\NodeVisitor\StructureNodeVisitor;
use SebastianBergmann\LinesOfCode\Counter;
use Throwable;
use Webmozart\Assert\Assert;

Expand All @@ -18,7 +18,6 @@
{
public function __construct(
private Parser $parser,
private Counter $counter,
) {
}

Expand Down Expand Up @@ -63,17 +62,16 @@ private function measureFile(Measurements $measurements, string $filePath): void

$measurements->addFile($filePath);

// measure structure
// measure structure and comment lines in a single traversal
$commentLineCountingNodeVisitor = new CommentLineCountingNodeVisitor();

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new StructureNodeVisitor($measurements));
$nodeTraverser->addVisitor($commentLineCountingNodeVisitor);
$nodeTraverser->traverse($stmts);

// measure lines of code
$initLinesOfCode = $this->resolveInitLinesOfCode($fileContents);
$linesOfCode = $this->counter->countInAbstractSyntaxTree($initLinesOfCode, $stmts);

$measurements->incrementLines($linesOfCode->linesOfCode());
$measurements->incrementCommentLines($linesOfCode->commentLinesOfCode());
$measurements->incrementLines($this->resolveInitLinesOfCode($fileContents));
$measurements->incrementCommentLines($commentLineCountingNodeVisitor->getCommentLineCount());
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/Lines/NodeVisitor/CommentLineCountingNodeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Lines\NodeVisitor;

use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;

final class CommentLineCountingNodeVisitor extends NodeVisitorAbstract
{
/**
* @var Comment[]
*/
private array $comments = [];

public function enterNode(Node $node): ?Node
{
$this->comments = array_merge($this->comments, $node->getComments());

return null;
}

public function getCommentLineCount(): int
{
$commentLines = [];

foreach ($this->uniqueComments() as $comment) {
for ($line = $comment->getStartLine(); $line <= $comment->getEndLine(); ++$line) {
$commentLines[$line] = true;
}
}

return count($commentLines);
}

/**
* @return Comment[]
*/
private function uniqueComments(): array
{
$uniqueComments = [];

foreach ($this->comments as $comment) {
$key = $comment->getStartLine() . '_' . $comment->getStartTokenPos()
. '_' . $comment->getEndLine() . '_' . $comment->getEndTokenPos();
$uniqueComments[$key] = $comment;
}

return $uniqueComments;
}
}
Loading