Skip to content

Commit d68d298

Browse files
Check for operator at end of line
1 parent 40eb821 commit d68d298

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyCustom\Sniffs\Operators;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Util\Tokens;
10+
11+
/**
12+
* Checks that the operator is at the start of the line.
13+
*/
14+
class OperatorPlacementSniff implements Sniff
15+
{
16+
/**
17+
* @return int[]
18+
*/
19+
public function register(): array
20+
{
21+
$targets = Tokens::$comparisonTokens;
22+
$targets += Tokens::$operators;
23+
$targets[] = T_STRING_CONCAT;
24+
$targets[] = T_INLINE_THEN;
25+
$targets[] = T_INLINE_ELSE;
26+
27+
return $targets;
28+
}
29+
30+
/**
31+
* @param File $phpcsFile
32+
* @param int $stackPtr
33+
*
34+
* @return void
35+
*/
36+
public function process(File $phpcsFile, $stackPtr): void
37+
{
38+
$tokens = $phpcsFile->getTokens();
39+
40+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
41+
42+
if ($tokens[$stackPtr]['line'] === $tokens[$next]['line']) {
43+
return;
44+
}
45+
46+
$content = $tokens[$stackPtr]['content'];
47+
$error = 'Operator "%s" should be on the start of the next line';
48+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OperatorPlacement', [$content]);
49+
50+
if ($fix) {
51+
$phpcsFile->fixer->beginChangeset();
52+
$phpcsFile->fixer->replaceToken($stackPtr, '');
53+
$phpcsFile->fixer->addContentBefore($next, $content);
54+
$phpcsFile->fixer->endChangeset();
55+
}
56+
}
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
$a = 'foo'.// With comment
4+
'foo';
5+
6+
$a = 'foo'.
7+
'foo';
8+
9+
$a = 'foo'
10+
.'foo';
11+
12+
$a = 'foo'?
13+
'foo':
14+
'bar';
15+
16+
$a = 1+
17+
1;
18+
19+
$a = 1<
20+
1;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
$a = 'foo'// With comment
4+
.'foo';
5+
6+
$a = 'foo'
7+
.'foo';
8+
9+
$a = 'foo'
10+
.'foo';
11+
12+
$a = 'foo'
13+
?'foo'
14+
:'bar';
15+
16+
$a = 1
17+
+1;
18+
19+
$a = 1
20+
<1;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyCustom\Tests\Operators;
6+
7+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
8+
9+
/**
10+
* Unit test class for the OperatorPlacement sniff.
11+
*/
12+
class OperatorPlacementUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* @return array<int, int>
16+
*/
17+
protected function getErrorList(): array
18+
{
19+
return [
20+
3 => 1,
21+
6 => 1,
22+
12 => 1,
23+
13 => 1,
24+
16 => 1,
25+
19 => 1,
26+
];
27+
}
28+
29+
/**
30+
* @return array<int, int>
31+
*/
32+
protected function getWarningList(): array
33+
{
34+
return [];
35+
}
36+
}

0 commit comments

Comments
 (0)