Skip to content

Fix tokenizer bug - Heredoc in if condition broke scope mapping #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Tokenizers/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,13 @@ private function recurseScopeMap($stackPtr, $depth=1, &$ignore=0)
continue;
}

if ($tokenType === T_START_HEREDOC) {
// Heredocs are special because they can be used as a value, including
// inside a function call or as a default value for a parameter.
// So if we find them nested inside another opener, just skip them.
continue;
}

if ($tokenType === T_FUNCTION
&& $this->tokens[$stackPtr]['code'] !== T_FUNCTION
) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Core/Tokenizers/PHP/ScopeOwnerTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/* testNormalIfCondition */
if (true) {
return;
}

/* testFunctionCallInIfCondition */
if (doThing(0)) {
return;
}

/* testHeredocInIfCondition */
// Ref: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/149
if (foo(<<<EOD
foobar!
EOD
)) {
return;
}
77 changes: 77 additions & 0 deletions tests/Core/Tokenizers/PHP/ScopeOwnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Tests that scope owner (scope_condition, conditions) is set correctly
*
* @author Dan Wallis <[email protected]>
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;

final class ScopeOwnerTest extends AbstractTokenizerTestCase
{


/**
* Test that a basic 'if' condition gets scope opener/closer set as expected.
*
* @param string $testMarker The comment which prefaces the target token in the test file.
* @param int $conditionWidth How many tokens wide the 'if' condition should be.
*
* @dataProvider dataIfCondition
* @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap
*
* @return void
*/
public function testIfCondition($testMarker, $conditionWidth)
{
$tokens = $this->phpcsFile->getTokens();
$targetIf = $this->getTargetToken($testMarker, T_IF);

$this->assertSame($targetIf, $tokens[$targetIf]['parenthesis_owner'], 'parenthesis owner is self for if');
$this->assertSame(($targetIf + 2), $tokens[$targetIf]['parenthesis_opener'], 'expected parenthesis opener');
$this->assertSame(($targetIf + 2 + $conditionWidth), $tokens[$targetIf]['parenthesis_closer'], 'expected parenthesis closer');
$this->assertSame($targetIf, $tokens[$targetIf]['scope_condition']);

$targetOpenCurly = $this->phpcsFile->findNext(T_OPEN_CURLY_BRACKET, $targetIf);
$this->assertSame($targetIf, $tokens[$targetOpenCurly]['scope_condition'], 'scope_condition set on open curly');

$targetCloseCurly = $tokens[$targetOpenCurly]['bracket_closer'];
$this->assertSame($targetIf, $tokens[$targetCloseCurly]['scope_condition'], 'scope_condition set on close curly');

$targetReturn = $this->phpcsFile->findNext(T_RETURN, $targetIf);
$this->assertSame([$targetIf => T_IF], $tokens[$targetReturn]['conditions'], 'conditions set on if statement body');

}//end testIfCondition()


/**
* Data provider.
*
* @see testIfCondition()
*
* @return array<string, array<string, int|string>>
*/
public static function dataIfCondition()
{
return [
'Basic if' => [
'testMarker' => '/* testNormalIfCondition */',
'conditionWidth' => 2,
],
'Function call in if condition' => [
'testMarker' => '/* testFunctionCallInIfCondition */',
'conditionWidth' => 5,
],
'Heredoc in if condition' => [
'testMarker' => '/* testHeredocInIfCondition */',
'conditionWidth' => 8,
],
];

}//end dataIfCondition()


}//end class