1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_Strings_ConcatenationSpacingSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Peter Philipp <[email protected]> |
10
|
|
|
* @author Alexander Obuhovich <[email protected]> |
11
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
12
|
|
|
* @link https://github.com/aik099/CodingStandard |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace CodingStandard\Sniffs\Strings; |
16
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Files\File; |
18
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* CodingStandard_Sniffs_Strings_ConcatenationSpacingSniff. |
22
|
|
|
* |
23
|
|
|
* Makes sure there are the needed spaces between the concatenation operator (.) and |
24
|
|
|
* the strings being concatenated. |
25
|
|
|
* |
26
|
|
|
* @category PHP |
27
|
|
|
* @package PHP_CodeSniffer |
28
|
|
|
* @author Peter Philipp <[email protected]> |
29
|
|
|
* @author Alexander Obuhovich <[email protected]> |
30
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
31
|
|
|
* @link https://github.com/aik099/CodingStandard |
32
|
|
|
*/ |
33
|
|
|
class ConcatenationSpacingSniff implements Sniff |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns an array of tokens this test wants to listen for. |
39
|
|
|
* |
40
|
|
|
* @return integer[] |
41
|
|
|
*/ |
42
|
1 |
|
public function register() |
43
|
|
|
{ |
44
|
1 |
|
return array(T_STRING_CONCAT); |
45
|
|
|
}//end register() |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Processes this test, when one of its tokens is encountered. |
50
|
|
|
* |
51
|
|
|
* @param File $phpcsFile The file being scanned. |
52
|
|
|
* @param int $stackPtr The position of the current token in the |
53
|
|
|
* stack passed in $tokens. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
58
|
|
|
{ |
59
|
1 |
|
$this->checkContent($phpcsFile, $stackPtr, true); |
60
|
1 |
|
$this->checkContent($phpcsFile, $stackPtr, false); |
61
|
1 |
|
}//end process() |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks content before concat operator. |
66
|
|
|
* |
67
|
|
|
* @param File $phpcsFile The file being scanned. |
68
|
|
|
* @param int $stackPtr The position of the current token in the |
69
|
|
|
* stack passed in $tokens. |
70
|
|
|
* @param bool $before Check content before concat operator. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
1 |
|
protected function checkContent(File $phpcsFile, $stackPtr, $before) |
75
|
|
|
{ |
76
|
1 |
|
if ($before === true) { |
77
|
1 |
|
$contentToken = ($phpcsFile->findPrevious( |
78
|
1 |
|
T_WHITESPACE, |
79
|
1 |
|
($stackPtr - 1), |
80
|
1 |
|
null, |
81
|
|
|
true |
82
|
1 |
|
) + 1); |
83
|
1 |
|
$errorWord = 'before'; |
84
|
1 |
|
} else { |
85
|
1 |
|
$contentToken = ($phpcsFile->findNext( |
86
|
1 |
|
T_WHITESPACE, |
87
|
1 |
|
($stackPtr + 1), |
88
|
1 |
|
null, |
89
|
|
|
true |
90
|
1 |
|
) - 1); |
91
|
1 |
|
$errorWord = 'after'; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
95
|
1 |
|
$contentData = $tokens[$contentToken]; |
96
|
|
|
|
97
|
1 |
|
if ($contentData['line'] !== $tokens[$stackPtr]['line']) { |
98
|
|
|
// Ignore concat operator split across several lines. |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
if ($contentData['code'] !== T_WHITESPACE) { |
103
|
1 |
|
$fix = $phpcsFile->addFixableError( |
104
|
1 |
|
'Expected 1 space '.$errorWord.' concat operator; 0 found', |
105
|
1 |
|
$stackPtr, |
106
|
1 |
|
'NoSpace'.ucfirst($errorWord) |
107
|
1 |
|
); |
108
|
|
|
|
109
|
1 |
|
if ($fix === true) { |
110
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
111
|
|
|
|
112
|
1 |
|
if ($before === true) { |
113
|
1 |
|
$phpcsFile->fixer->addContentBefore($stackPtr, ' '); |
114
|
1 |
|
} else { |
115
|
1 |
|
$phpcsFile->fixer->addContent($stackPtr, ' '); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
119
|
1 |
|
} |
120
|
1 |
|
} elseif ($contentData['length'] !== 1) { |
121
|
1 |
|
$data = array($contentData['length']); |
122
|
1 |
|
$fix = $phpcsFile->addFixableError( |
123
|
1 |
|
'Expected 1 space '.$errorWord.' concat operator; %s found', |
124
|
1 |
|
$stackPtr, |
125
|
1 |
|
'SpaceBefore'.ucfirst($errorWord), |
126
|
|
|
$data |
127
|
1 |
|
); |
128
|
|
|
|
129
|
1 |
|
if ($fix === true) { |
130
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
131
|
1 |
|
$phpcsFile->fixer->replaceToken($contentToken, ' '); |
132
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
133
|
1 |
|
} |
134
|
1 |
|
} |
135
|
1 |
|
}//end checkContent() |
136
|
|
|
}//end class |
137
|
|
|
|