Skip to content

Commit a3ea1ff

Browse files
Extract IgnoredLinesFindingVisitor from IgnoredLinesFinder
1 parent c1c9356 commit a3ea1ff

File tree

2 files changed

+70
-53
lines changed

2 files changed

+70
-53
lines changed

src/IgnoredLinesFinder.php

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,11 @@
1818
use function array_unique;
1919
use function file_get_contents;
2020
use function is_array;
21-
use function range;
2221
use function sort;
23-
use function strpos;
2422
use function token_get_all;
2523
use function trim;
26-
use PhpParser\Builder\Trait_;
2724
use PhpParser\Error;
28-
use PhpParser\Node;
29-
use PhpParser\Node\Stmt\Class_;
30-
use PhpParser\Node\Stmt\ClassMethod;
31-
use PhpParser\Node\Stmt\Function_;
3225
use PhpParser\NodeTraverser;
33-
use PhpParser\NodeVisitorAbstract;
3426
use PhpParser\ParserFactory;
3527

3628
final class IgnoredLinesFinder
@@ -124,51 +116,7 @@ private function findLinesIgnoredByDocBlockAnnotations(string $filename, bool $i
124116
assert($nodes !== null);
125117

126118
$traverser = new NodeTraverser;
127-
128-
$visitor = new class($ignoreDeprecatedCode) extends NodeVisitorAbstract {
129-
/**
130-
* @psalm-var list<int>
131-
*/
132-
private $ignoredLines = [];
133-
134-
private $ignoreDeprecated;
135-
136-
public function __construct(bool $ignoreDeprecated)
137-
{
138-
$this->ignoreDeprecated = $ignoreDeprecated;
139-
}
140-
141-
public function enterNode(Node $node): void
142-
{
143-
if (!$node instanceof Class_ &&
144-
!$node instanceof Trait_ &&
145-
!$node instanceof ClassMethod &&
146-
!$node instanceof Function_) {
147-
return;
148-
}
149-
150-
$docComment = $node->getDocComment();
151-
152-
if ($docComment === null) {
153-
return;
154-
}
155-
156-
if (strpos($docComment->getText(), '@codeCoverageIgnore') !== false) {
157-
$this->ignoredLines = array_merge(
158-
$this->ignoredLines,
159-
range($node->getStartLine(), $node->getEndLine())
160-
);
161-
}
162-
}
163-
164-
/**
165-
* @psalm-return list<int>
166-
*/
167-
public function ignoredLines(): array
168-
{
169-
return $this->ignoredLines;
170-
}
171-
};
119+
$visitor = new IgnoredLinesFindingVisitor($ignoreDeprecatedCode);
172120

173121
$traverser->addVisitor($visitor);
174122

src/IgnoredLinesFindingVisitor.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage;
11+
12+
use function array_merge;
13+
use function range;
14+
use function strpos;
15+
use PhpParser\Builder\Trait_;
16+
use PhpParser\Node;
17+
use PhpParser\Node\Stmt\Class_;
18+
use PhpParser\Node\Stmt\ClassMethod;
19+
use PhpParser\Node\Stmt\Function_;
20+
use PhpParser\NodeVisitorAbstract;
21+
22+
final class IgnoredLinesFindingVisitor extends NodeVisitorAbstract
23+
{
24+
/**
25+
* @psalm-var list<int>
26+
*/
27+
private $ignoredLines = [];
28+
29+
/**
30+
* @var bool
31+
*/
32+
private $ignoreDeprecated;
33+
34+
public function __construct(bool $ignoreDeprecated)
35+
{
36+
$this->ignoreDeprecated = $ignoreDeprecated;
37+
}
38+
39+
public function enterNode(Node $node): void
40+
{
41+
if (!$node instanceof Class_ &&
42+
!$node instanceof Trait_ &&
43+
!$node instanceof ClassMethod &&
44+
!$node instanceof Function_) {
45+
return;
46+
}
47+
48+
$docComment = $node->getDocComment();
49+
50+
if ($docComment === null) {
51+
return;
52+
}
53+
54+
if (strpos($docComment->getText(), '@codeCoverageIgnore') !== false) {
55+
$this->ignoredLines = array_merge(
56+
$this->ignoredLines,
57+
range($node->getStartLine(), $node->getEndLine())
58+
);
59+
}
60+
}
61+
62+
/**
63+
* @psalm-return list<int>
64+
*/
65+
public function ignoredLines(): array
66+
{
67+
return $this->ignoredLines;
68+
}
69+
}

0 commit comments

Comments
 (0)