| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of phpunit/php-code-coverage.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
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\StaticAnalysis;
|
|
|
11 |
|
|
|
12 |
use function array_merge;
|
|
|
13 |
use function assert;
|
|
|
14 |
use function range;
|
|
|
15 |
use function strpos;
|
|
|
16 |
use PhpParser\Node;
|
|
|
17 |
use PhpParser\Node\Attribute;
|
|
|
18 |
use PhpParser\Node\Stmt\Class_;
|
|
|
19 |
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
20 |
use PhpParser\Node\Stmt\Function_;
|
|
|
21 |
use PhpParser\Node\Stmt\Interface_;
|
|
|
22 |
use PhpParser\Node\Stmt\Trait_;
|
|
|
23 |
use PhpParser\NodeVisitorAbstract;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
|
|
|
27 |
*/
|
|
|
28 |
final class IgnoredLinesFindingVisitor extends NodeVisitorAbstract
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* @psalm-var list<int>
|
|
|
32 |
*/
|
|
|
33 |
private $ignoredLines = [];
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @var bool
|
|
|
37 |
*/
|
|
|
38 |
private $useAnnotationsForIgnoringCode;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @var bool
|
|
|
42 |
*/
|
|
|
43 |
private $ignoreDeprecated;
|
|
|
44 |
|
|
|
45 |
public function __construct(bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecated)
|
|
|
46 |
{
|
|
|
47 |
$this->useAnnotationsForIgnoringCode = $useAnnotationsForIgnoringCode;
|
|
|
48 |
$this->ignoreDeprecated = $ignoreDeprecated;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function enterNode(Node $node): void
|
|
|
52 |
{
|
|
|
53 |
if (!$node instanceof Class_ &&
|
|
|
54 |
!$node instanceof Trait_ &&
|
|
|
55 |
!$node instanceof Interface_ &&
|
|
|
56 |
!$node instanceof ClassMethod &&
|
|
|
57 |
!$node instanceof Function_ &&
|
|
|
58 |
!$node instanceof Attribute) {
|
|
|
59 |
return;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if ($node instanceof Class_ && $node->isAnonymous()) {
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if ($node instanceof Class_ ||
|
|
|
67 |
$node instanceof Trait_ ||
|
|
|
68 |
$node instanceof Interface_ ||
|
|
|
69 |
$node instanceof Attribute) {
|
|
|
70 |
$this->ignoredLines[] = $node->getStartLine();
|
|
|
71 |
|
|
|
72 |
assert($node->name !== null);
|
|
|
73 |
|
|
|
74 |
// Workaround for https://github.com/nikic/PHP-Parser/issues/886
|
|
|
75 |
$this->ignoredLines[] = $node->name->getStartLine();
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if (!$this->useAnnotationsForIgnoringCode) {
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if ($node instanceof Interface_) {
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$this->processDocComment($node);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* @psalm-return list<int>
|
|
|
91 |
*/
|
|
|
92 |
public function ignoredLines(): array
|
|
|
93 |
{
|
|
|
94 |
return $this->ignoredLines;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private function processDocComment(Node $node): void
|
|
|
98 |
{
|
|
|
99 |
$docComment = $node->getDocComment();
|
|
|
100 |
|
|
|
101 |
if ($docComment === null) {
|
|
|
102 |
return;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
if (strpos($docComment->getText(), '@codeCoverageIgnore') !== false) {
|
|
|
106 |
$this->ignoredLines = array_merge(
|
|
|
107 |
$this->ignoredLines,
|
|
|
108 |
range($node->getStartLine(), $node->getEndLine())
|
|
|
109 |
);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if ($this->ignoreDeprecated && strpos($docComment->getText(), '@deprecated') !== false) {
|
|
|
113 |
$this->ignoredLines = array_merge(
|
|
|
114 |
$this->ignoredLines,
|
|
|
115 |
range($node->getStartLine(), $node->getEndLine())
|
|
|
116 |
);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|