Skip to content

Commit 0bdbb89

Browse files
committed
Fixed an issue where the tokenizer was assigning curly braces incorrectly (ref #2596)
1 parent b0a2647 commit 0bdbb89

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4444
- JSON reports now end with a newline character
4545
- The phpcs.xsd schema now validates phpcs-only and phpcbf-only attributes correctly
4646
-- Thanks to Juliette Reinders Folmer for the patch
47+
- The tokenizer now correctly identifies inline control structures in more cases
4748
- All helper methods inside the File class now throw RuntimeException instead of TokenizerException
4849
-- Some tokenizer methods were also throwing RuntimeExpection but now correctly throw TokenizerException
4950
-- Thanks to Juliette Reinders Folmer for the patch

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,9 @@ if ($bar)
238238
if (true) $callable = function () {
239239
return true;
240240
};
241+
242+
foreach ([] as $a)
243+
echo 'bar';
244+
{
245+
echo 'baz';
246+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,10 @@ if (true) { $callable = function () {
273273
return true;
274274
};
275275
}
276+
277+
foreach ([] as $a) {
278+
echo 'bar';
279+
}
280+
{
281+
echo 'baz';
282+
}

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc')
7575
235 => 1,
7676
236 => 1,
7777
238 => 1,
78+
242 => 1,
7879
];
7980

8081
case 'InlineControlStructureUnitTest.js':

src/Tokenizers/Tokenizer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,20 @@ private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0)
13191319

13201320
$opener = $i;
13211321
}
1322+
} else if ($tokenType === T_SEMICOLON
1323+
&& $opener === null
1324+
&& (isset($this->tokens[$stackPtr]['parenthesis_closer']) === false
1325+
|| $i > $this->tokens[$stackPtr]['parenthesis_closer'])
1326+
) {
1327+
// Found the end of a statement but still haven't
1328+
// found our opener, so we are never going to find one.
1329+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1330+
$type = $this->tokens[$stackPtr]['type'];
1331+
echo str_repeat("\t", $depth);
1332+
echo "=> Found end of statement before scope opener for $stackPtr:$type, continuing".PHP_EOL;
1333+
}
1334+
1335+
return ($i - 1);
13221336
} else if ($tokenType === T_OPEN_PARENTHESIS) {
13231337
if (isset($this->tokens[$i]['parenthesis_owner']) === true) {
13241338
$owner = $this->tokens[$i]['parenthesis_owner'];
@@ -1328,7 +1342,7 @@ private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0)
13281342
// If we get into here, then we opened a parenthesis for
13291343
// a scope (eg. an if or else if) so we need to update the
13301344
// start of the line so that when we check to see
1331-
// if the closing parenthesis is more than 3 lines away from
1345+
// if the closing parenthesis is more than n lines away from
13321346
// the statement, we check from the closing parenthesis.
13331347
$startLine = $this->tokens[$this->tokens[$i]['parenthesis_closer']]['line'];
13341348
}

0 commit comments

Comments
 (0)