|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that the array keyword is tokenized correctly. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <[email protected]> |
| 6 | + * @copyright 2021 Squiz Pty Ltd (ABN 77 084 670 600) |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase; |
| 13 | + |
| 14 | +final class CreateTokenMapArrayParenthesesTest extends AbstractTokenizerTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + |
| 18 | + /** |
| 19 | + * Test that the array keyword is correctly tokenized as `T_ARRAY`. |
| 20 | + * |
| 21 | + * @param string $testMarker The comment prefacing the target token. |
| 22 | + * @param string $testContent Optional. The token content to look for. |
| 23 | + * |
| 24 | + * @dataProvider dataArrayKeyword |
| 25 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testArrayKeyword($testMarker, $testContent='array') |
| 30 | + { |
| 31 | + $tokens = $this->phpcsFile->getTokens(); |
| 32 | + |
| 33 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 34 | + $tokenArray = $tokens[$token]; |
| 35 | + |
| 36 | + // Make sure we're looking at the right token. |
| 37 | + $this->assertSame(T_ARRAY, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (code)'); |
| 38 | + |
| 39 | + $this->assertArrayHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is not set'); |
| 40 | + $this->assertArrayHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is not set'); |
| 41 | + $this->assertArrayHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is not set'); |
| 42 | + |
| 43 | + }//end testArrayKeyword() |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Data provider. |
| 48 | + * |
| 49 | + * @see testArrayKeyword() |
| 50 | + * |
| 51 | + * @return array<string, array<string, string>> |
| 52 | + */ |
| 53 | + public static function dataArrayKeyword() |
| 54 | + { |
| 55 | + return [ |
| 56 | + 'empty array' => [ |
| 57 | + 'testMarker' => '/* testEmptyArray */', |
| 58 | + ], |
| 59 | + 'array with space before parenthesis' => [ |
| 60 | + 'testMarker' => '/* testArrayWithSpace */', |
| 61 | + ], |
| 62 | + 'array with comment before parenthesis' => [ |
| 63 | + 'testMarker' => '/* testArrayWithComment */', |
| 64 | + 'testContent' => 'Array', |
| 65 | + ], |
| 66 | + 'nested: outer array' => [ |
| 67 | + 'testMarker' => '/* testNestingArray */', |
| 68 | + ], |
| 69 | + 'nested: inner array' => [ |
| 70 | + 'testMarker' => '/* testNestedArray */', |
| 71 | + ], |
| 72 | + 'OO constant default value' => [ |
| 73 | + 'testMarker' => '/* testOOConstDefault */', |
| 74 | + ], |
| 75 | + ]; |
| 76 | + |
| 77 | + }//end dataArrayKeyword() |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Test that the array keyword when used in a type declaration is correctly tokenized as `T_STRING`. |
| 82 | + * |
| 83 | + * @param string $testMarker The comment prefacing the target token. |
| 84 | + * @param string $testContent Optional. The token content to look for. |
| 85 | + * |
| 86 | + * @dataProvider dataArrayType |
| 87 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function testArrayType($testMarker, $testContent='array') |
| 92 | + { |
| 93 | + $tokens = $this->phpcsFile->getTokens(); |
| 94 | + |
| 95 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 96 | + $tokenArray = $tokens[$token]; |
| 97 | + |
| 98 | + // Make sure we're looking at the right token. |
| 99 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 100 | + |
| 101 | + $this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set'); |
| 102 | + $this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set'); |
| 103 | + $this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set'); |
| 104 | + |
| 105 | + }//end testArrayType() |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * Data provider. |
| 110 | + * |
| 111 | + * @see testArrayType() |
| 112 | + * |
| 113 | + * @return array<string, array<string, string>> |
| 114 | + */ |
| 115 | + public static function dataArrayType() |
| 116 | + { |
| 117 | + return [ |
| 118 | + 'closure return type' => [ |
| 119 | + 'testMarker' => '/* testClosureReturnType */', |
| 120 | + 'testContent' => 'Array', |
| 121 | + ], |
| 122 | + 'function param type' => [ |
| 123 | + 'testMarker' => '/* testFunctionDeclarationParamType */', |
| 124 | + ], |
| 125 | + 'function union return type' => [ |
| 126 | + 'testMarker' => '/* testFunctionDeclarationReturnType */', |
| 127 | + ], |
| 128 | + 'OO constant type' => [ |
| 129 | + 'testMarker' => '/* testOOConstType */', |
| 130 | + ], |
| 131 | + 'OO property type' => [ |
| 132 | + 'testMarker' => '/* testOOPropertyType */', |
| 133 | + ], |
| 134 | + |
| 135 | + 'OO constant DNF type' => [ |
| 136 | + 'testMarker' => '/* testOOConstDNFType */', |
| 137 | + ], |
| 138 | + 'OO property DNF type' => [ |
| 139 | + 'testMarker' => '/* testOOPropertyDNFType */', |
| 140 | + 'testContent' => 'ARRAY', |
| 141 | + ], |
| 142 | + 'function param DNF type' => [ |
| 143 | + 'testMarker' => '/* testFunctionDeclarationParamDNFType */', |
| 144 | + ], |
| 145 | + 'closure param DNF type' => [ |
| 146 | + 'testMarker' => '/* testClosureDeclarationParamDNFType */', |
| 147 | + ], |
| 148 | + 'arrow return DNF type' => [ |
| 149 | + 'testMarker' => '/* testArrowDeclarationReturnDNFType */', |
| 150 | + 'testContent' => 'Array', |
| 151 | + ], |
| 152 | + ]; |
| 153 | + |
| 154 | + }//end dataArrayType() |
| 155 | + |
| 156 | + |
| 157 | + /** |
| 158 | + * Verify that the retokenization of `T_ARRAY` tokens to `T_STRING` is handled correctly |
| 159 | + * for tokens with the contents 'array' which aren't in actual fact the array keyword. |
| 160 | + * |
| 161 | + * @param string $testMarker The comment prefacing the target token. |
| 162 | + * @param string $testContent The token content to look for. |
| 163 | + * |
| 164 | + * @dataProvider dataNotArrayKeyword |
| 165 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 166 | + * |
| 167 | + * @return void |
| 168 | + */ |
| 169 | + public function testNotArrayKeyword($testMarker, $testContent='array') |
| 170 | + { |
| 171 | + $tokens = $this->phpcsFile->getTokens(); |
| 172 | + |
| 173 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 174 | + $tokenArray = $tokens[$token]; |
| 175 | + |
| 176 | + // Make sure we're looking at the right token. |
| 177 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 178 | + |
| 179 | + $this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set'); |
| 180 | + $this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set'); |
| 181 | + $this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set'); |
| 182 | + |
| 183 | + }//end testNotArrayKeyword() |
| 184 | + |
| 185 | + |
| 186 | + /** |
| 187 | + * Data provider. |
| 188 | + * |
| 189 | + * @see testNotArrayKeyword() |
| 190 | + * |
| 191 | + * @return array<string, array<string, string>> |
| 192 | + */ |
| 193 | + public static function dataNotArrayKeyword() |
| 194 | + { |
| 195 | + return [ |
| 196 | + 'class-constant-name' => [ |
| 197 | + 'testMarker' => '/* testClassConst */', |
| 198 | + 'testContent' => 'ARRAY', |
| 199 | + ], |
| 200 | + 'class-method-name' => [ |
| 201 | + 'testMarker' => '/* testClassMethod */', |
| 202 | + ], |
| 203 | + 'class-constant-name-after-type' => [ |
| 204 | + 'testMarker' => '/* testTypedOOConstName */', |
| 205 | + 'testContent' => 'ARRAY', |
| 206 | + ], |
| 207 | + ]; |
| 208 | + |
| 209 | + }//end dataNotArrayKeyword() |
| 210 | + |
| 211 | + |
| 212 | +}//end class |
0 commit comments