Skip to content

Commit 7434d0d

Browse files
edgardmessiassamdark
authored andcommitted
Added check spaces around dot when concatenating strings for #24 (#26)
1 parent 444b087 commit 7434d0d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* Class for a sniff to oblige whitespaces before and after a concatenation operator
5+
*
6+
* This Sniff check if a whitespace is missing before or after any concatenation
7+
* operator.
8+
* The concatenation operator is identified by the PHP token T_STRING_CONCAT
9+
* The whitespace is identified by the PHP token T_WHITESPACE
10+
*
11+
* @since 2.0.2
12+
*/
13+
class Yii2_Sniffs_Files_SpacesAroundConcatSniff implements PHP_CodeSniffer_Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_STRING_CONCAT];
18+
}
19+
20+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
21+
{
22+
$tokens = $phpcsFile->getTokens();
23+
24+
if ($tokens[$stackPtr-1]['type'] !== 'T_WHITESPACE') {
25+
$error = 'Whitespace is expected before any concat operator "."';
26+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpace');
27+
if ($fix === true) {
28+
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
29+
}
30+
}
31+
if ($tokens[$stackPtr+1]['type'] !== 'T_WHITESPACE') {
32+
$error = 'Whitespace is expected after any concat operator "."';
33+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpace');
34+
if ($fix === true) {
35+
$phpcsFile->fixer->addContent($stackPtr, ' ');
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)