1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
<?php
/**
* Tests the backfilling of the T_FN token to PHP < 7.4.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;
use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
final class BackfillFnTokenTest extends AbstractTokenizerTestCase
{
/**
* Test simple arrow functions.
*
* @param string $testMarker The comment prefacing the target token.
*
* @dataProvider dataSimple
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testSimple($testMarker)
{
$token = $this->getTargetToken($testMarker, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 12);
}//end testSimple()
/**
* Data provider.
*
* @see testSimple()
*
* @return array<string, array<string, string>>
*/
public static function dataSimple()
{
return [
'standard' => [
'testMarker' => '/* testStandard */',
],
'mixed case' => [
'testMarker' => '/* testMixedCase */',
],
];
}//end dataSimple()
/**
* Test whitespace inside arrow function definitions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testWhitespace()
{
$token = $this->getTargetToken('/* testWhitespace */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 6, 13);
}//end testWhitespace()
/**
* Test comments inside arrow function definitions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testComment()
{
$token = $this->getTargetToken('/* testComment */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 8, 15);
}//end testComment()
/**
* Test heredocs inside arrow function definitions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testHeredoc()
{
$token = $this->getTargetToken('/* testHeredoc */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 4, 9);
}//end testHeredoc()
/**
* Test nested arrow functions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNestedOuter()
{
$token = $this->getTargetToken('/* testNestedOuter */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 25);
}//end testNestedOuter()
/**
* Test nested arrow functions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNestedInner()
{
$tokens = $this->phpcsFile->getTokens();
$token = $this->getTargetToken('/* testNestedInner */', T_FN);
$this->backfillHelper($token, true);
$expectedScopeOpener = ($token + 5);
$expectedScopeCloser = ($token + 16);
$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], 'Scope opener is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], 'Scope closer is not the semicolon token');
$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'Opener scope opener is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'Opener scope closer is not the semicolon token');
$closer = $tokens[$token]['scope_closer'];
$this->assertSame(($token - 4), $tokens[$closer]['scope_opener'], 'Closer scope opener is not the arrow token of the "outer" arrow function (shared scope closer)');
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'Closer scope closer is not the semicolon token');
}//end testNestedInner()
/**
* Test nested arrow functions with a shared closer.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNestedSharedCloser()
{
$tokens = $this->phpcsFile->getTokens();
$token = $this->getTargetToken('/* testNestedSharedCloserOuter */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 4, 20);
$token = $this->getTargetToken('/* testNestedSharedCloserInner */', T_FN);
$this->backfillHelper($token, true);
$expectedScopeOpener = ($token + 4);
$expectedScopeCloser = ($token + 12);
$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], 'Scope opener for "inner" arrow function is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], 'Scope closer for "inner" arrow function is not the TRUE token');
$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'Opener scope opener for "inner" arrow function is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'Opener scope closer for "inner" arrow function is not the semicolon token');
$closer = $tokens[$token]['scope_closer'];
$this->assertSame(($token - 4), $tokens[$closer]['scope_opener'], 'Closer scope opener for "inner" arrow function is not the arrow token of the "outer" arrow function (shared scope closer)');
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'Closer scope closer for "inner" arrow function is not the TRUE token');
}//end testNestedSharedCloser()
/**
* Test arrow functions that call functions.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testFunctionCall()
{
$token = $this->getTargetToken('/* testFunctionCall */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 17);
}//end testFunctionCall()
/**
* Test arrow functions that are included in chained calls.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testChainedFunctionCall()
{
$token = $this->getTargetToken('/* testChainedFunctionCall */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 12, 'bracket');
}//end testChainedFunctionCall()
/**
* Test arrow functions that are used as function arguments.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testFunctionArgument()
{
$token = $this->getTargetToken('/* testFunctionArgument */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 8, 15, 'comma');
}//end testFunctionArgument()
/**
* Test arrow functions that use closures.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testClosure()
{
$token = $this->getTargetToken('/* testClosure */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 60, 'comma');
}//end testClosure()
/**
* Test arrow functions using an array index.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testArrayIndex()
{
$token = $this->getTargetToken('/* testArrayIndex */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 8, 17, 'comma');
}//end testArrayIndex()
/**
* Test arrow functions with a return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testReturnType()
{
$token = $this->getTargetToken('/* testReturnType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 18, 'comma');
}//end testReturnType()
/**
* Test arrow functions that return a reference.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testReference()
{
$token = $this->getTargetToken('/* testReference */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 6, 9);
}//end testReference()
/**
* Test arrow functions that are grouped by parenthesis.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testGrouped()
{
$token = $this->getTargetToken('/* testGrouped */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 8);
}//end testGrouped()
/**
* Test arrow functions that are used as array values.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testArrayValue()
{
$token = $this->getTargetToken('/* testArrayValue */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 4, 9, 'comma');
}//end testArrayValue()
/**
* Test arrow functions that are used as array values with no trailing comma.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testArrayValueNoTrailingComma()
{
$token = $this->getTargetToken('/* testArrayValueNoTrailingComma */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 4, 8, 'closing parenthesis');
}//end testArrayValueNoTrailingComma()
/**
* Test arrow functions that use the yield keyword.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testYield()
{
$token = $this->getTargetToken('/* testYield */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 14);
}//end testYield()
/**
* Test arrow functions that use nullable namespace types.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNullableNamespace()
{
$token = $this->getTargetToken('/* testNullableNamespace */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 15, 18);
}//end testNullableNamespace()
/**
* Test arrow functions that use the namespace operator in the return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNamespaceOperatorInTypes()
{
$token = $this->getTargetToken('/* testNamespaceOperatorInTypes */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 16, 19);
}//end testNamespaceOperatorInTypes()
/**
* Test arrow functions that use keyword return types.
*
* @param string $testMarker The comment prefacing the target token.
*
* @dataProvider dataKeywordReturnTypes
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testKeywordReturnTypes($testMarker)
{
$token = $this->getTargetToken($testMarker, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 14);
}//end testKeywordReturnTypes()
/**
* Data provider.
*
* @see testKeywordReturnTypes()
*
* @return array<string, array<string, string>>
*/
public static function dataKeywordReturnTypes()
{
return [
'self' => [
'testMarker' => '/* testSelfReturnType */',
],
'parent' => [
'testMarker' => '/* testParentReturnType */',
],
'callable' => [
'testMarker' => '/* testCallableReturnType */',
],
'array' => [
'testMarker' => '/* testArrayReturnType */',
],
'static' => [
'testMarker' => '/* testStaticReturnType */',
],
'false' => [
'testMarker' => '/* testFalseReturnType */',
],
'true' => [
'testMarker' => '/* testTrueReturnType */',
],
'null' => [
'testMarker' => '/* testNullReturnType */',
],
];
}//end dataKeywordReturnTypes()
/**
* Test arrow function with a union parameter type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testUnionParamType()
{
$token = $this->getTargetToken('/* testUnionParamType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 13, 21);
}//end testUnionParamType()
/**
* Test arrow function with a union return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testUnionReturnType()
{
$token = $this->getTargetToken('/* testUnionReturnType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 18);
}//end testUnionReturnType()
/**
* Test arrow function with a union return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testUnionReturnTypeWithTrue()
{
$token = $this->getTargetToken('/* testUnionReturnTypeWithTrue */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 18);
}//end testUnionReturnTypeWithTrue()
/**
* Test arrow function with a union return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testUnionReturnTypeWithFalse()
{
$token = $this->getTargetToken('/* testUnionReturnTypeWithFalse */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 11, 18);
}//end testUnionReturnTypeWithFalse()
/**
* Test arrow function with an intersection parameter type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testIntersectionParamType()
{
$token = $this->getTargetToken('/* testIntersectionParamType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 13, 27);
}//end testIntersectionParamType()
/**
* Test arrow function with an intersection return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testIntersectionReturnType()
{
$token = $this->getTargetToken('/* testIntersectionReturnType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 12, 20);
}//end testIntersectionReturnType()
/**
* Test arrow function with a DNF parameter type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testDNFParamType()
{
$token = $this->getTargetToken('/* testDNFParamType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 17, 29);
}//end testDNFParamType()
/**
* Test arrow function with a DNF return type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testDNFReturnType()
{
$token = $this->getTargetToken('/* testDNFReturnType */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 16, 29);
}//end testDNFReturnType()
/**
* Test arrow function which returns by reference with a DNF parameter type.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testDNFParamTypeWithReturnByRef()
{
$token = $this->getTargetToken('/* testDNFParamTypeWithReturnByRef */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 15, 22);
}//end testDNFParamTypeWithReturnByRef()
/**
* Test arrow functions used in ternary operators.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testTernary()
{
$tokens = $this->phpcsFile->getTokens();
$token = $this->getTargetToken('/* testTernary */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 40);
$token = $this->getTargetToken('/* testTernaryThen */', T_FN);
$this->backfillHelper($token);
$expectedScopeOpener = ($token + 8);
$expectedScopeCloser = ($token + 12);
$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], 'Scope opener for THEN is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], 'Scope closer for THEN is not the semicolon token');
$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'Opener scope opener for THEN is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'Opener scope closer for THEN is not the semicolon token');
$closer = $tokens[$token]['scope_closer'];
$this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], 'Closer scope opener for THEN is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'Closer scope closer for THEN is not the semicolon token');
$token = $this->getTargetToken('/* testTernaryElse */', T_FN);
$this->backfillHelper($token, true);
$expectedScopeOpener = ($token + 8);
$expectedScopeCloser = ($token + 11);
$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], 'Scope opener for ELSE is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], 'Scope closer for ELSE is not the semicolon token');
$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'Opener scope opener for ELSE is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'Opener scope closer for ELSE is not the semicolon token');
$closer = $tokens[$token]['scope_closer'];
$this->assertSame(($token - 24), $tokens[$closer]['scope_opener'], 'Closer scope opener for ELSE is not the arrow token of the "outer" arrow function (shared scope closer)');
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'Closer scope closer for ELSE is not the semicolon token');
}//end testTernary()
/**
* Test typed arrow functions used in ternary operators.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testTernaryWithTypes()
{
$token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 15, 27);
}//end testTernaryWithTypes()
/**
* Test arrow function returning a match control structure.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testWithMatchValue()
{
$token = $this->getTargetToken('/* testWithMatchValue */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 44);
}//end testWithMatchValue()
/**
* Test arrow function returning a match control structure with something behind it.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testWithMatchValueAndMore()
{
$token = $this->getTargetToken('/* testWithMatchValueAndMore */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 48);
}//end testWithMatchValueAndMore()
/**
* Test match control structure returning arrow functions.
*
* @param string $testMarker The comment prefacing the target token.
* @param int $openerOffset The expected offset of the scope opener in relation to the testMarker.
* @param int $closerOffset The expected offset of the scope closer in relation to the testMarker.
* @param string $expectedCloserType The type of token expected for the scope closer.
* @param string $expectedCloserFriendlyName A friendly name for the type of token expected for the scope closer
* to be used in the error message for failing tests.
*
* @dataProvider dataInMatchValue
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testInMatchValue($testMarker, $openerOffset, $closerOffset, $expectedCloserType, $expectedCloserFriendlyName)
{
$tokens = $this->phpcsFile->getTokens();
$token = $this->getTargetToken($testMarker, T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, $openerOffset, $closerOffset, $expectedCloserFriendlyName);
$this->assertSame($expectedCloserType, $tokens[($token + $closerOffset)]['type'], 'Mismatched scope closer type');
}//end testInMatchValue()
/**
* Data provider.
*
* @see testInMatchValue()
*
* @return array<string, array<string, string|int>>
*/
public static function dataInMatchValue()
{
return [
'not_last_value' => [
'testMarker' => '/* testInMatchNotLastValue */',
'openerOffset' => 5,
'closerOffset' => 11,
'expectedCloserType' => 'T_COMMA',
'expectedCloserFriendlyName' => 'comma',
],
'last_value_with_trailing_comma' => [
'testMarker' => '/* testInMatchLastValueWithTrailingComma */',
'openerOffset' => 5,
'closerOffset' => 11,
'expectedCloserType' => 'T_COMMA',
'expectedCloserFriendlyName' => 'comma',
],
'last_value_without_trailing_comma_1' => [
'testMarker' => '/* testInMatchLastValueNoTrailingComma1 */',
'openerOffset' => 5,
'closerOffset' => 10,
'expectedCloserType' => 'T_CLOSE_PARENTHESIS',
'expectedCloserFriendlyName' => 'close parenthesis',
],
'last_value_without_trailing_comma_2' => [
'testMarker' => '/* testInMatchLastValueNoTrailingComma2 */',
'openerOffset' => 5,
'closerOffset' => 11,
'expectedCloserType' => 'T_VARIABLE',
'expectedCloserFriendlyName' => '$y variable',
],
];
}//end dataInMatchValue()
/**
* Test arrow function nested within a method declaration.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNestedInMethod()
{
$token = $this->getTargetToken('/* testNestedInMethod */', T_FN);
$this->backfillHelper($token);
$this->scopePositionTestHelper($token, 5, 17);
}//end testNestedInMethod()
/**
* Verify that "fn" keywords which are not arrow functions get tokenized as T_STRING and don't
* have the extra token array indexes.
*
* @param string $testMarker The comment prefacing the target token.
* @param string $testContent The token content to look for.
*
* @dataProvider dataNotAnArrowFunction
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
*
* @return void
*/
public function testNotAnArrowFunction($testMarker, $testContent='fn')
{
$tokens = $this->phpcsFile->getTokens();
$token = $this->getTargetToken($testMarker, [T_STRING, T_FN], $testContent);
$tokenArray = $tokens[$token];
$this->assertSame('T_STRING', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_STRING');
$this->assertArrayNotHasKey('scope_condition', $tokenArray, 'Scope condition is set');
$this->assertArrayNotHasKey('scope_opener', $tokenArray, 'Scope opener is set');
$this->assertArrayNotHasKey('scope_closer', $tokenArray, 'Scope closer is set');
$this->assertArrayNotHasKey('parenthesis_owner', $tokenArray, 'Parenthesis owner is set');
$this->assertArrayNotHasKey('parenthesis_opener', $tokenArray, 'Parenthesis opener is set');
$this->assertArrayNotHasKey('parenthesis_closer', $tokenArray, 'Parenthesis closer is set');
}//end testNotAnArrowFunction()
/**
* Data provider.
*
* @see testNotAnArrowFunction()
*
* @return array<string, array<string, string>>
*/
public static function dataNotAnArrowFunction()
{
return [
'name of a function, context: declaration' => [
'testMarker' => '/* testFunctionName */',
],
'name of a constant, context: declaration using "const" keyword - uppercase' => [
'testMarker' => '/* testConstantDeclaration */',
'testContent' => 'FN',
],
'name of a constant, context: declaration using "const" keyword - lowercase' => [
'testMarker' => '/* testConstantDeclarationLower */',
'testContent' => 'fn',
],
'name of a (static) method, context: declaration' => [
'testMarker' => '/* testStaticMethodName */',
],
'name of a property, context: property access' => [
'testMarker' => '/* testPropertyAssignment */',
],
'name of a method, context: declaration in an anon class - mixed case' => [
'testMarker' => '/* testAnonClassMethodName */',
'testContent' => 'fN',
],
'name of a method, context: static method call' => [
'testMarker' => '/* testNonArrowStaticMethodCall */',
],
'name of a constant, context: constant access - uppercase' => [
'testMarker' => '/* testNonArrowConstantAccess */',
'testContent' => 'FN',
],
'name of a constant, context: constant access - mixed case' => [
'testMarker' => '/* testNonArrowConstantAccessMixed */',
'testContent' => 'Fn',
],
'name of a method, context: method call on object - lowercase' => [
'testMarker' => '/* testNonArrowObjectMethodCall */',
],
'name of a method, context: method call on object - uppercase' => [
'testMarker' => '/* testNonArrowObjectMethodCallUpper */',
'testContent' => 'FN',
],
'name of a (namespaced) function, context: partially qualified function call' => [
'testMarker' => '/* testNonArrowNamespacedFunctionCall */',
'testContent' => 'Fn',
],
'name of a (namespaced) function, context: namespace relative function call' => [
'testMarker' => '/* testNonArrowNamespaceOperatorFunctionCall */',
],
'name of a function, context: declaration with union types for param and return' => [
'testMarker' => '/* testNonArrowFunctionNameWithUnionTypes */',
],
'unknown - live coding/parse error' => [
'testMarker' => '/* testLiveCoding */',
],
];
}//end dataNotAnArrowFunction()
/**
* Helper function to check that all token keys are correctly set for T_FN tokens.
*
* @param int $token The T_FN token to check.
* @param bool $skipScopeCloserCheck Whether to skip the scope closer check.
* This should be set to "true" when testing nested arrow functions,
* where the "inner" arrow function shares a scope closer with the
* "outer" arrow function, as the 'scope_condition' for the scope closer
* of the "inner" arrow function will point to the "outer" arrow function.
*
* @return void
*/
private function backfillHelper($token, $skipScopeCloserCheck=false)
{
$tokens = $this->phpcsFile->getTokens();
$this->assertTrue(array_key_exists('scope_condition', $tokens[$token]), 'Scope condition is not set');
$this->assertTrue(array_key_exists('scope_opener', $tokens[$token]), 'Scope opener is not set');
$this->assertTrue(array_key_exists('scope_closer', $tokens[$token]), 'Scope closer is not set');
$this->assertSame($tokens[$token]['scope_condition'], $token, 'Scope condition is not the T_FN token');
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$token]), 'Parenthesis owner is not set');
$this->assertTrue(array_key_exists('parenthesis_opener', $tokens[$token]), 'Parenthesis opener is not set');
$this->assertTrue(array_key_exists('parenthesis_closer', $tokens[$token]), 'Parenthesis closer is not set');
$this->assertSame($tokens[$token]['parenthesis_owner'], $token, 'Parenthesis owner is not the T_FN token');
$opener = $tokens[$token]['scope_opener'];
$this->assertTrue(array_key_exists('scope_condition', $tokens[$opener]), 'Opener scope condition is not set');
$this->assertTrue(array_key_exists('scope_opener', $tokens[$opener]), 'Opener scope opener is not set');
$this->assertTrue(array_key_exists('scope_closer', $tokens[$opener]), 'Opener scope closer is not set');
$this->assertSame($tokens[$opener]['scope_condition'], $token, 'Opener scope condition is not the T_FN token');
$this->assertSame(T_FN_ARROW, $tokens[$opener]['code'], 'Arrow scope opener not tokenized as T_FN_ARROW (code)');
$this->assertSame('T_FN_ARROW', $tokens[$opener]['type'], 'Arrow scope opener not tokenized as T_FN_ARROW (type)');
$closer = $tokens[$token]['scope_closer'];
$this->assertTrue(array_key_exists('scope_condition', $tokens[$closer]), 'Closer scope condition is not set');
$this->assertTrue(array_key_exists('scope_opener', $tokens[$closer]), 'Closer scope opener is not set');
$this->assertTrue(array_key_exists('scope_closer', $tokens[$closer]), 'Closer scope closer is not set');
if ($skipScopeCloserCheck === false) {
$this->assertSame($tokens[$closer]['scope_condition'], $token, 'Closer scope condition is not the T_FN token');
}
$opener = $tokens[$token]['parenthesis_opener'];
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$opener]), 'Opening parenthesis owner is not set');
$this->assertSame($tokens[$opener]['parenthesis_owner'], $token, 'Opening parenthesis owner is not the T_FN token');
$closer = $tokens[$token]['parenthesis_closer'];
$this->assertTrue(array_key_exists('parenthesis_owner', $tokens[$closer]), 'Closing parenthesis owner is not set');
$this->assertSame($tokens[$closer]['parenthesis_owner'], $token, 'Closing parenthesis owner is not the T_FN token');
}//end backfillHelper()
/**
* Helper function to check that the scope opener/closer positions are correctly set for T_FN tokens.
*
* @param int $token The T_FN token to check.
* @param int $openerOffset The expected offset of the scope opener in relation to
* the fn keyword.
* @param int $closerOffset The expected offset of the scope closer in relation to
* the fn keyword.
* @param string $expectedCloserType Optional. The type of token expected for the scope closer.
*
* @return void
*/
private function scopePositionTestHelper($token, $openerOffset, $closerOffset, $expectedCloserType='semicolon')
{
$tokens = $this->phpcsFile->getTokens();
$expectedScopeOpener = ($token + $openerOffset);
$expectedScopeCloser = ($token + $closerOffset);
$this->assertSame($expectedScopeOpener, $tokens[$token]['scope_opener'], 'Scope opener is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$token]['scope_closer'], 'Scope closer is not the '.$expectedCloserType.' token');
$opener = $tokens[$token]['scope_opener'];
$this->assertSame($expectedScopeOpener, $tokens[$opener]['scope_opener'], 'Opener scope opener is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$opener]['scope_closer'], 'Opener scope closer is not the '.$expectedCloserType.' token');
$closer = $tokens[$token]['scope_closer'];
$this->assertSame($expectedScopeOpener, $tokens[$closer]['scope_opener'], 'Closer scope opener is not the arrow token');
$this->assertSame($expectedScopeCloser, $tokens[$closer]['scope_closer'], 'Closer scope closer is not the '.$expectedCloserType.' token');
}//end scopePositionTestHelper()
}//end class
|