-
-
Notifications
You must be signed in to change notification settings - Fork 65
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
fredden
wants to merge
2
commits into
PHPCSStandards:master
Choose a base branch
from
fredden:issue-149
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||||||
{ | ||||||
|
||||||
|
||||||
/** | ||||||
* 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this |
||||||
* | ||||||
* @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>> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.