|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the tokenization of PHP open tags. |
| 4 | + * |
| 5 | + * Prior to PHP 7.4, PHP didn't support stand-alone PHP open tags at the end of a file (without a new line), |
| 6 | + * so we need to make sure that the tokenization in PHPCS is consistent and correct. |
| 7 | + * |
| 8 | + * @copyright 2025 PHPCSStandards and contributors |
| 9 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP; |
| 13 | + |
| 14 | +use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase; |
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests the tokenization of PHP open tags. |
| 19 | + * |
| 20 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 21 | + */ |
| 22 | +final class PHPOpenTagEOF3Test extends AbstractTokenizerTestCase |
| 23 | +{ |
| 24 | + |
| 25 | + |
| 26 | + /** |
| 27 | + * Test that the tokenization of a long PHP open tag at the very end of a file is correct and consistent. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + public function testLongOpenTagAtEndOfFile() |
| 32 | + { |
| 33 | + $tokens = $this->phpcsFile->getTokens(); |
| 34 | + $stackPtr = $this->getTargetToken('/* testLongOpenTagEndOfFileNoSpaceNoNewLineUppercase */', [T_OPEN_TAG, T_STRING, T_INLINE_HTML]); |
| 35 | + |
| 36 | + $this->assertSame( |
| 37 | + T_OPEN_TAG, |
| 38 | + $tokens[$stackPtr]['code'], |
| 39 | + 'Token tokenized as '.Tokens::tokenName($tokens[$stackPtr]['code']).', not T_OPEN_TAG (code)' |
| 40 | + ); |
| 41 | + $this->assertSame( |
| 42 | + 'T_OPEN_TAG', |
| 43 | + $tokens[$stackPtr]['type'], |
| 44 | + 'Token tokenized as '.$tokens[$stackPtr]['type'].', not T_OPEN_TAG (type)' |
| 45 | + ); |
| 46 | + $this->assertSame('<?PHP', $tokens[$stackPtr]['content']); |
| 47 | + |
| 48 | + // Now make sure that this is the very last token in the file and there are no tokens after it. |
| 49 | + $this->assertArrayNotHasKey(($stackPtr + 1), $tokens); |
| 50 | + |
| 51 | + }//end testLongOpenTagAtEndOfFile() |
| 52 | + |
| 53 | + |
| 54 | +}//end class |
0 commit comments