Skip to content

Commit 48a3e31

Browse files
Set up code coverage, remove unreachable logic
1 parent af93edf commit 48a3e31

File tree

7 files changed

+51
-16
lines changed

7 files changed

+51
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
# Tests
66
.phpunit.result.cache
7+
8+
# Code coverage report
9+
html-coverage

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {
2828
"parallel-lint": "parallel-lint . --exclude vendor",
2929
"phpcs": "phpcs -p -s",
30-
"phpunit": "phpunit",
30+
"phpunit": "php -d extension=pcov.so -d pcov.enabled=1 -d pcov.directory=. vendor/bin/phpunit",
3131
"lint": [
3232
"@parallel-lint",
3333
"@phpcs"

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@
1717
<directory>./src/Tests</directory>
1818
</testsuite>
1919
</testsuites>
20+
<source>
21+
<include>
22+
<directory>./src/Sniffs</directory>
23+
</include>
24+
</source>
25+
<coverage includeUncoveredFiles="true">
26+
<report>
27+
<html outputDirectory="html-coverage" />
28+
</report>
29+
</coverage>
2030
</phpunit>

src/Sniffs/Attributes/AttributeAlignmentSniff.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,10 @@ public function process( File $phpcsFile, $stackPtr ) {
9292
);
9393
}
9494

95-
// Clear all of the other whitespace tokens before the attribute; this
96-
// works for both cases because if the attribute was the start of the
97-
// line then $iii will start at $stackPtr and the loop never runs
98-
for ( $iii = $targetLineStart; $iii < $stackPtr - 1; $iii++ ) {
99-
$phpcsFile->fixer->replaceToken( $iii, '' );
100-
}
95+
// Apparently we don't need to worry about the other whitespace tokens
96+
// that came before the stack pointer, I could not figure out a way to
97+
// trigger needing to remove them, so the code for that was removed
98+
// since it was untested and potentially unneeded.
10199

102100
$phpcsFile->fixer->endChangeset();
103101
}
@@ -117,7 +115,11 @@ private function getLineStart( File $phpcsFile, int $stackPtr ): int|false {
117115
true
118116
);
119117
if ( $prevNonWhitespace === false ) {
118+
// There were no tokens before this in the file, should be
119+
// impossible
120+
// @codeCoverageIgnoreStart
120121
return false;
122+
// @codeCoverageIgnoreEnd
121123
}
122124
$tokens = $phpcsFile->getTokens();
123125
$stackPtrLine = $tokens[$stackPtr]['line'];

src/Tests/data/AttributeAlignment/wrong.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare( strict_types = 1 );
33

4-
// phpcs:disable MediaWiki.Commenting
4+
// phpcs:disable MediaWiki.Commenting,Squiz.WhiteSpace.FunctionSpacing.Before
55
// phpcs:disable MediaWiki.Files.ClassMatchesFilename
66
// phpcs:disable Generic.WhiteSpace.ScopeIndent
77

@@ -49,14 +49,22 @@ function demo4() {
4949
#[First]
5050
#[Second]
5151
function demo5() {
52+
}
53+
54+
#[TwoSpaces]
55+
function demo6() {
5256
}
5357

5458
// Not fixable
5559
#[MyAttribute]
5660

57-
/* ignored */ function demo6() {
61+
/* ignored */ function demo7() {
62+
}
63+
64+
/* not fixable */ #[MyAttribute]
65+
function demo8() {
5866
}
5967

6068
// Not covered
61-
#[MyAttribute] function demo7() {
69+
#[MyAttribute] function demo9() {
6270
}

src/Tests/data/AttributeAlignment/wrong.php.fixed

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare( strict_types = 1 );
33

4-
// phpcs:disable MediaWiki.Commenting
4+
// phpcs:disable MediaWiki.Commenting,Squiz.WhiteSpace.FunctionSpacing.Before
55
// phpcs:disable MediaWiki.Files.ClassMatchesFilename
66
// phpcs:disable Generic.WhiteSpace.ScopeIndent
77

@@ -51,12 +51,20 @@ function demo4() {
5151
function demo5() {
5252
}
5353

54+
#[TwoSpaces]
55+
function demo6() {
56+
}
57+
5458
// Not fixable
5559
#[MyAttribute]
5660

57-
/* ignored */ function demo6() {
61+
/* ignored */ function demo7() {
62+
}
63+
64+
/* not fixable */ #[MyAttribute]
65+
function demo8() {
5866
}
5967

6068
// Not covered
61-
#[MyAttribute] function demo7() {
69+
#[MyAttribute] function demo9() {
6270
}

src/Tests/data/AttributeAlignment/wrong.report

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FILE: {dir}/data/AttributeAlignment/wrong.php
22
--------------------------------------------------------------------------------
3-
FOUND 0 ERRORS AND 14 WARNINGS AFFECTING 14 LINES
3+
FOUND 0 ERRORS AND 16 WARNINGS AFFECTING 16 LINES
44
--------------------------------------------------------------------------------
55
8 | WARNING | [x] Attributes should be aligned with their targets
66
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
@@ -28,8 +28,12 @@ FOUND 0 ERRORS AND 14 WARNINGS AFFECTING 14 LINES
2828
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
2929
50 | WARNING | [x] Attributes should be aligned with their targets
3030
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
31-
55 | WARNING | [ ] Attributes should be aligned with their targets
31+
54 | WARNING | [x] Attributes should be aligned with their targets
32+
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
33+
59 | WARNING | [ ] Attributes should be aligned with their targets
34+
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
35+
64 | WARNING | [ ] Attributes should be aligned with their targets
3236
| | (CommonPhpcs.Attributes.AttributeAlignment.NotAligned)
3337
--------------------------------------------------------------------------------
34-
PHPCBF CAN FIX THE 13 MARKED SNIFF VIOLATIONS AUTOMATICALLY
38+
PHPCBF CAN FIX THE 14 MARKED SNIFF VIOLATIONS AUTOMATICALLY
3539
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)