Skip to content

Commit f608290

Browse files
author
Vincent Langlet
committed
✨ Add fixer for AssignmentSpacingSniff
1 parent b0bb443 commit f608290

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

Symfony3Custom/Sniffs/WhiteSpace/AssignmentSpacingSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
4343
|| $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE)
4444
&& $tokens[$stackPtr - 1]['content'] !== 'strict_types'
4545
) {
46-
$phpcsFile->addError(
46+
$fix = $phpcsFile->addFixableError(
4747
'Add a single space around assignment operators',
4848
$stackPtr,
4949
'Invalid'
5050
);
51+
52+
if ($fix === true) {
53+
if ($tokens[$stackPtr -1]['code'] !== T_WHITESPACE) {
54+
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
55+
}
56+
if ($tokens[$stackPtr +1]['code'] !== T_WHITESPACE) {
57+
$phpcsFile->fixer->addContent($stackPtr, ' ');
58+
}
59+
}
5160
}
5261
}
5362
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$a=1;
4+
$a = 1;
5+
6+
$a+=1;
7+
$a += 1;
8+
9+
$a.=1;
10+
$a .= 1;
11+
12+
$arr = [
13+
1=>1,
14+
2 => 2,
15+
];
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$a = 1;
4+
$a = 1;
5+
6+
$a += 1;
7+
$a += 1;
8+
9+
$a .= 1;
10+
$a .= 1;
11+
12+
$arr = [
13+
1 => 1,
14+
2 => 2,
15+
];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the AssignmentSpacing sniff.
5+
*
6+
* A sniff unit test checks a .inc file for expected violations of a single
7+
* coding standard. Expected errors and warnings are stored in this class.
8+
*/
9+
class Symfony3Custom_Tests_WhiteSpace_AssignmentSpacingUnitTest
10+
extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* Returns the lines where errors should occur.
14+
*
15+
* The key of the array should represent the line number and the value
16+
* should represent the number of errors that should occur on that line.
17+
*
18+
* @return array<int, int>
19+
*/
20+
public function getErrorList()
21+
{
22+
return array(
23+
3 => 1,
24+
6 => 1,
25+
9 => 1,
26+
13 => 1,
27+
);
28+
}
29+
30+
/**
31+
* Returns the lines where warnings should occur.
32+
*
33+
* The key of the array should represent the line number and the value
34+
* should represent the number of warnings that should occur on that line.
35+
*
36+
* @return array(int => int)
37+
*/
38+
protected function getWarningList()
39+
{
40+
return array();
41+
}
42+
}

0 commit comments

Comments
 (0)