|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that parentheses tokens are not converted to type parentheses tokens in broken DNF types. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer\PHP; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase; |
| 13 | + |
| 14 | +final class DNFTypesParseError1Test extends AbstractTokenizerTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Document handling for a DNF type / parse error where the last significant type specific token is an open parenthesis. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment prefacing the target token. |
| 22 | + * |
| 23 | + * @dataProvider dataBrokenDNFTypeCantEndOnOpenParenthesis |
| 24 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function testBrokenDNFTypeCantEndOnOpenParenthesis($testMarker) |
| 29 | + { |
| 30 | + $tokens = $this->phpcsFile->getTokens(); |
| 31 | + |
| 32 | + $openPtr = $this->getTargetToken($testMarker, [T_OPEN_PARENTHESIS, T_TYPE_OPEN_PARENTHESIS], '('); |
| 33 | + $token = $tokens[$openPtr]; |
| 34 | + |
| 35 | + // Verify that the open parenthesis is tokenized as a normal parenthesis. |
| 36 | + $this->assertSame(T_OPEN_PARENTHESIS, $token['code'], 'Token tokenized as '.$token['type'].', not T_OPEN_PARENTHESIS (code)'); |
| 37 | + $this->assertSame('T_OPEN_PARENTHESIS', $token['type'], 'Token tokenized as '.$token['type'].', not T_OPEN_PARENTHESIS (type)'); |
| 38 | + |
| 39 | + // Verify that the type union is still tokenized as T_BITWISE_OR as the type declaration |
| 40 | + // is not recognized as a valid type declaration. |
| 41 | + $unionPtr = $this->getTargetToken($testMarker, [T_BITWISE_OR, T_TYPE_UNION], '|'); |
| 42 | + $token = $tokens[$unionPtr]; |
| 43 | + |
| 44 | + $this->assertSame(T_BITWISE_OR, $token['code'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (code)'); |
| 45 | + $this->assertSame('T_BITWISE_OR', $token['type'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (type)'); |
| 46 | + |
| 47 | + }//end testBrokenDNFTypeCantEndOnOpenParenthesis() |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Data provider. |
| 52 | + * |
| 53 | + * @see testBrokenDNFTypeCantEndOnOpenParenthesis() |
| 54 | + * |
| 55 | + * @return array<string, array<string, string>> |
| 56 | + */ |
| 57 | + public static function dataBrokenDNFTypeCantEndOnOpenParenthesis() |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'OO const type' => ['/* testBrokenConstDNFTypeEndOnOpenParenthesis */'], |
| 61 | + 'OO property type' => ['/* testBrokenPropertyDNFTypeEndOnOpenParenthesis */'], |
| 62 | + 'Parameter type' => ['/* testBrokenParamDNFTypeEndOnOpenParenthesis */'], |
| 63 | + 'Return type' => ['/* testBrokenReturnDNFTypeEndOnOpenParenthesis */'], |
| 64 | + ]; |
| 65 | + |
| 66 | + }//end dataBrokenDNFTypeCantEndOnOpenParenthesis() |
| 67 | + |
| 68 | + |
| 69 | +}//end class |
0 commit comments