Skip to content

Commit fe68f36

Browse files
committed
Merge branch 'php-8.0/tokenizer-php-match-efficiency-tweaks' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents fc4970b + b4ad1ad commit fe68f36

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

src/Tokenizers/PHP.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,33 +1288,22 @@ protected function tokenize($string)
12881288
break;
12891289
}
12901290

1291-
// Next was an open parenthesis, now check what is before the match keyword.
1292-
for ($y = ($stackPtr - 1); $y >= 0; $y--) {
1293-
if (is_array($tokens[$y]) === true
1294-
&& isset(Util\Tokens::$emptyTokens[$tokens[$y][0]]) === true
1295-
) {
1296-
continue;
1297-
}
1291+
$notMatchContext = [
1292+
T_PAAMAYIM_NEKUDOTAYIM => true,
1293+
T_OBJECT_OPERATOR => true,
1294+
T_NULLSAFE_OBJECT_OPERATOR => true,
1295+
T_NS_SEPARATOR => true,
1296+
T_NEW => true,
1297+
T_FUNCTION => true,
1298+
];
12981299

1299-
if (is_array($tokens[$y]) === true
1300-
&& ($tokens[$y][0] === T_PAAMAYIM_NEKUDOTAYIM
1301-
|| $tokens[$y][0] === T_OBJECT_OPERATOR
1302-
|| $tokens[$y][0] === T_NS_SEPARATOR
1303-
|| $tokens[$y][0] === T_NEW
1304-
|| $tokens[$y][0] === T_FUNCTION
1305-
|| $tokens[$y][0] === T_CLASS
1306-
|| $tokens[$y][0] === T_INTERFACE
1307-
|| $tokens[$y][0] === T_TRAIT
1308-
|| $tokens[$y][0] === T_NAMESPACE
1309-
|| $tokens[$y][0] === T_CONST)
1310-
) {
1311-
// This is not a match expression.
1312-
break 2;
1313-
}
1300+
if (isset($notMatchContext[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
1301+
// Also not a match expression.
1302+
break;
1303+
}
13141304

1315-
$isMatch = true;
1316-
break 2;
1317-
}//end for
1305+
$isMatch = true;
1306+
break;
13181307
}//end for
13191308

13201309
if ($isMatch === true && $token[0] === T_STRING) {

tests/Core/Tokenizer/BackfillMatchTokenTest.inc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ $a = MyClass::Match[$a];
220220
$a = $obj->match($param);
221221

222222
/* testNoMatchMethodCallUpper */
223-
$a = $obj->MATCH()->chain($param);
223+
$a = $obj??->MATCH()->chain($param);
224224

225225
/* testNoMatchPropertyAccess */
226226
$a = $obj->match;
@@ -282,6 +282,18 @@ function match() {}
282282
// Intentional fatal error. Match is now a reserved keyword.
283283
namespace Match {}
284284

285+
/* testNoMatchExtendedClassDeclaration */
286+
// Intentional fatal error. Match is now a reserved keyword.
287+
class Foo extends Match {}
288+
289+
/* testNoMatchImplementedClassDeclaration */
290+
// Intentional fatal error. Match is now a reserved keyword.
291+
class Bar implements Match {}
292+
293+
/* testNoMatchInUseStatement */
294+
// Intentional fatal error in PHP < 8. Match is now a reserved keyword.
295+
use Match\me;
296+
285297
function brokenMatchNoCurlies($x) {
286298
/* testNoMatchMissingCurlies */
287299
// Intentional fatal error. New control structure is not supported without curly braces.

tests/Core/Tokenizer/BackfillMatchTokenTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ public function dataNotAMatchStructure()
296296
'/* testNoMatchNamespaceDeclaration */',
297297
'Match',
298298
],
299+
'class_extends_declaration' => [
300+
'/* testNoMatchExtendedClassDeclaration */',
301+
'Match',
302+
],
303+
'class_implements_declaration' => [
304+
'/* testNoMatchImplementedClassDeclaration */',
305+
'Match',
306+
],
307+
'use_statement' => [
308+
'/* testNoMatchInUseStatement */',
309+
'Match',
310+
],
299311
'unsupported_inline_control_structure' => ['/* testNoMatchMissingCurlies */'],
300312
'unsupported_alternative_syntax' => ['/* testNoMatchAlternativeSyntax */'],
301313
'live_coding' => ['/* testLiveCoding */'],

0 commit comments

Comments
 (0)