forked from squizlabs/PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tokenizer/PHP: stabilize T_FINALLY backfill
Make the backfill for T_FINALLY a little more stable by preventing re-tokenizing non-keyword `finally` strings to `T_FINALLY`. Includes unit tests.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
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
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,40 @@ | ||
<?php | ||
|
||
try { | ||
// Do something. | ||
} catch(Exception $e) { | ||
// Do something. | ||
} | ||
/* testTryCatchFinally */ | ||
finally { | ||
// Do something. | ||
} | ||
|
||
/* testTryFinallyCatch */ | ||
try { | ||
// Do something. | ||
} finally { | ||
// Do something. | ||
} catch(Exception $e) { | ||
// Do something. | ||
} | ||
|
||
/* testTryFinally */ | ||
try { | ||
// Do something. | ||
} FINALLY { | ||
// Do something. | ||
} | ||
|
||
class FinallyAsMethod { | ||
/* testFinallyUsedAsClassConstantName */ | ||
const FINALLY = 'foo'; | ||
|
||
/* testFinallyUsedAsMethodName */ | ||
public function finally() { | ||
// Do something. | ||
|
||
/* testFinallyUsedAsPropertyName */ | ||
$this->finally = 'foo'; | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
/** | ||
* Tests the tokenization of the finally keyword. | ||
* | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
* @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Tests\Core\Tokenizer; | ||
|
||
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; | ||
|
||
class FinallyTest extends AbstractMethodUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Test that the finally keyword is tokenized as such. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* | ||
* @dataProvider dataFinallyKeyword | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testFinallyKeyword($testMarker) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, [T_FINALLY, T_STRING]); | ||
$this->assertSame(T_FINALLY, $tokens[$target]['code']); | ||
$this->assertSame('T_FINALLY', $tokens[$target]['type']); | ||
|
||
}//end testFinallyKeyword() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testFinallyKeyword() | ||
* | ||
* @return array | ||
*/ | ||
public function dataFinallyKeyword() | ||
{ | ||
return [ | ||
['/* testTryCatchFinally */'], | ||
['/* testTryFinallyCatch */'], | ||
['/* testTryFinally */'], | ||
]; | ||
|
||
}//end dataFinallyKeyword() | ||
|
||
|
||
/** | ||
* Test that 'finally' when not used as the reserved keyword is tokenized as `T_STRING`. | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* | ||
* @dataProvider dataFinallyNonKeyword | ||
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize | ||
* | ||
* @return void | ||
*/ | ||
public function testFinallyNonKeyword($testMarker) | ||
{ | ||
$tokens = self::$phpcsFile->getTokens(); | ||
|
||
$target = $this->getTargetToken($testMarker, [T_FINALLY, T_STRING]); | ||
$this->assertSame(T_STRING, $tokens[$target]['code']); | ||
$this->assertSame('T_STRING', $tokens[$target]['type']); | ||
|
||
}//end testFinallyNonKeyword() | ||
|
||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testFinallyNonKeyword() | ||
* | ||
* @return array | ||
*/ | ||
public function dataFinallyNonKeyword() | ||
{ | ||
return [ | ||
['/* testFinallyUsedAsClassConstantName */'], | ||
['/* testFinallyUsedAsMethodName */'], | ||
['/* testFinallyUsedAsPropertyName */'], | ||
]; | ||
|
||
}//end dataFinallyNonKeyword() | ||
|
||
|
||
}//end class |