Skip to content

Commit 0f87551

Browse files
committed
Fix false positive.
1 parent 582fbde commit 0f87551

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

PhpCollective/Sniffs/WhiteSpace/MethodSpacingSniff.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,23 @@ public function process(File $phpcsFile, $stackPtr): void
8888
$error = 'There should be no extra newline at the end of a method';
8989
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeClose');
9090
if ($fix === true) {
91-
$phpcsFile->fixer->replaceToken($lastContentIndex + 1, '');
91+
// Find all whitespace tokens between last content and closing brace
92+
$phpcsFile->fixer->beginChangeset();
93+
for ($i = $lastContentIndex + 1; $i < $braceEndIndex; $i++) {
94+
if ($tokens[$i]['code'] === T_WHITESPACE) {
95+
if ($tokens[$i]['line'] === $tokens[$lastContentIndex]['line']) {
96+
// Keep whitespace on the same line as last content
97+
continue;
98+
}
99+
if ($tokens[$i]['line'] === $tokens[$braceEndIndex]['line']) {
100+
// Keep indentation before closing brace
101+
continue;
102+
}
103+
// Remove extra blank lines
104+
$phpcsFile->fixer->replaceToken($i, '');
105+
}
106+
}
107+
$phpcsFile->fixer->endChangeset();
92108
}
93109
}
94110
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
* For full license information, please view the LICENSE file that was distributed with this source code.
6+
*/
7+
8+
namespace PhpCollective\Test\PhpCollective\Sniffs\WhiteSpace;
9+
10+
use PhpCollective\Sniffs\WhiteSpace\MethodSpacingSniff;
11+
use PhpCollective\Test\TestCase;
12+
13+
class MethodSpacingSniffTest extends TestCase
14+
{
15+
/**
16+
* @return void
17+
*/
18+
public function testMethodSpacingSniffer(): void
19+
{
20+
$this->assertSnifferFindsErrors(new MethodSpacingSniff(), 2);
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function testMethodSpacingFixer(): void
27+
{
28+
$this->assertSnifferCanFixErrors(new MethodSpacingSniff());
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
class Test
4+
{
5+
public function foo()
6+
{
7+
return 'bar';
8+
}
9+
10+
public function bar()
11+
{
12+
return 'baz';
13+
}
14+
15+
public function getMonthDifference($from, $to)
16+
{
17+
$diff = date_diff($from, $to);
18+
19+
return (12 * $diff['y']) + $diff['m'];
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class Test
4+
{
5+
public function foo()
6+
{
7+
8+
return 'bar';
9+
}
10+
11+
public function bar()
12+
{
13+
return 'baz';
14+
15+
}
16+
17+
public function getMonthDifference($from, $to)
18+
{
19+
$diff = date_diff($from, $to);
20+
21+
return (12 * $diff['y']) + $diff['m'];
22+
23+
}
24+
}

0 commit comments

Comments
 (0)