Skip to content

Commit ce62dee

Browse files
committed
Fixed bug #2926 : phpcs hangs when using arrow functions that return heredoc
1 parent a30f08c commit ce62dee

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
29+
- Fixed bug #2926 : phpcs hangs when using arrow functions that return heredoc
2930
- Fixed bug #2943 : Redundant semicolon added to a file when fixing PSR2.Files.ClosingTag.NotAllowed
3031
</notes>
3132
<contents>

src/Tokenizers/PHP.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,8 @@ protected function processAdditional()
18681868

18691869
if (isset($this->tokens[$scopeCloser]['scope_closer']) === true
18701870
&& $this->tokens[$scopeCloser]['code'] !== T_INLINE_ELSE
1871+
&& $this->tokens[$scopeCloser]['code'] !== T_END_HEREDOC
1872+
&& $this->tokens[$scopeCloser]['code'] !== T_END_NOWDOC
18711873
) {
18721874
// We minus 1 here in case the closer can be shared with us.
18731875
$scopeCloser = ($this->tokens[$scopeCloser]['scope_closer'] - 1);

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ $fn1 = fn ($x) => $x + $y;
1212
/* testComment */
1313
$fn1 = fn /* comment here */ ($x) => $x + $y;
1414

15+
/* testHeredoc */
16+
$fn1 = fn() => <<<HTML
17+
fn
18+
HTML;
19+
1520
/* testFunctionName */
1621
function fn() {}
1722

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function testWhitespace()
8080
*
8181
* @return void
8282
*/
83-
public function testComments()
83+
public function testComment()
8484
{
8585
$tokens = self::$phpcsFile->getTokens();
8686

@@ -98,7 +98,35 @@ public function testComments()
9898
$this->assertSame($tokens[$closer]['scope_opener'], ($token + 8), 'Closer scope opener is not the arrow token');
9999
$this->assertSame($tokens[$closer]['scope_closer'], ($token + 15), 'Closer scope closer is not the semicolon token');
100100

101-
}//end testComments()
101+
}//end testComment()
102+
103+
104+
/**
105+
* Test heredocs inside arrow function definitions.
106+
*
107+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
108+
*
109+
* @return void
110+
*/
111+
public function testHeredoc()
112+
{
113+
$tokens = self::$phpcsFile->getTokens();
114+
115+
$token = $this->getTargetToken('/* testHeredoc */', T_FN);
116+
$this->backfillHelper($token);
117+
118+
$this->assertSame($tokens[$token]['scope_opener'], ($token + 4), 'Scope opener is not the arrow token');
119+
$this->assertSame($tokens[$token]['scope_closer'], ($token + 9), 'Scope closer is not the semicolon token');
120+
121+
$opener = $tokens[$token]['scope_opener'];
122+
$this->assertSame($tokens[$opener]['scope_opener'], ($token + 4), 'Opener scope opener is not the arrow token');
123+
$this->assertSame($tokens[$opener]['scope_closer'], ($token + 9), 'Opener scope closer is not the semicolon token');
124+
125+
$closer = $tokens[$token]['scope_opener'];
126+
$this->assertSame($tokens[$closer]['scope_opener'], ($token + 4), 'Closer scope opener is not the arrow token');
127+
$this->assertSame($tokens[$closer]['scope_closer'], ($token + 9), 'Closer scope closer is not the semicolon token');
128+
129+
}//end testHeredoc()
102130

103131

104132
/**

0 commit comments

Comments
 (0)