Skip to content

Commit

Permalink
Hotfix: detecting end of statement with PHP 7.4 fn closure
Browse files Browse the repository at this point in the history
Fixes #2748
  • Loading branch information
michalbundyra committed Dec 6, 2019
1 parent 2ecd8dc commit 1be4196
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2356,10 +2356,12 @@ public function findEndOfStatement($start, $ignore=null)
&& ($i === $this->tokens[$i]['scope_opener']
|| $i === $this->tokens[$i]['scope_condition'])
) {
if ($i === $start
&& (isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true
|| $this->tokens[$i]['code'] === T_FN)
) {
if ($this->tokens[$i]['code'] === T_FN) {
$i = ($this->tokens[$i]['scope_closer'] - 1);
continue;
}

if ($i === $start && isset(Util\Tokens::$scopeOpeners[$this->tokens[$i]['code']]) === true) {
return $this->tokens[$i]['scope_closer'];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ fn(callable $a) : callable => $a;

/* testTernary */
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';

/* testEndOfStatement */
static fn ($a) => $a;

return 0;
19 changes: 19 additions & 0 deletions tests/Core/Tokenizer/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,23 @@ private function backfillHelper($token)
}//end backfillHelper()


/**
* Test end of statement for fn closure.
*
* @return void
*/
public function testEndOfStatement()
{
$token = $this->getTargetToken('/* testEndOfStatement */', T_FN);
$this->backfillHelper($token);

$static = self::$phpcsFile->findPrevious(T_STATIC, ($token - 1));
$endOfStatementStatic = self::$phpcsFile->findEndOfStatement($static);
$endOfStatementFn = self::$phpcsFile->findEndOfStatement($token);

$this->assertSame($endOfStatementFn, $endOfStatementStatic);

}//end testEndOfStatement()


}//end class

0 comments on commit 1be4196

Please sign in to comment.