Skip to content

Commit 1c451ea

Browse files
committed
Update the sniff to catch all the test cases
1 parent ad50cf0 commit 1c451ea

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

Universal/Sniffs/DeclareStatements/DeclareStatementsStyleSniff.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,44 @@ public function process(File $phpcsFile, $stackPtr)
101101
{
102102
$tokens = $phpcsFile->getTokens();
103103

104-
if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
104+
$openParenPtr = $tokens[$stackPtr]['parenthesis_opener'];
105+
$closeParenPtr = $tokens[$stackPtr]['parenthesis_closer'];
106+
107+
if (isset($openParenPtr, $closeParenPtr) === false) {
105108
// Parse error or live coding, bow out.
106109
return;
107110
}
108111

112+
$directiveStrings = [];
109113
// Get the next string and check if it's an allowed directive.
110-
$directiveStringPtr = $phpcsFile->findNext(\T_STRING, $stackPtr);
111-
$directiveString = $tokens[$directiveStringPtr]['content'];
114+
// Find all the directive strings inside the declare statement.
115+
for ($i = $openParenPtr; $i <= $closeParenPtr; $i++) {
116+
if ($tokens[$i]['code'] === \T_STRING) {
117+
$directiveStrings[] = $tokens[$i]['content'];
118+
}
119+
}
112120

113-
if (!in_array($directiveString, $this->allowedDirectives, true)) {
114-
$phpcsFile->addError(
115-
sprintf(
116-
'Declare directives can be one of: %1$s. "%2$s" found.',
117-
implode(', ', $this->allowedDirectives),
118-
$directiveString
119-
),
120-
$stackPtr,
121-
'WrongDeclareDirective'
122-
);
123-
return;
121+
foreach ($directiveStrings as $directiveString) {
122+
if (!in_array($directiveString, $this->allowedDirectives, true)) {
123+
$phpcsFile->addError(
124+
sprintf(
125+
'Declare directives can be one of: %1$s. "%2$s" found.',
126+
implode(', ', $this->allowedDirectives),
127+
$directiveString
128+
),
129+
$stackPtr,
130+
'WrongDeclareDirective'
131+
);
132+
return;
133+
}
134+
unset($directiveString);
124135
}
125136

126137
// Curly braces.
127138
$hasScopeOpenerCloser = isset($tokens[$stackPtr]['scope_opener']);
128139

129140
// If strict types is defined using curly brace, throw error.
130-
if ($hasScopeOpenerCloser !== false && $directiveString === 'strict_types') {
141+
if ($hasScopeOpenerCloser !== false && in_array('strict_types', $directiveStrings, true)) {
131142
$phpcsFile->addError(
132143
sprintf(
133144
'strict_types declaration must not use block mode. Opening brace found on line %d',
@@ -140,17 +151,19 @@ public function process(File $phpcsFile, $stackPtr)
140151
}
141152

142153
// Fix for the case when the code is between the curly braces for the strict_types.
143-
$closeParenPtr = $tokens[$stackPtr]['parenthesis_closer'];
144154
$codePtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($closeParenPtr + 1), null, true);
145155

146-
// If the code pointer is not one of: \T_SEMICOLON, \T_CLOSE_TAG or \T_OPEN_CURLY_BRACKET, throw an error.
147-
if (!in_array($tokens[$codePtr]['code'], [\T_SEMICOLON, \T_CLOSE_TAG, \T_OPEN_CURLY_BRACKET], true)) {
156+
/* If the code pointer is not one of:
157+
* \T_SEMICOLON, \T_CLOSE_TAG or \T_OPEN_CURLY_BRACKET, \T_COLON
158+
* throw an error.
159+
*/
160+
if (!in_array($tokens[$codePtr]['code'], [\T_SEMICOLON, \T_CLOSE_TAG, \T_OPEN_CURLY_BRACKET, \T_COLON], true)) {
148161
$phpcsFile->addError('Unexpected code found after opening the declare statement without closing it.', $stackPtr, 'UnexpectedCodeFound');
149162
return;
150163
}
151164

152-
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false) {
153-
$phpcsFile->addError('Declare statement found without curly braces', $stackPtr, 'MissingCurlyBraces');
165+
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false && in_array('strict_types', $directiveStrings, true)) {
166+
$phpcsFile->addError('strict_types declaration is not compatible with requireBraces option.', $stackPtr, 'IncompatibleCurlyBracesRequirement');
154167
return;
155168
}
156169

@@ -160,27 +173,32 @@ public function process(File $phpcsFile, $stackPtr)
160173
}
161174

162175
if (in_array('ticks', $this->directiveType, true)) {
163-
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false) {
164-
$phpcsFile->addError('Declare statement found without curly braces', $stackPtr, 'MissingCurlyBraces');
176+
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false && in_array('ticks', $directiveStrings, true)) {
177+
$phpcsFile->addError('Declare statement for found without curly braces', $stackPtr, 'MissingCurlyBracesTicks');
165178
return;
166179
}
167180

168181
if ($this->declareStyle === 'disallowBraces' && $hasScopeOpenerCloser !== false) {
169-
$phpcsFile->addError('Declare statement found using curly braces', $stackPtr, 'DisallowedCurlyBraces');
182+
$phpcsFile->addError('Declare statement found using curly braces', $stackPtr, 'DisallowedCurlyBracesTicks');
170183
return;
171184
}
172185
}
173186

174187
if (in_array('encoding', $this->directiveType, true)) {
175-
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false) {
176-
$phpcsFile->addError('Declare statement found without curly braces', $stackPtr, 'MissingCurlyBraces');
188+
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false && in_array('encoding', $directiveStrings, true)) {
189+
$phpcsFile->addError('Declare statement found without curly braces', $stackPtr, 'MissingCurlyBracesEncoding');
177190
return;
178191
}
179192

180193
if ($this->declareStyle === 'disallowBraces' && $hasScopeOpenerCloser !== false) {
181-
$phpcsFile->addError('Declare statement found using curly braces', $stackPtr, 'DisallowedCurlyBraces');
194+
$phpcsFile->addError('Declare statement found using curly braces', $stackPtr, 'DisallowedCurlyBracesEncoding');
182195
return;
183196
}
184197
}
198+
199+
if ($this->declareStyle === 'requireBraces' && $hasScopeOpenerCloser === false && empty($this->directiveType) && !in_array('strict_types', $directiveStrings, true)) {
200+
$phpcsFile->addError('Declare statement found without curly braces', $stackPtr, 'MissingCurlyBraces');
201+
return;
202+
}
185203
}
186204
}

0 commit comments

Comments
 (0)