|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the conversion of PHP native context sensitive keywords to T_STRING when used in a "goto" statement. |
| 4 | + * |
| 5 | + * @copyright 2025 PHPCSStandards and contributors |
| 6 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 7 | + */ |
| 8 | + |
| 9 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP; |
| 10 | + |
| 11 | +use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase; |
| 12 | +use PHP_CodeSniffer\Util\Tokens; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests the conversion of PHP native context sensitive keywords to T_STRING when used in a "goto" statement. |
| 16 | + * |
| 17 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 18 | + */ |
| 19 | +final class ContextSensitiveKeywordsGotoTest extends AbstractTokenizerTestCase |
| 20 | +{ |
| 21 | + |
| 22 | + |
| 23 | + /** |
| 24 | + * Test that context sensitive keyword is tokenized as string when used in a "goto" statement. |
| 25 | + * |
| 26 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 27 | + * |
| 28 | + * @dataProvider dataStrings |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function testStrings($testMarker) |
| 33 | + { |
| 34 | + $tokens = $this->phpcsFile->getTokens(); |
| 35 | + $target = $this->getTargetToken($testMarker, (Tokens::$contextSensitiveKeywords + [T_STRING])); |
| 36 | + $tokenArray = $tokens[$target]; |
| 37 | + |
| 38 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 39 | + $this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)'); |
| 40 | + |
| 41 | + }//end testStrings() |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Data provider. |
| 46 | + * |
| 47 | + * @see testStrings() |
| 48 | + * |
| 49 | + * @return array<string, array<string>> |
| 50 | + */ |
| 51 | + public static function dataStrings() |
| 52 | + { |
| 53 | + return [ |
| 54 | + 'abstract' => ['/* testAbstract */'], |
| 55 | + 'array' => ['/* testArray */'], |
| 56 | + 'as' => ['/* testAs */'], |
| 57 | + 'break' => ['/* testBreak */'], |
| 58 | + 'callable' => ['/* testCallable */'], |
| 59 | + 'case' => ['/* testCase */'], |
| 60 | + 'catch' => ['/* testCatch */'], |
| 61 | + 'class' => ['/* testClass */'], |
| 62 | + 'clone' => ['/* testClone */'], |
| 63 | + 'const' => ['/* testConst */'], |
| 64 | + 'continue' => ['/* testContinue */'], |
| 65 | + 'declare' => ['/* testDeclare */'], |
| 66 | + 'default' => ['/* testDefault */'], |
| 67 | + 'do' => ['/* testDo */'], |
| 68 | + 'echo' => ['/* testEcho */'], |
| 69 | + 'else' => ['/* testElse */'], |
| 70 | + 'elseif' => ['/* testElseIf */'], |
| 71 | + 'empty' => ['/* testEmpty */'], |
| 72 | + 'enddeclare' => ['/* testEndDeclare */'], |
| 73 | + 'endfor' => ['/* testEndFor */'], |
| 74 | + 'endforeach' => ['/* testEndForeach */'], |
| 75 | + 'endif' => ['/* testEndIf */'], |
| 76 | + 'endswitch' => ['/* testEndSwitch */'], |
| 77 | + 'endwhile' => ['/* testEndWhile */'], |
| 78 | + 'enum' => ['/* testEnum */'], |
| 79 | + 'eval' => ['/* testEval */'], |
| 80 | + 'exit' => ['/* testExit */'], |
| 81 | + 'extends' => ['/* testExtends */'], |
| 82 | + 'final' => ['/* testFinal */'], |
| 83 | + 'finally' => ['/* testFinally */'], |
| 84 | + 'fn' => ['/* testFn */'], |
| 85 | + 'for' => ['/* testFor */'], |
| 86 | + 'foreach' => ['/* testForeach */'], |
| 87 | + 'function' => ['/* testFunction */'], |
| 88 | + 'global' => ['/* testGlobal */'], |
| 89 | + 'goto' => ['/* testGoto */'], |
| 90 | + 'if' => ['/* testIf */'], |
| 91 | + 'implements' => ['/* testImplements */'], |
| 92 | + 'include' => ['/* testInclude */'], |
| 93 | + 'include_once' => ['/* testIncludeOnce */'], |
| 94 | + 'instanceof' => ['/* testInstanceOf */'], |
| 95 | + 'insteadof' => ['/* testInsteadOf */'], |
| 96 | + 'interface' => ['/* testInterface */'], |
| 97 | + 'isset' => ['/* testIsset */'], |
| 98 | + 'list' => ['/* testList */'], |
| 99 | + 'match' => ['/* testMatch */'], |
| 100 | + 'namespace' => ['/* testNamespace */'], |
| 101 | + 'new' => ['/* testNew */'], |
| 102 | + 'print' => ['/* testPrint */'], |
| 103 | + 'private' => ['/* testPrivate */'], |
| 104 | + 'protected' => ['/* testProtected */'], |
| 105 | + 'public' => ['/* testPublic */'], |
| 106 | + 'readonly' => ['/* testReadonly */'], |
| 107 | + 'require' => ['/* testRequire */'], |
| 108 | + 'require_once' => ['/* testRequireOnce */'], |
| 109 | + 'return' => ['/* testReturn */'], |
| 110 | + 'static' => ['/* testStatic */'], |
| 111 | + 'switch' => ['/* testSwitch */'], |
| 112 | + 'throws' => ['/* testThrows */'], |
| 113 | + 'trait' => ['/* testTrait */'], |
| 114 | + 'try' => ['/* testTry */'], |
| 115 | + 'unset' => ['/* testUnset */'], |
| 116 | + 'use' => ['/* testUse */'], |
| 117 | + 'var' => ['/* testVar */'], |
| 118 | + 'while' => ['/* testWhile */'], |
| 119 | + 'yield' => ['/* testYield */'], |
| 120 | + 'yield_from' => ['/* testYieldFrom */'], |
| 121 | + 'and' => ['/* testAnd */'], |
| 122 | + 'or' => ['/* testOr */'], |
| 123 | + 'xor' => ['/* testXor */'], |
| 124 | + ]; |
| 125 | + |
| 126 | + }//end dataStrings() |
| 127 | + |
| 128 | + |
| 129 | +}//end class |
0 commit comments