|
| 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/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest; |
| 13 | + |
| 14 | +class ArrayKeywordTest extends AbstractMethodUnitTest |
| 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\PHP::tokenize |
| 26 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function testArrayKeyword($testMarker, $testContent='array') |
| 31 | + { |
| 32 | + $tokens = self::$phpcsFile->getTokens(); |
| 33 | + |
| 34 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 35 | + $tokenArray = $tokens[$token]; |
| 36 | + |
| 37 | + $this->assertSame(T_ARRAY, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (code)'); |
| 38 | + $this->assertSame('T_ARRAY', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_ARRAY (type)'); |
| 39 | + |
| 40 | + $this->assertArrayHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is not set'); |
| 41 | + $this->assertArrayHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is not set'); |
| 42 | + $this->assertArrayHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is not set'); |
| 43 | + |
| 44 | + }//end testArrayKeyword() |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * Data provider. |
| 49 | + * |
| 50 | + * @see testArrayKeyword() |
| 51 | + * |
| 52 | + * @return array |
| 53 | + */ |
| 54 | + public function dataArrayKeyword() |
| 55 | + { |
| 56 | + return [ |
| 57 | + 'empty array' => ['/* testEmptyArray */'], |
| 58 | + 'array with space before parenthesis' => ['/* testArrayWithSpace */'], |
| 59 | + 'array with comment before parenthesis' => [ |
| 60 | + '/* testArrayWithComment */', |
| 61 | + 'Array', |
| 62 | + ], |
| 63 | + 'nested: outer array' => ['/* testNestingArray */'], |
| 64 | + 'nested: inner array' => ['/* testNestedArray */'], |
| 65 | + ]; |
| 66 | + |
| 67 | + }//end dataArrayKeyword() |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Test that the array keyword when used in a type declaration is correctly tokenized as `T_STRING`. |
| 72 | + * |
| 73 | + * @param string $testMarker The comment prefacing the target token. |
| 74 | + * @param string $testContent Optional. The token content to look for. |
| 75 | + * |
| 76 | + * @dataProvider dataArrayType |
| 77 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 78 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function testArrayType($testMarker, $testContent='array') |
| 83 | + { |
| 84 | + $tokens = self::$phpcsFile->getTokens(); |
| 85 | + |
| 86 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 87 | + $tokenArray = $tokens[$token]; |
| 88 | + |
| 89 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 90 | + $this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)'); |
| 91 | + |
| 92 | + $this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set'); |
| 93 | + $this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set'); |
| 94 | + $this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set'); |
| 95 | + |
| 96 | + }//end testArrayType() |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * Data provider. |
| 101 | + * |
| 102 | + * @see testArrayType() |
| 103 | + * |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + public function dataArrayType() |
| 107 | + { |
| 108 | + return [ |
| 109 | + 'closure return type' => [ |
| 110 | + '/* testClosureReturnType */', |
| 111 | + 'Array', |
| 112 | + ], |
| 113 | + 'function param type' => ['/* testFunctionDeclarationParamType */'], |
| 114 | + 'function union return type' => ['/* testFunctionDeclarationReturnType */'], |
| 115 | + ]; |
| 116 | + |
| 117 | + }//end dataArrayType() |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + * Verify that the retokenization of `T_ARRAY` tokens to `T_STRING` is handled correctly |
| 122 | + * for tokens with the contents 'array' which aren't in actual fact the array keyword. |
| 123 | + * |
| 124 | + * @param string $testMarker The comment prefacing the target token. |
| 125 | + * @param string $testContent The token content to look for. |
| 126 | + * |
| 127 | + * @dataProvider dataNotArrayKeyword |
| 128 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize |
| 129 | + * @covers PHP_CodeSniffer\Tokenizers\Tokenizer::createTokenMap |
| 130 | + * |
| 131 | + * @return void |
| 132 | + */ |
| 133 | + public function testNotArrayKeyword($testMarker, $testContent='array') |
| 134 | + { |
| 135 | + $tokens = self::$phpcsFile->getTokens(); |
| 136 | + |
| 137 | + $token = $this->getTargetToken($testMarker, [T_ARRAY, T_STRING], $testContent); |
| 138 | + $tokenArray = $tokens[$token]; |
| 139 | + |
| 140 | + $this->assertSame(T_STRING, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (code)'); |
| 141 | + $this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING (type)'); |
| 142 | + |
| 143 | + $this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set'); |
| 144 | + $this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set'); |
| 145 | + $this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set'); |
| 146 | + |
| 147 | + }//end testNotArrayKeyword() |
| 148 | + |
| 149 | + |
| 150 | + /** |
| 151 | + * Data provider. |
| 152 | + * |
| 153 | + * @see testNotArrayKeyword() |
| 154 | + * |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + public function dataNotArrayKeyword() |
| 158 | + { |
| 159 | + return [ |
| 160 | + 'class-constant-name' => [ |
| 161 | + '/* testClassConst */', |
| 162 | + 'ARRAY', |
| 163 | + ], |
| 164 | + 'class-method-name' => ['/* testClassMethod */'], |
| 165 | + ]; |
| 166 | + |
| 167 | + }//end dataNotArrayKeyword() |
| 168 | + |
| 169 | + |
| 170 | +}//end class |
0 commit comments