Skip to content

Commit 2f27ace

Browse files
author
Stephan Wentz
committed
fix: Add support for single-line docblocks with covers annotation
1 parent 56ee1cf commit 2f27ace

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@
4949
},
5050
"config": {
5151
"preferred-install": "dist",
52-
"sort-packages": true
52+
"sort-packages": true,
53+
"allow-plugins": {
54+
"dealerdirect/phpcodesniffer-composer-installer": true
55+
}
5356
},
5457
"extra": {
5558
"branch-alias": {

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ parameters:
99
paths:
1010
- src
1111
- tests
12-
excludes_analyse:
12+
excludePaths:
1313
- %currentWorkingDirectory%/tests/Fixture

src/CoversAnnotationRule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public function processNode(Node $node, Scope $scope): array
6363
$hasCovers = true;
6464
break;
6565
}
66+
67+
$lineHasCovers = (bool) preg_match('/^(?:\s*\/\*\*\s*@(?:covers|coversDefaultClass)\h+)\\\\?(?<className>\w[^:\s]*)(?:::\S+)?\s*\*\/\s*$/u', $lineContent, $matches);
68+
if ($lineHasCovers) {
69+
$hasCovers = true;
70+
break;
71+
}
6672
}
6773

6874
if ($isUnitTest && !$hasCovers) {

src/CoversExistsRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public function processNode(Node $node, Scope $scope): array
7171
$matches = [];
7272

7373
if (! preg_match('/^(?:\s*\*\s*@(?:covers|coversDefaultClass)\h+)\\\\?(?<className>\w[^:\s]*)(?:::\S+)?\s*$/u', $lineContent, $matches)) {
74-
continue;
74+
if (! preg_match('/^(?:\s*\/\*\*\s*@(?:covers|coversDefaultClass)\h+)\\\\?(?<className>\w[^:\s]*)(?:::\S+)?\s*\*\/\s*$/u', $lineContent, $matches)) {
75+
continue;
76+
}
7577
}
7678

7779
if ($this->broker->hasClass($matches['className'])) {

tests/CoversExistsRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testRule(): void
1919
$this->analyse([__DIR__ . '/Fixture/CoversExistsRule/fixture.php'], [
2020
['Class BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Invalid does not exist.', 32],
2121
['Class BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Invalid does not exist.', 33],
22+
['Class BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Invalid does not exist.', 58],
2223
]);
2324
}
2425

tests/Fixture/CoversAnnotationRule/Unit/fixture-with-unit.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@ final class WithInvalidCoversTest extends TestCase
3333
final class UnitWithValidCoversTest extends TestCase
3434
{
3535
}
36+
37+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversAnnotationRule\JustAClass */
38+
final class SingleLineClassUnitWithValidCoversTest extends TestCase
39+
{
40+
}
41+
42+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversAnnotationRule\JustAClass::test */
43+
final class SingleLineMethodUnitWithValidCoversTest extends TestCase
44+
{
45+
}
46+
47+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversAnnotationRule\Invalid::test */
48+
final class SingleLineMethodUnitWithInvalidCoversTest extends TestCase
49+
{
50+
}

tests/Fixture/CoversExistsRule/fixture.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ final class WithInvalidCoversTest extends TestCase
4444
final class UnitWithValidCoversTest extends TestCase
4545
{
4646
}
47+
48+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Valid */
49+
final class SingleLineValidClassUnitWithValidCoversTest extends TestCase
50+
{
51+
}
52+
53+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Valid::validMethod */
54+
final class SingleLineValidMethodUnitWithValidCoversTest extends TestCase
55+
{
56+
}
57+
58+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Invalid::invalid */
59+
final class SingleLineInvalidClassUnitWithInvalidCoversTest extends TestCase
60+
{
61+
}
62+
63+
/** @covers \BrainbitsPhpStan\Tests\Fixture\CoversExistsRule\Valid::invalidMethod */
64+
final class SingleLineInvalidMethodUnitWithValidCoversTest extends TestCase
65+
{
66+
}

0 commit comments

Comments
 (0)