Skip to content

Commit 60160dc

Browse files
author
Vincent Langlet
committed
✨ Add check arounf (, {, ) and }
1 parent 876f900 commit 60160dc

8 files changed

+247
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* Checks that there is no white space before a closing bracket, for ")" and "}".
5+
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
6+
*/
7+
class Symfony3Custom_Sniffs_WhiteSpace_CloseBracketSpacingSniff implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of tokenizers this sniff supports.
11+
*
12+
* @var array
13+
*/
14+
public $supportedTokenizers = array(
15+
'PHP',
16+
'JS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(
27+
T_CLOSE_CURLY_BRACKET,
28+
T_CLOSE_PARENTHESIS,
29+
);
30+
}
31+
32+
/**
33+
* Processes this test, when one of its tokens is encountered.
34+
*
35+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token
37+
* in the stack passed in $tokens.
38+
*
39+
* @return void
40+
*/
41+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
45+
// Ignore curly brackets in javascript files.
46+
if ($tokens[$stackPtr]['code'] === T_CLOSE_CURLY_BRACKET
47+
&& $phpcsFile->tokenizerType === 'JS'
48+
) {
49+
return;
50+
}
51+
52+
if (isset($tokens[($stackPtr - 1)]) === true
53+
&& $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE
54+
) {
55+
$before = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
56+
if ($before !== false
57+
&& $tokens[$stackPtr]['line'] === $tokens[$before]['line']
58+
) {
59+
$error = 'There should be no space before a closing "%s"';
60+
$fix = $phpcsFile->addFixableError(
61+
$error,
62+
($stackPtr - 1),
63+
'ClosingWhitespace',
64+
array($tokens[$stackPtr]['content'])
65+
);
66+
67+
if ($fix === true) {
68+
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
69+
}
70+
}
71+
}
72+
}
73+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Checks that there is no white space after an opening bracket, for "(" and "{".
5+
* Square Brackets are handled by Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
6+
*/
7+
class Symfony3Custom_Sniffs_WhiteSpace_OpenBracketSpacingSniff implements PHP_CodeSniffer_Sniff
8+
{
9+
/**
10+
* A list of tokenizers this sniff supports.
11+
*
12+
* @var array
13+
*/
14+
public $supportedTokenizers = array(
15+
'PHP',
16+
'JS',
17+
);
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return array(
27+
T_OPEN_CURLY_BRACKET,
28+
T_OPEN_PARENTHESIS,
29+
);
30+
31+
}
32+
33+
/**
34+
* Processes this test, when one of its tokens is encountered.
35+
*
36+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
37+
* @param int $stackPtr The position of the current token
38+
* in the stack passed in $tokens.
39+
*
40+
* @return void
41+
*/
42+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
43+
{
44+
$tokens = $phpcsFile->getTokens();
45+
46+
// Ignore curly brackets in javascript files.
47+
if ($tokens[$stackPtr]['code'] === T_OPEN_CURLY_BRACKET
48+
&& $phpcsFile->tokenizerType === 'JS'
49+
) {
50+
return;
51+
}
52+
53+
if (isset($tokens[($stackPtr + 1)]) === true
54+
&& $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
55+
&& strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) === false
56+
) {
57+
$error = 'There should be no space after an opening "%s"';
58+
$phpcsFile->addError(
59+
$error,
60+
($stackPtr + 1),
61+
'OpeningWhitespace',
62+
array($tokens[$stackPtr]['content'])
63+
);
64+
}
65+
}
66+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
testFunction( args1, args2 );
4+
5+
function f( ) { };
6+
7+
$array = array( 1, 2 );
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
testFunction( args1, args2);
4+
5+
function f() {};
6+
7+
$array = array( 1, 2);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the CloseBracketSpacing 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_CloseBracketSpacingUnitTest extends AbstractSniffUnitTest
10+
{
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @return array<int, int>
18+
*/
19+
public function getErrorList()
20+
{
21+
return array(
22+
3 => 1,
23+
5 => 2,
24+
7 => 1,
25+
);
26+
}
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* The key of the array should represent the line number and the value
32+
* should represent the number of warnings that should occur on that line.
33+
*
34+
* @return array(int => int)
35+
*/
36+
protected function getWarningList()
37+
{
38+
return array();
39+
}
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
testFunction( args1, args2 );
4+
5+
function f( ) { };
6+
7+
$array = array( 1, 2 );
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
testFunction(args1, args2 );
4+
5+
function f() {};
6+
7+
$array = array(1, 2 );
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the OpenBracketSpacing 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_OpenBracketSpacingUnitTest extends AbstractSniffUnitTest
10+
{
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @return array<int, int>
18+
*/
19+
public function getErrorList()
20+
{
21+
return array(
22+
3 => 1,
23+
5 => 2,
24+
7 => 1,
25+
);
26+
}
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* The key of the array should represent the line number and the value
32+
* should represent the number of warnings that should occur on that line.
33+
*
34+
* @return array(int => int)
35+
*/
36+
protected function getWarningList()
37+
{
38+
return array();
39+
}
40+
}

0 commit comments

Comments
 (0)