Skip to content

Commit 7559324

Browse files
committed
Merge branch 'feature/3326-generic-multiplestatementalignment-bugfix' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents bf08940 + 0d5663c commit 7559324

File tree

5 files changed

+82
-44
lines changed

5 files changed

+82
-44
lines changed

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,13 @@ $resource = new class() {
468468

469469
$one <<= 8;
470470
$onetwothree = 3;
471+
472+
// Issue 3326.
473+
class Test
474+
{
475+
public const DEFAULT = 'default';
476+
public const SOS = 'sos';
477+
public const HELP = 'help';
478+
479+
protected static $thisIsAReallyLongVariableName = [];
480+
}

src/Standards/Generic/Tests/Formatting/MultipleStatementAlignmentUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,13 @@ $resource = new class() {
468468

469469
$one <<= 8;
470470
$onetwothree = 3;
471+
472+
// Issue 3326.
473+
class Test
474+
{
475+
public const DEFAULT = 'default';
476+
public const SOS = 'sos';
477+
public const HELP = 'help';
478+
479+
protected static $thisIsAReallyLongVariableName = [];
480+
}

src/Tokenizers/PHP.php

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,29 @@ class PHP extends Tokenizer
454454
T_TYPE_UNION => 1,
455455
];
456456

457+
/**
458+
* Contexts in which keywords should always be tokenized as T_STRING.
459+
*
460+
* @var array
461+
*/
462+
protected $tstringContexts = [
463+
T_OBJECT_OPERATOR => true,
464+
T_NULLSAFE_OBJECT_OPERATOR => true,
465+
T_FUNCTION => true,
466+
T_CLASS => true,
467+
T_INTERFACE => true,
468+
T_TRAIT => true,
469+
T_EXTENDS => true,
470+
T_IMPLEMENTS => true,
471+
T_ATTRIBUTE => true,
472+
T_NEW => true,
473+
T_CONST => true,
474+
T_NS_SEPARATOR => true,
475+
T_USE => true,
476+
T_NAMESPACE => true,
477+
T_PAAMAYIM_NEKUDOTAYIM => true,
478+
];
479+
457480
/**
458481
* A cache of different token types, resolved into arrays.
459482
*
@@ -1332,16 +1355,7 @@ protected function tokenize($string)
13321355
break;
13331356
}
13341357

1335-
$notMatchContext = [
1336-
T_PAAMAYIM_NEKUDOTAYIM => true,
1337-
T_OBJECT_OPERATOR => true,
1338-
T_NULLSAFE_OBJECT_OPERATOR => true,
1339-
T_NS_SEPARATOR => true,
1340-
T_NEW => true,
1341-
T_FUNCTION => true,
1342-
];
1343-
1344-
if (isset($notMatchContext[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
1358+
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
13451359
// Also not a match expression.
13461360
break;
13471361
}
@@ -1389,14 +1403,7 @@ protected function tokenize($string)
13891403
if ($tokenIsArray === true
13901404
&& $token[0] === T_DEFAULT
13911405
) {
1392-
$ignoreContext = [
1393-
T_OBJECT_OPERATOR => true,
1394-
T_NULLSAFE_OBJECT_OPERATOR => true,
1395-
T_NS_SEPARATOR => true,
1396-
T_PAAMAYIM_NEKUDOTAYIM => true,
1397-
];
1398-
1399-
if (isset($ignoreContext[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
1406+
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
14001407
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
14011408
if ($tokens[$x] === ',') {
14021409
// Skip over potential trailing comma (supported in PHP).
@@ -1894,25 +1901,7 @@ function return types. We want to keep the parenthesis map clean,
18941901
if ($tokenIsArray === true && $token[0] === T_STRING) {
18951902
// Some T_STRING tokens should remain that way
18961903
// due to their context.
1897-
$context = [
1898-
T_OBJECT_OPERATOR => true,
1899-
T_NULLSAFE_OBJECT_OPERATOR => true,
1900-
T_FUNCTION => true,
1901-
T_CLASS => true,
1902-
T_INTERFACE => true,
1903-
T_TRAIT => true,
1904-
T_EXTENDS => true,
1905-
T_IMPLEMENTS => true,
1906-
T_ATTRIBUTE => true,
1907-
T_NEW => true,
1908-
T_CONST => true,
1909-
T_NS_SEPARATOR => true,
1910-
T_USE => true,
1911-
T_NAMESPACE => true,
1912-
T_PAAMAYIM_NEKUDOTAYIM => true,
1913-
];
1914-
1915-
if (isset($context[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
1904+
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
19161905
// Special case for syntax like: return new self
19171906
// where self should not be a string.
19181907
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
@@ -2786,13 +2775,7 @@ protected function processAdditional()
27862775
}
27872776
}
27882777

2789-
$context = [
2790-
T_OBJECT_OPERATOR => true,
2791-
T_NULLSAFE_OBJECT_OPERATOR => true,
2792-
T_NS_SEPARATOR => true,
2793-
T_PAAMAYIM_NEKUDOTAYIM => true,
2794-
];
2795-
if (isset($context[$this->tokens[$x]['code']]) === true) {
2778+
if (isset($this->tstringContexts[$this->tokens[$x]['code']]) === true) {
27962779
if (PHP_CODESNIFFER_VERBOSITY > 1) {
27972780
$line = $this->tokens[$i]['line'];
27982781
$type = $this->tokens[$i]['type'];

tests/Core/Tokenizer/DefaultKeywordTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,11 @@ function switchWithConstantNonDefault($i) {
193193
return 2;
194194
}
195195
}
196+
197+
class Foo {
198+
/* testClassConstant */
199+
const DEFAULT = 'foo';
200+
201+
/* testMethodDeclaration */
202+
public function default() {}
203+
}

tests/Core/Tokenizer/DefaultKeywordTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,36 @@ public function dataNotDefaultKeyword()
267267
'class-property-in-switch-case' => ['/* testClassPropertyInSwitchCase */'],
268268
'namespaced-constant-in-switch-case' => ['/* testNamespacedConstantInSwitchCase */'],
269269
'namespace-relative-constant-in-switch-case' => ['/* testNamespaceRelativeConstantInSwitchCase */'],
270+
271+
'class-constant-declaration' => ['/* testClassConstant */'],
272+
'class-method-declaration' => [
273+
'/* testMethodDeclaration */',
274+
'default',
275+
],
270276
];
271277

272278
}//end dataNotDefaultKeyword()
273279

274280

281+
/**
282+
* Test a specific edge case where a scope opener would be incorrectly set.
283+
*
284+
* @link https://github.com/squizlabs/PHP_CodeSniffer/issues/3326
285+
*
286+
* @return void
287+
*/
288+
public function testIssue3326()
289+
{
290+
$tokens = self::$phpcsFile->getTokens();
291+
292+
$token = $this->getTargetToken('/* testClassConstant */', [T_SEMICOLON]);
293+
$tokenArray = $tokens[$token];
294+
295+
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
296+
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
297+
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
298+
299+
}//end testIssue3326()
300+
301+
275302
}//end class

0 commit comments

Comments
 (0)