|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the retokenization of ? to T_NULLABLE or T_INLINE_THEN. |
| 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 | + |
| 13 | +/** |
| 14 | + * Tests the retokenization of ? to T_NULLABLE or T_INLINE_THEN. |
| 15 | + * |
| 16 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 17 | + */ |
| 18 | +final class NullableVsInlineThenTest extends AbstractTokenizerTestCase |
| 19 | +{ |
| 20 | + |
| 21 | + |
| 22 | + /** |
| 23 | + * Test that the ? at the start of type declarations is retokenized to T_NULLABLE. |
| 24 | + * |
| 25 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 26 | + * |
| 27 | + * @dataProvider dataNullable |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + public function testNullable($testMarker) |
| 32 | + { |
| 33 | + $tokens = $this->phpcsFile->getTokens(); |
| 34 | + $target = $this->getTargetToken($testMarker, [T_NULLABLE, T_INLINE_THEN]); |
| 35 | + $tokenArray = $tokens[$target]; |
| 36 | + |
| 37 | + $this->assertSame(T_NULLABLE, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_NULLABLE (code)'); |
| 38 | + $this->assertSame('T_NULLABLE', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_NULLABLE (type)'); |
| 39 | + |
| 40 | + }//end testNullable() |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * Data provider. |
| 45 | + * |
| 46 | + * @see testNullable() |
| 47 | + * |
| 48 | + * @return array<string, array<string>> |
| 49 | + */ |
| 50 | + public static function dataNullable() |
| 51 | + { |
| 52 | + return [ |
| 53 | + 'property declaration, readonly, no visibility' => ['/* testNullableReadonlyOnly */'], |
| 54 | + ]; |
| 55 | + |
| 56 | + }//end dataNullable() |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Test that "readonly" when not used as the keyword is still tokenized as `T_STRING`. |
| 61 | + * |
| 62 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 63 | + * |
| 64 | + * @dataProvider dataInlineThen |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testInlineThen($testMarker) |
| 69 | + { |
| 70 | + $tokens = $this->phpcsFile->getTokens(); |
| 71 | + $target = $this->getTargetToken($testMarker, [T_NULLABLE, T_INLINE_THEN]); |
| 72 | + $tokenArray = $tokens[$target]; |
| 73 | + |
| 74 | + $this->assertSame(T_INLINE_THEN, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_INLINE_THEN (code)'); |
| 75 | + $this->assertSame('T_INLINE_THEN', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_INLINE_THEN (type)'); |
| 76 | + |
| 77 | + }//end testInlineThen() |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Data provider. |
| 82 | + * |
| 83 | + * @see testInlineThen() |
| 84 | + * |
| 85 | + * @return array<string, array<string>> |
| 86 | + */ |
| 87 | + public static function dataInlineThen() |
| 88 | + { |
| 89 | + return [ |
| 90 | + 'ternary in property default value' => ['/* testInlineThenInPropertyDefaultValue */'], |
| 91 | + ]; |
| 92 | + |
| 93 | + }//end dataInlineThen() |
| 94 | + |
| 95 | + |
| 96 | +}//end class |
0 commit comments