Skip to content

Commit 22a3446

Browse files
committed
Roughs in a simple ReturnTrailingNewlineSniff
1 parent b8574fb commit 22a3446

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Corpus\Sniffs\General;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
class ReturnTrailingNewlineSniff implements Sniff {
9+
10+
public const CODE_RETURN_HAS_TRAILING_NEWLINE = 'ReturnHasTrailingNewline';
11+
12+
/**
13+
* @inheritDoc
14+
*/
15+
public function register() {
16+
return [ T_RETURN ];
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function process( File $phpcsFile, $stackPtr ) {
23+
$tokens = $phpcsFile->getTokens();
24+
25+
$eosPtr = $phpcsFile->findEndOfStatement($stackPtr);
26+
$nextPtr = $phpcsFile->findNext(T_WHITESPACE, $eosPtr + 1, null, true);
27+
if( !$nextPtr ) {
28+
return;
29+
}
30+
31+
if( $tokens[$nextPtr]['code'] !== T_CLOSE_CURLY_BRACKET ) {
32+
return;
33+
}
34+
35+
if( $tokens[$nextPtr]['line'] <= $tokens[$eosPtr]['line'] + 1 ) {
36+
return;
37+
}
38+
39+
$fix = $phpcsFile->addFixableError(
40+
'There must be no blank lines between return and the following curly brace',
41+
$stackPtr,
42+
self::CODE_RETURN_HAS_TRAILING_NEWLINE
43+
);
44+
if( $fix ) {
45+
$phpcsFile->fixer->beginChangeset();
46+
for( $removePtr = $eosPtr + 1; $removePtr <= $nextPtr; $removePtr++ ) {
47+
if( $tokens[$removePtr + 1]['line'] === $tokens[$nextPtr]['line'] ) {
48+
break;
49+
}
50+
51+
$phpcsFile->fixer->replaceToken($removePtr, '');
52+
}
53+
54+
$phpcsFile->fixer->endChangeset();
55+
}
56+
}
57+
58+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
function foo() {
4+
5+
return;
6+
}
7+
8+
function bar() {
9+
return 1;
10+
}
11+
12+
13+
class Foo {
14+
15+
function foo() {
16+
if(true) {
17+
return;
18+
}
19+
20+
}
21+
22+
function bar() {
23+
return 1;
24+
}
25+
26+
27+
}
28+
29+
function baz() {
30+
return 1;
31+
32+
// This is fine
33+
}
34+
35+
// nothing after this, ignore
36+
return;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
function foo() {
4+
5+
return;
6+
7+
}
8+
9+
function bar() {
10+
return 1;
11+
12+
}
13+
14+
15+
class Foo {
16+
17+
function foo() {
18+
if(true) {
19+
return;
20+
21+
}
22+
23+
}
24+
25+
function bar() {
26+
return 1;
27+
28+
}
29+
30+
31+
}
32+
33+
function baz() {
34+
return 1;
35+
36+
// This is fine
37+
}
38+
39+
// nothing after this, ignore
40+
return;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Returning from the root shouldn't do anything
4+
5+
return [];
6+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Corpus\Sniffs\General;
4+
5+
use SlevomatCodingStandard\Sniffs\TestCase;
6+
7+
class ReturnTrailingNewlineSniffTest extends TestCase {
8+
9+
public function testErrors() : void {
10+
$report = self::checkFile(__DIR__ . '/ReturnTrailingNewline.data/examples.php');
11+
$this->assertSame(4, $report->getErrorCount());
12+
13+
self::assertAllFixedInFile($report);
14+
}
15+
16+
public function testRootReturn() : void{
17+
$report = self::checkFile(__DIR__ . '/ReturnTrailingNewline.data/rootreturn.php');
18+
$this->assertSame(0, $report->getErrorCount());
19+
}
20+
21+
}

0 commit comments

Comments
 (0)