Skip to content

Commit 081faa0

Browse files
Fix #793
1 parent 4abbce3 commit 081faa0

6 files changed

+66
-25
lines changed

ChangeLog.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
44

5+
## [9.1.4] - 2020-MM-DD
6+
7+
### Fixed
8+
9+
* [#793](https://github.com/sebastianbergmann/php-code-coverage/issues/793): Lines with `::class` constant are not covered
10+
511
## [9.1.3] - 2020-08-10
612

713
### Changed
@@ -213,6 +219,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
213219
* Class names are now abbreviated (unqualified name shown, fully qualified name shown on hover) in the file view of the HTML report
214220
* Update HTML report to Bootstrap 4
215221

222+
[9.1.4]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.1.3...master
216223
[9.1.3]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.1.2...9.1.3
217224
[9.1.2]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.1.1...9.1.2
218225
[9.1.1]: https://github.com/sebastianbergmann/php-code-coverage/compare/9.1.0...9.1.1

src/StaticAnalysis/IgnoredLinesFindingVisitor.php

+25-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpParser\Node\Stmt\Class_;
1717
use PhpParser\Node\Stmt\ClassMethod;
1818
use PhpParser\Node\Stmt\Function_;
19+
use PhpParser\Node\Stmt\Interface_;
1920
use PhpParser\Node\Stmt\Trait_;
2021
use PhpParser\NodeTraverser;
2122
use PhpParser\NodeVisitorAbstract;
@@ -27,20 +28,27 @@ final class IgnoredLinesFindingVisitor extends NodeVisitorAbstract
2728
*/
2829
private $ignoredLines = [];
2930

31+
/**
32+
* @var bool
33+
*/
34+
private $useAnnotationsForIgnoringCode;
35+
3036
/**
3137
* @var bool
3238
*/
3339
private $ignoreDeprecated;
3440

35-
public function __construct(bool $ignoreDeprecated)
41+
public function __construct(bool $useAnnotationsForIgnoringCode, bool $ignoreDeprecated)
3642
{
37-
$this->ignoreDeprecated = $ignoreDeprecated;
43+
$this->useAnnotationsForIgnoringCode = $useAnnotationsForIgnoringCode;
44+
$this->ignoreDeprecated = $ignoreDeprecated;
3845
}
3946

4047
public function enterNode(Node $node): ?int
4148
{
4249
if (!$node instanceof Class_ &&
4350
!$node instanceof Trait_ &&
51+
!$node instanceof Interface_ &&
4452
!$node instanceof ClassMethod &&
4553
!$node instanceof Function_) {
4654
return null;
@@ -50,6 +58,21 @@ public function enterNode(Node $node): ?int
5058
return null;
5159
}
5260

61+
// Workaround for https://bugs.xdebug.org/view.php?id=1798
62+
if ($node instanceof Class_ ||
63+
$node instanceof Trait_ ||
64+
$node instanceof Interface_) {
65+
$this->ignoredLines[] = $node->getStartLine();
66+
}
67+
68+
if (!$this->useAnnotationsForIgnoringCode) {
69+
return null;
70+
}
71+
72+
if ($node instanceof Interface_) {
73+
return null;
74+
}
75+
5376
$docComment = $node->getDocComment();
5477

5578
if ($docComment === null) {

src/StaticAnalysis/ParsingCoveredFileAnalyser.php

+9-23
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,16 @@ private function analyse(string $filename): void
127127

128128
assert($nodes !== null);
129129

130-
$traverser = new NodeTraverser;
131-
$codeUnitFindingVisitor = new CodeUnitFindingVisitor;
132-
$lineCountingVisitor = new LineCountingVisitor($linesOfCode);
130+
$traverser = new NodeTraverser;
131+
$codeUnitFindingVisitor = new CodeUnitFindingVisitor;
132+
$lineCountingVisitor = new LineCountingVisitor($linesOfCode);
133+
$ignoredLinesFindingVisitor = new IgnoredLinesFindingVisitor($this->useAnnotationsForIgnoringCode, $this->ignoreDeprecatedCode);
133134

134135
$traverser->addVisitor(new NameResolver);
135136
$traverser->addVisitor(new ParentConnectingVisitor);
136137
$traverser->addVisitor($codeUnitFindingVisitor);
137138
$traverser->addVisitor($lineCountingVisitor);
138-
139-
if ($this->useAnnotationsForIgnoringCode) {
140-
$ignoredLinesFindingVisitor = new IgnoredLinesFindingVisitor($this->ignoreDeprecatedCode);
141-
142-
$traverser->addVisitor($ignoredLinesFindingVisitor);
143-
}
139+
$traverser->addVisitor($ignoredLinesFindingVisitor);
144140

145141
/* @noinspection UnusedFunctionResultInspection */
146142
$traverser->traverse($nodes);
@@ -166,14 +162,12 @@ private function analyse(string $filename): void
166162

167163
$this->findLinesIgnoredByLineBasedAnnotations($filename, $source, $this->useAnnotationsForIgnoringCode);
168164

169-
if (isset($ignoredLinesFindingVisitor)) {
170-
$this->ignoredLines[$filename] = array_merge(
165+
$this->ignoredLines[$filename] = array_unique(
166+
array_merge(
171167
$this->ignoredLines[$filename],
172168
$ignoredLinesFindingVisitor->ignoredLines()
173-
);
174-
}
175-
176-
$this->ignoredLines[$filename] = array_unique($this->ignoredLines[$filename]);
169+
)
170+
);
177171

178172
sort($this->ignoredLines[$filename]);
179173
}
@@ -209,14 +203,6 @@ private function findLinesIgnoredByLineBasedAnnotations(string $filename, string
209203
$stop = true;
210204
}
211205

212-
break;
213-
214-
case T_INTERFACE:
215-
case T_TRAIT:
216-
case T_CLASS:
217-
// Workaround for https://bugs.xdebug.org/view.php?id=1798
218-
$this->ignoredLines[$filename][] = $token[2];
219-
220206
break;
221207
}
222208

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
class CoveredClassFullyQualifiedClassNameConstant
3+
{
4+
public function method(): string
5+
{
6+
return self::class;
7+
}
8+
}

tests/tests/FilterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ protected function setUp(): void
7272
TEST_FILES_PATH . 'NamespacedBankAccount.php',
7373
TEST_FILES_PATH . 'NotExistingCoveredElementTest.php',
7474
TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php',
75+
TEST_FILES_PATH . 'source_with_class_and_fqcn_constant.php',
7576
TEST_FILES_PATH . 'source_with_empty_class.php',
7677
TEST_FILES_PATH . 'source_with_ignore.php',
7778
TEST_FILES_PATH . 'source_with_interface.php',

tests/tests/StaticAnalysis/CoveredFileAnalyserTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public function testGetLinesToBeIgnored3(): void
6666
);
6767
}
6868

69+
/**
70+
* @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/793
71+
* @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/794
72+
*/
73+
public function testLineWithFullyQualifiedClassNameConstantIsNotIgnored(): void
74+
{
75+
$this->assertSame(
76+
[
77+
2,
78+
],
79+
(new ParsingCoveredFileAnalyser(true, true))->ignoredLinesFor(
80+
TEST_FILES_PATH . 'source_with_class_and_fqcn_constant.php'
81+
)
82+
);
83+
}
84+
6985
public function testGetLinesToBeIgnoredOneLineAnnotations(): void
7086
{
7187
$this->assertEquals(

0 commit comments

Comments
 (0)