|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the retokenization of the `default` keyword to T_MATCH_DEFAULT for PHP 8.0 match structures |
| 4 | + * and makes sure that the tokenization of switch `T_DEFAULT` structures is not aversely affected. |
| 5 | + * |
| 6 | + * @author Juliette Reinders Folmer <[email protected]> |
| 7 | + * @copyright 2020-2021 Squiz Pty Ltd (ABN 77 084 670 600) |
| 8 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 14 | + |
| 15 | +class DefaultKeywordTest extends AbstractMethodUnitTest |
| 16 | +{ |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Test the retokenization of the `default` keyword for match structure to `T_MATCH_DEFAULT`. |
| 21 | + * |
| 22 | + * Note: Cases and default structures within a match structure do *NOT* get case/default scope |
| 23 | + * conditions, in contrast to case and default structures in switch control structures. |
| 24 | + * |
| 25 | + * @param string $testMarker The comment prefacing the target token. |
| 26 | + * |
| 27 | + * @dataProvider dataMatchDefault |
| 28 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 29 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function testMatchDefault($testMarker) |
| 34 | + { |
| 35 | + $tokens = self::$phpcsFile->getTokens(); |
| 36 | + |
| 37 | + $token = $this->getTargetToken($testMarker, [T_MATCH_DEFAULT, T_DEFAULT]); |
| 38 | + $tokenArray = $tokens[$token]; |
| 39 | + |
| 40 | + $this->assertSame(T_MATCH_DEFAULT, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_DEFAULT (code)'); |
| 41 | + $this->assertSame('T_MATCH_DEFAULT', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_DEFAULT (type)'); |
| 42 | + |
| 43 | + $this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set'); |
| 44 | + $this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set'); |
| 45 | + $this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set'); |
| 46 | + |
| 47 | + }//end testMatchDefault() |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Data provider. |
| 52 | + * |
| 53 | + * @see testMatchDefault() |
| 54 | + * |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function dataMatchDefault() |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'simple_match_default' => ['/* testSimpleMatchDefault */'], |
| 61 | + 'match_default_in_switch_case_1' => ['/* testMatchDefaultNestedInSwitchCase1 */'], |
| 62 | + 'match_default_in_switch_case_2' => ['/* testMatchDefaultNestedInSwitchCase2 */'], |
| 63 | + 'match_default_in_switch_default' => ['/* testMatchDefaultNestedInSwitchDefault */'], |
| 64 | + 'match_default_containing_switch' => ['/* testMatchDefault */'], |
| 65 | + ]; |
| 66 | + |
| 67 | + }//end dataMatchDefault() |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Verify that the retokenization of `T_DEFAULT` tokens in match constructs, doesn't negatively |
| 72 | + * impact the tokenization of `T_DEFAULT` tokens in switch control structures. |
| 73 | + * |
| 74 | + * Note: Cases and default structures within a switch control structure *do* get case/default scope |
| 75 | + * conditions. |
| 76 | + * |
| 77 | + * @param string $testMarker The comment prefacing the target token. |
| 78 | + * @param int $openerOffset The expected offset of the scope opener in relation to the testMarker. |
| 79 | + * @param int $closerOffset The expected offset of the scope closer in relation to the testMarker. |
| 80 | + * @param int|null $conditionStop The expected offset at which tokens stop having T_DEFAULT as a scope condition. |
| 81 | + * |
| 82 | + * @dataProvider dataSwitchDefault |
| 83 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::recurseScopeMap |
| 84 | + * |
| 85 | + * @return void |
| 86 | + */ |
| 87 | + public function testSwitchDefault($testMarker, $openerOffset, $closerOffset, $conditionStop=null) |
| 88 | + { |
| 89 | + $tokens = self::$phpcsFile->getTokens(); |
| 90 | + |
| 91 | + $token = $this->getTargetToken($testMarker, [T_MATCH_DEFAULT, T_DEFAULT]); |
| 92 | + $tokenArray = $tokens[$token]; |
| 93 | + $expectedScopeOpener = ($token + $openerOffset); |
| 94 | + $expectedScopeCloser = ($token + $closerOffset); |
| 95 | + |
| 96 | + $this->assertSame(T_DEFAULT, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_DEFAULT (code)'); |
| 97 | + $this->assertSame('T_DEFAULT', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_DEFAULT (type)'); |
| 98 | + |
| 99 | + $this->assertArrayHasKey('scope_condition', $tokenArray, 'Scope condition is not set'); |
| 100 | + $this->assertArrayHasKey('scope_opener', $tokenArray, 'Scope opener is not set'); |
| 101 | + $this->assertArrayHasKey('scope_closer', $tokenArray, 'Scope closer is not set'); |
| 102 | + $this->assertSame($token, $tokenArray['scope_condition'], 'Scope condition is not the T_DEFAULT token'); |
| 103 | + $this->assertSame($expectedScopeOpener, $tokenArray['scope_opener'], 'Scope opener of the T_DEFAULT token incorrect'); |
| 104 | + $this->assertSame($expectedScopeCloser, $tokenArray['scope_closer'], 'Scope closer of the T_DEFAULT token incorrect'); |
| 105 | + |
| 106 | + $opener = $tokenArray['scope_opener']; |
| 107 | + $this->assertArrayHasKey('scope_condition', $tokens[$opener], 'Opener scope condition is not set'); |
| 108 | + $this->assertArrayHasKey('scope_opener', $tokens[$opener], 'Opener scope opener is not set'); |
| 109 | + $this->assertArrayHasKey('scope_closer', $tokens[$opener], 'Opener scope closer is not set'); |
| 110 | + $this->assertSame($token, $tokens[$opener]['scope_condition'], 'Opener scope condition is not the T_DEFAULT token'); |
| 111 | + $this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'T_DEFAULT opener scope opener token incorrect'); |
| 112 | + $this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'T_DEFAULT opener scope closer token incorrect'); |
| 113 | + |
| 114 | + $closer = $tokenArray['scope_closer']; |
| 115 | + $this->assertArrayHasKey('scope_condition', $tokens[$closer], 'Closer scope condition is not set'); |
| 116 | + $this->assertArrayHasKey('scope_opener', $tokens[$closer], 'Closer scope opener is not set'); |
| 117 | + $this->assertArrayHasKey('scope_closer', $tokens[$closer], 'Closer scope closer is not set'); |
| 118 | + $this->assertSame($token, $tokens[$closer]['scope_condition'], 'Closer scope condition is not the T_DEFAULT token'); |
| 119 | + $this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], 'T_DEFAULT closer scope opener token incorrect'); |
| 120 | + $this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'T_DEFAULT closer scope closer token incorrect'); |
| 121 | + |
| 122 | + if (($opener + 1) !== $closer) { |
| 123 | + $end = $closer; |
| 124 | + if (isset($conditionStop) === true) { |
| 125 | + $end = $conditionStop; |
| 126 | + } |
| 127 | + |
| 128 | + for ($i = ($opener + 1); $i < $end; $i++) { |
| 129 | + $this->assertArrayHasKey( |
| 130 | + $token, |
| 131 | + $tokens[$i]['conditions'], |
| 132 | + 'T_DEFAULT condition not added for token belonging to the T_DEFAULT structure' |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + }//end testSwitchDefault() |
| 138 | + |
| 139 | + |
| 140 | + /** |
| 141 | + * Data provider. |
| 142 | + * |
| 143 | + * @see testSwitchDefault() |
| 144 | + * |
| 145 | + * @return array |
| 146 | + */ |
| 147 | + public function dataSwitchDefault() |
| 148 | + { |
| 149 | + return [ |
| 150 | + 'simple_switch_default' => [ |
| 151 | + '/* testSimpleSwitchDefault */', |
| 152 | + 1, |
| 153 | + 4, |
| 154 | + ], |
| 155 | + 'simple_switch_default_with_curlies' => [ |
| 156 | + // For a default structure with curly braces, the scope opener |
| 157 | + // will be the open curly and the closer the close curly. |
| 158 | + // However, scope conditions will not be set for open to close, |
| 159 | + // but only for the open token up to the "break/return/continue" etc. |
| 160 | + '/* testSimpleSwitchDefaultWithCurlies */', |
| 161 | + 3, |
| 162 | + 12, |
| 163 | + 6, |
| 164 | + ], |
| 165 | + 'switch_default_toplevel' => [ |
| 166 | + '/* testSwitchDefault */', |
| 167 | + 1, |
| 168 | + 43, |
| 169 | + ], |
| 170 | + 'switch_default_nested_in_match_case' => [ |
| 171 | + '/* testSwitchDefaultNestedInMatchCase */', |
| 172 | + 1, |
| 173 | + 20, |
| 174 | + ], |
| 175 | + 'switch_default_nested_in_match_default' => [ |
| 176 | + '/* testSwitchDefaultNestedInMatchDefault */', |
| 177 | + 1, |
| 178 | + 18, |
| 179 | + ], |
| 180 | + ]; |
| 181 | + |
| 182 | + }//end dataSwitchDefault() |
| 183 | + |
| 184 | + |
| 185 | +}//end class |
0 commit comments