|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Formatting; |
|
6
|
|
|
|
|
7
|
|
|
use PHP_CodeSniffer_File; |
|
8
|
|
|
use PHP_CodeSniffer_Sniff; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class SpaceAfterDeclareSniff |
|
12
|
|
|
* |
|
13
|
|
|
* @package BestIt\Sniffs\Formatting |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nick Lubisch <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class SpaceAfterDeclareSniff implements PHP_CodeSniffer_Sniff |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Error message when no whitespace is found. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const MESSAGE_NO_WHITESPACE_FOUND = 'There is no whitespace after declare-statement.'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Code when no whitespace is found. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const CODE_NO_WHITESPACE_FOUND = 'NoWhitespaceFound'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Error message when more than one whitespaces are found. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
const MESSAGE_MUCH_WHITESPACE_FOUND = 'There are more than one whitespaces after declare-statement.'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Code when more than one whitespaces are found. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
const CODE_MUCH_WHITESPACE_FOUND = 'MuchWhitespaceFound'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Registers the tokens that this sniff wants to listen for. |
|
49
|
|
|
* |
|
50
|
|
|
* @return int[] |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function register() |
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
4 |
|
T_DECLARE |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Called when one of the token types that this sniff is listening for |
|
61
|
|
|
* is found. |
|
62
|
|
|
* |
|
63
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the token was found. |
|
64
|
|
|
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found. |
|
65
|
|
|
* |
|
66
|
|
|
* @return void|int Optionally returns a stack pointer. |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
69
|
|
|
{ |
|
70
|
4 |
|
$tokens = $phpcsFile->getTokens(); |
|
71
|
|
|
|
|
72
|
4 |
|
$semicolonPtr = $phpcsFile->findEndOfStatement($stackPtr); |
|
73
|
|
|
|
|
74
|
4 |
|
$secondSpacePtr = $semicolonPtr + 2; |
|
75
|
4 |
|
$secondSpaceToken = $tokens[$secondSpacePtr]; |
|
76
|
|
|
|
|
77
|
4 |
|
if ($secondSpaceToken['code'] !== T_WHITESPACE) { |
|
78
|
2 |
|
$this->handleNoWhitespaceFound($phpcsFile, $semicolonPtr); |
|
79
|
|
|
|
|
80
|
2 |
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
$nextNonSpacePtr = $phpcsFile->findNext(T_WHITESPACE, $secondSpacePtr, null, true); |
|
84
|
|
|
|
|
85
|
4 |
|
if ($nextNonSpacePtr === false) { |
|
86
|
1 |
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$nextNonSpaceToken = $tokens[$nextNonSpacePtr]; |
|
90
|
|
|
|
|
91
|
3 |
|
if (($nextNonSpaceToken['line'] - $secondSpaceToken['line']) > 1) { |
|
92
|
1 |
|
$this->handleMuchWhitespacesFound($phpcsFile, $semicolonPtr, $secondSpacePtr, $nextNonSpacePtr); |
|
93
|
|
|
|
|
94
|
1 |
|
return; |
|
95
|
|
|
} |
|
96
|
3 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Handles when no whitespace is found. |
|
100
|
|
|
* |
|
101
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile |
|
102
|
|
|
* @param int $semicolonPtr |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
2 |
|
private function handleNoWhitespaceFound(PHP_CodeSniffer_File $phpcsFile, $semicolonPtr) |
|
107
|
|
|
{ |
|
108
|
2 |
|
$fixNoWhitespace = $phpcsFile->addFixableError( |
|
109
|
2 |
|
self::MESSAGE_NO_WHITESPACE_FOUND, |
|
110
|
|
|
$semicolonPtr, |
|
111
|
2 |
|
self::CODE_NO_WHITESPACE_FOUND |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
2 |
|
if ($fixNoWhitespace) { |
|
115
|
2 |
|
$phpcsFile->fixer->beginChangeset(); |
|
116
|
2 |
|
$phpcsFile->fixer->addNewline($semicolonPtr); |
|
117
|
2 |
|
$phpcsFile->fixer->endChangeset(); |
|
118
|
|
|
} |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Handles when more than one whitespaces are found. |
|
123
|
|
|
* |
|
124
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile |
|
125
|
|
|
* @param int $semicolonPtr |
|
126
|
|
|
* @param int $secondSpacePtr |
|
127
|
|
|
* @param int $nextNonSpacePtr |
|
128
|
|
|
* |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
1 |
|
private function handleMuchWhitespacesFound( |
|
132
|
|
|
PHP_CodeSniffer_File $phpcsFile, |
|
133
|
|
|
$semicolonPtr, |
|
134
|
|
|
$secondSpacePtr, |
|
135
|
|
|
$nextNonSpacePtr |
|
136
|
|
|
) { |
|
137
|
1 |
|
$fixMuchWhitespaces = $phpcsFile->addFixableError( |
|
138
|
1 |
|
self::MESSAGE_MUCH_WHITESPACE_FOUND, |
|
139
|
|
|
$semicolonPtr, |
|
140
|
1 |
|
self::CODE_MUCH_WHITESPACE_FOUND |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
1 |
|
if ($fixMuchWhitespaces) { |
|
144
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
145
|
1 |
|
for ($i = $secondSpacePtr; $i < $nextNonSpacePtr; $i++) { |
|
146
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
147
|
|
|
} |
|
148
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
149
|
|
|
} |
|
150
|
1 |
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|