Skip to content
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

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

Draft
wants to merge 2 commits into
base: master
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 @@ -1142,6 +1142,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/Tokenizer/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/Tokenizer/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\Tokenizer;

use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;

final class ScopeOwnerTest extends AbstractMethodUnitTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change this to use the Tokenizer specific testcase I introduced in #314 ? That should allow code coverage to be recorded for this.

{


/**
* 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\PHP::tokenize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this @covers tag correct ? The change is in the Tokenizer::recurseScopeMap() method ?

*
* @return void
*/
public function testIfCondition($testMarker, $conditionWidth)
{
$tokens = self::$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 = self::$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 = self::$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>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return array<string, array<string, int>>
* @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