From b80a2b496600d107b67cace4c4d4744bd19e9e5c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 30 Jan 2020 01:44:09 +0100 Subject: [PATCH] Tokenizer/PHP: allow for alternative syntax for declare The `declare` language construct can also use the alternative control structure syntax, but in that case would not get assigned `scope_opener` or `scope_closer` indexes. This fixed that. Includes unit test via the `PSR1.Files.SideEffects` sniff, which also needed as small tweaks to disregard the semicolon after the `enddeclare`. --- src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php | 6 ++++++ .../PSR1/Tests/Files/SideEffectsUnitTest.1.inc | 6 +++++- src/Tokenizers/PHP.php | 10 ++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php b/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php index 7ddcdf5267..12508fad4c 100644 --- a/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php +++ b/src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php @@ -151,6 +151,12 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens) ) { if (isset($tokens[$i]['scope_opener']) === true) { $i = $tokens[$i]['scope_closer']; + if ($tokens[$i]['code'] === T_ENDDECLARE) { + $semicolon = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true); + if ($semicolon !== false && $tokens[$semicolon]['code'] === T_SEMICOLON) { + $i = $semicolon; + } + } } else { $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($i + 1)); if ($semicolon !== false) { diff --git a/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc b/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc index e78824cd13..fdfd9b0985 100644 --- a/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc +++ b/src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc @@ -10,9 +10,13 @@ use SomethingElse; declare(ticks=1); declare(ticks=1) { - // Code. + echo $i; } +declare(ticks=1) : + echo $i; +enddeclare; + define("MAXSIZE", 100); if (defined('MINSIZE') === false) { define("MINSIZE", 10); diff --git a/src/Tokenizers/PHP.php b/src/Tokenizers/PHP.php index 94eae962f6..31c81af66f 100644 --- a/src/Tokenizers/PHP.php +++ b/src/Tokenizers/PHP.php @@ -160,8 +160,14 @@ class PHP extends Tokenizer 'with' => [], ], T_DECLARE => [ - 'start' => [T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET], - 'end' => [T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET], + 'start' => [ + T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET, + T_COLON => T_COLON, + ], + 'end' => [ + T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET, + T_ENDDECLARE => T_ENDDECLARE, + ], 'strict' => false, 'shared' => false, 'with' => [],