Skip to content

Commit

Permalink
Tokenizer/PHP: allow for alternative syntax for declare
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jrfnl committed Jan 30, 2020
1 parent a9f8060 commit b80a2b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion src/Standards/PSR1/Tests/Files/SideEffectsUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down

0 comments on commit b80a2b4

Please sign in to comment.