|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests that parentheses tokens are not converted to type parentheses tokens in broken DNF types. |
| 4 | + * |
| 5 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
| 6 | + * @copyright 2024 PHPCSStandards and contributors |
| 7 | + * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHP_CodeSniffer\Tests\Core\Tokenizer\PHP; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Tests\Core\Tokenizer\AbstractTokenizerTestCase; |
| 13 | +use PHP_CodeSniffer\Util\Tokens; |
| 14 | + |
| 15 | +final class DNFTypesParseError2Test extends AbstractTokenizerTestCase |
| 16 | +{ |
| 17 | + |
| 18 | + |
| 19 | + /** |
| 20 | + * Document handling for a DNF type / parse error where the type declaration contains an unmatched parenthesis. |
| 21 | + * |
| 22 | + * @param string $testMarker The comment prefacing the target token. |
| 23 | + * |
| 24 | + * @dataProvider dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens |
| 25 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public function testBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens($testMarker) |
| 30 | + { |
| 31 | + $tokens = $this->phpcsFile->getTokens(); |
| 32 | + |
| 33 | + // Verify that the type union is still tokenized as T_BITWISE_OR as the type declaration |
| 34 | + // is not recognized as a valid type declaration. |
| 35 | + $unionPtr = $this->getTargetToken($testMarker, [T_BITWISE_OR, T_TYPE_UNION], '|'); |
| 36 | + $token = $tokens[$unionPtr]; |
| 37 | + |
| 38 | + $this->assertSame(T_BITWISE_OR, $token['code'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (code)'); |
| 39 | + $this->assertSame('T_BITWISE_OR', $token['type'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (type)'); |
| 40 | + |
| 41 | + // Verify that the unmatched open parenthesis is tokenized as a normal parenthesis. |
| 42 | + $openPtr = $this->getTargetToken($testMarker, [T_OPEN_PARENTHESIS, T_TYPE_OPEN_PARENTHESIS], '('); |
| 43 | + $token = $tokens[$openPtr]; |
| 44 | + |
| 45 | + $this->assertSame(T_OPEN_PARENTHESIS, $token['code'], 'Token tokenized as '.$token['type'].', not T_OPEN_PARENTHESIS (code)'); |
| 46 | + $this->assertSame('T_OPEN_PARENTHESIS', $token['type'], 'Token tokenized as '.$token['type'].', not T_OPEN_PARENTHESIS (type)'); |
| 47 | + |
| 48 | + // Verify that the type intersection is still tokenized as T_BITWISE_AND as the type declaration |
| 49 | + // is not recognized as a valid type declaration. |
| 50 | + $intersectPtr = $this->getTargetToken($testMarker, [T_BITWISE_AND, T_TYPE_INTERSECTION], '&'); |
| 51 | + $token = $tokens[$intersectPtr]; |
| 52 | + |
| 53 | + $this->assertSame(T_BITWISE_AND, $token['code'], 'Token tokenized as '.$token['type'].', not T_BITWISE_AND (code)'); |
| 54 | + $this->assertSame('T_BITWISE_AND', $token['type'], 'Token tokenized as '.$token['type'].', not T_BITWISE_AND (type)'); |
| 55 | + |
| 56 | + }//end testBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens() |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * Data provider. |
| 61 | + * |
| 62 | + * @see testBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens() |
| 63 | + * |
| 64 | + * @return array<string, array<string, string>> |
| 65 | + */ |
| 66 | + public static function dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens() |
| 67 | + { |
| 68 | + return [ |
| 69 | + 'OO const type' => ['/* testBrokenConstDNFTypeParensMissingClose */'], |
| 70 | + 'OO property type' => ['/* testBrokenPropertyDNFTypeParensMissingClose */'], |
| 71 | + 'Parameter type' => ['/* testBrokenParamDNFTypeParensMissingClose */'], |
| 72 | + 'Return type' => ['/* testBrokenReturnDNFTypeParensMissingClose */'], |
| 73 | + ]; |
| 74 | + |
| 75 | + }//end dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingCloseParens() |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Document handling for a DNF type / parse error where the type declaration contains an unmatched parenthesis. |
| 80 | + * |
| 81 | + * @param string $testMarker The comment prefacing the target token. |
| 82 | + * |
| 83 | + * @dataProvider dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens |
| 84 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function testBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens($testMarker) |
| 89 | + { |
| 90 | + $tokens = $this->phpcsFile->getTokens(); |
| 91 | + |
| 92 | + // Verify that the type union is still tokenized as T_BITWISE_OR as the type declaration |
| 93 | + // is not recognized as a valid type declaration. |
| 94 | + $unionPtr = $this->getTargetToken($testMarker, [T_BITWISE_OR, T_TYPE_UNION], '|'); |
| 95 | + $token = $tokens[$unionPtr]; |
| 96 | + |
| 97 | + $this->assertSame(T_BITWISE_OR, $token['code'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (code)'); |
| 98 | + $this->assertSame('T_BITWISE_OR', $token['type'], 'Token tokenized as '.$token['type'].', not T_BITWISE_OR (type)'); |
| 99 | + |
| 100 | + // Verify that the unmatched open parenthesis is tokenized as a normal parenthesis. |
| 101 | + $closePtr = $this->getTargetToken($testMarker, [T_CLOSE_PARENTHESIS, T_TYPE_CLOSE_PARENTHESIS], ')'); |
| 102 | + $token = $tokens[$closePtr]; |
| 103 | + |
| 104 | + $this->assertSame(T_CLOSE_PARENTHESIS, $token['code'], 'Token tokenized as '.$token['type'].', not T_CLOSE_PARENTHESIS (code)'); |
| 105 | + $this->assertSame('T_CLOSE_PARENTHESIS', $token['type'], 'Token tokenized as '.$token['type'].', not T_CLOSE_PARENTHESIS (type)'); |
| 106 | + |
| 107 | + // Verify that the type intersection is still tokenized as T_BITWISE_AND as the type declaration |
| 108 | + // is not recognized as a valid type declaration. |
| 109 | + $intersectPtr = $this->getTargetToken($testMarker, [T_BITWISE_AND, T_TYPE_INTERSECTION], '&'); |
| 110 | + $token = $tokens[$intersectPtr]; |
| 111 | + |
| 112 | + $this->assertSame(T_BITWISE_AND, $token['code'], 'Token tokenized as '.$token['type'].', not T_BITWISE_AND (code)'); |
| 113 | + $this->assertSame('T_BITWISE_AND', $token['type'], 'Token tokenized as '.$token['type'].', not T_BITWISE_AND (type)'); |
| 114 | + |
| 115 | + }//end testBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens() |
| 116 | + |
| 117 | + |
| 118 | + /** |
| 119 | + * Data provider. |
| 120 | + * |
| 121 | + * @see testBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens() |
| 122 | + * |
| 123 | + * @return array<string, array<string, string>> |
| 124 | + */ |
| 125 | + public static function dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens() |
| 126 | + { |
| 127 | + return [ |
| 128 | + 'OO const type' => ['/* testBrokenConstDNFTypeParensMissingOpen */'], |
| 129 | + 'OO property type' => ['/* testBrokenPropertyDNFTypeParensMissingOpen */'], |
| 130 | + 'Parameter type' => ['/* testBrokenParamDNFTypeParensMissingOpen */'], |
| 131 | + 'Return type' => ['/* testBrokenReturnDNFTypeParensMissingOpen */'], |
| 132 | + ]; |
| 133 | + |
| 134 | + }//end dataBrokenDNFTypeParensShouldAlwaysBeAPairMissingOpenParens() |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Document handling for a DNF type / parse error where the type declaration contains an unmatched parenthesis, |
| 139 | + * but also contains a set of matched parentheses. |
| 140 | + * |
| 141 | + * @param string $testMarker The comment prefacing the target token. |
| 142 | + * |
| 143 | + * @dataProvider dataBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched |
| 144 | + * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional |
| 145 | + * |
| 146 | + * @return void |
| 147 | + */ |
| 148 | + public function testBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched($testMarker) |
| 149 | + { |
| 150 | + $tokens = $this->phpcsFile->getTokens(); |
| 151 | + $startPtr = $this->getTargetToken($testMarker, [T_OPEN_PARENTHESIS, T_TYPE_OPEN_PARENTHESIS], '('); |
| 152 | + |
| 153 | + for ($i = $startPtr; $i < $this->phpcsFile->numTokens; $i++) { |
| 154 | + if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + if ($tokens[$i]['code'] === T_EQUAL |
| 159 | + || $tokens[$i]['code'] === T_VARIABLE |
| 160 | + || $tokens[$i]['code'] === T_OPEN_CURLY_BRACKET |
| 161 | + ) { |
| 162 | + // Reached the end of the type. |
| 163 | + break; |
| 164 | + } |
| 165 | + |
| 166 | + $errorPrefix = 'Token tokenized as '.$tokens[$i]['type']; |
| 167 | + |
| 168 | + // Verify that type tokens have not been retokenized to `T_TYPE_*` tokens for broken type declarations. |
| 169 | + switch ($tokens[$i]['content']) { |
| 170 | + case '|': |
| 171 | + $this->assertSame(T_BITWISE_OR, $tokens[$i]['code'], $errorPrefix.', not T_BITWISE_OR (code)'); |
| 172 | + $this->assertSame('T_BITWISE_OR', $tokens[$i]['type'], $errorPrefix.', not T_BITWISE_OR (type)'); |
| 173 | + break; |
| 174 | + |
| 175 | + case '&': |
| 176 | + $this->assertSame(T_BITWISE_AND, $tokens[$i]['code'], $errorPrefix.', not T_BITWISE_AND (code)'); |
| 177 | + $this->assertSame('T_BITWISE_AND', $tokens[$i]['type'], $errorPrefix.', not T_BITWISE_AND (type)'); |
| 178 | + break; |
| 179 | + |
| 180 | + case '(': |
| 181 | + // Verify that the open parenthesis is tokenized as a normal parenthesis. |
| 182 | + $this->assertSame(T_OPEN_PARENTHESIS, $tokens[$i]['code'], $errorPrefix.', not T_OPEN_PARENTHESIS (code)'); |
| 183 | + $this->assertSame('T_OPEN_PARENTHESIS', $tokens[$i]['type'], $errorPrefix.', not T_OPEN_PARENTHESIS (type)'); |
| 184 | + break; |
| 185 | + |
| 186 | + case ')': |
| 187 | + $this->assertSame(T_CLOSE_PARENTHESIS, $tokens[$i]['code'], $errorPrefix.', not T_CLOSE_PARENTHESIS (code)'); |
| 188 | + $this->assertSame('T_CLOSE_PARENTHESIS', $tokens[$i]['type'], $errorPrefix.', not T_CLOSE_PARENTHESIS (type)'); |
| 189 | + break; |
| 190 | + |
| 191 | + default: |
| 192 | + break; |
| 193 | + }//end switch |
| 194 | + }//end for |
| 195 | + |
| 196 | + }//end testBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched() |
| 197 | + |
| 198 | + |
| 199 | + /** |
| 200 | + * Data provider. |
| 201 | + * |
| 202 | + * @see testBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched() |
| 203 | + * |
| 204 | + * @return array<string, array<string, string>> |
| 205 | + */ |
| 206 | + public static function dataBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched() |
| 207 | + { |
| 208 | + return [ |
| 209 | + 'OO const type - missing one close parenthesis' => ['/* testBrokenConstDNFTypeParensMissingOneClose */'], |
| 210 | + 'OO property type - missing one open parenthesis' => ['/* testBrokenPropertyDNFTypeParensMissingOneOpen */'], |
| 211 | + 'Parameter type - missing one close parenthesis' => ['/* testBrokenParamDNFTypeParensMissingOneClose */'], |
| 212 | + 'Return type - missing one open parenthesis' => ['/* testBrokenReturnDNFTypeParensMissingOneOpen */'], |
| 213 | + ]; |
| 214 | + |
| 215 | + }//end dataBrokenDNFTypeParensShouldAlwaysBeAPairMatchedAndUnmatched() |
| 216 | + |
| 217 | + |
| 218 | +}//end class |
0 commit comments