|
3 | 3 | * Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
|
4 | 4 | *
|
5 | 5 | * @author Greg Sherwood <gsherwood@squiz.net>
|
| 6 | + * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl> |
6 | 7 | * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
| 8 | + * @copyright 2019-2024 PHPCSStandards Contributors |
7 | 9 | * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
8 | 10 | */
|
9 | 11 |
|
@@ -505,4 +507,134 @@ public function testOpenTagWithEcho()
|
505 | 507 | }//end testOpenTagWithEcho()
|
506 | 508 |
|
507 | 509 |
|
| 510 | + /** |
| 511 | + * Test object call on result of static function call with arrow function as parameter and wrapped within an array. |
| 512 | + * |
| 513 | + * @link https://github.com/squizlabs/PHP_CodeSniffer/issues/2849 |
| 514 | + * @link https://github.com/squizlabs/PHP_CodeSniffer/commit/fbf67efc3fc0c2a355f5585d49f4f6fe160ff2f9 |
| 515 | + * |
| 516 | + * @return void |
| 517 | + */ |
| 518 | + public function testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray() |
| 519 | + { |
| 520 | + $expected = $this->getTargetToken('/* testPrecededByArrowFunctionInArray - Expected */', T_STRING, 'Url'); |
| 521 | + |
| 522 | + $start = $this->getTargetToken('/* testPrecededByArrowFunctionInArray */', T_STRING, 'onlyOnDetail'); |
| 523 | + $found = self::$phpcsFile->findStartOfStatement($start); |
| 524 | + |
| 525 | + $this->assertSame($expected, $found); |
| 526 | + |
| 527 | + }//end testObjectCallPrecededByArrowFunctionAsFunctionCallParameterInArray() |
| 528 | + |
| 529 | + |
| 530 | + /** |
| 531 | + * Test finding the start of a statement inside a switch control structure case/default statement. |
| 532 | + * |
| 533 | + * @param string $testMarker The comment which prefaces the target token in the test file. |
| 534 | + * @param int|string $targets The token to search for after the test marker. |
| 535 | + * @param string|int $expectedTarget Token code of the expected start of statement stack pointer. |
| 536 | + * |
| 537 | + * @link https://github.com/squizlabs/php_codesniffer/issues/3192 |
| 538 | + * @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3186/commits/18a0e54735bb9b3850fec266e5f4c50dacf618ea |
| 539 | + * |
| 540 | + * @dataProvider dataFindStartInsideSwitchCaseDefaultStatements |
| 541 | + * |
| 542 | + * @return void |
| 543 | + */ |
| 544 | + public function testFindStartInsideSwitchCaseDefaultStatements($testMarker, $targets, $expectedTarget) |
| 545 | + { |
| 546 | + $testToken = $this->getTargetToken($testMarker, $targets); |
| 547 | + $expected = $this->getTargetToken($testMarker, $expectedTarget); |
| 548 | + |
| 549 | + $found = self::$phpcsFile->findStartOfStatement($testToken); |
| 550 | + |
| 551 | + $this->assertSame($expected, $found); |
| 552 | + |
| 553 | + }//end testFindStartInsideSwitchCaseDefaultStatements() |
| 554 | + |
| 555 | + |
| 556 | + /** |
| 557 | + * Data provider. |
| 558 | + * |
| 559 | + * @return array<string, array<string, int|string>> |
| 560 | + */ |
| 561 | + public static function dataFindStartInsideSwitchCaseDefaultStatements() |
| 562 | + { |
| 563 | + return [ |
| 564 | + 'Case keyword should be start of case statement - case itself' => [ |
| 565 | + 'testMarker' => '/* testCaseStatement */', |
| 566 | + 'targets' => T_CASE, |
| 567 | + 'expectedTarget' => T_CASE, |
| 568 | + ], |
| 569 | + 'Case keyword should be start of case statement - number (what\'s being compared)' => [ |
| 570 | + 'testMarker' => '/* testCaseStatement */', |
| 571 | + 'targets' => T_LNUMBER, |
| 572 | + 'expectedTarget' => T_CASE, |
| 573 | + ], |
| 574 | + 'Variable should be start of arbitrary assignment statement - variable itself' => [ |
| 575 | + 'testMarker' => '/* testInsideCaseStatement */', |
| 576 | + 'targets' => T_VARIABLE, |
| 577 | + 'expectedTarget' => T_VARIABLE, |
| 578 | + ], |
| 579 | + 'Variable should be start of arbitrary assignment statement - equal sign' => [ |
| 580 | + 'testMarker' => '/* testInsideCaseStatement */', |
| 581 | + 'targets' => T_EQUAL, |
| 582 | + 'expectedTarget' => T_VARIABLE, |
| 583 | + ], |
| 584 | + 'Variable should be start of arbitrary assignment statement - function call' => [ |
| 585 | + 'testMarker' => '/* testInsideCaseStatement */', |
| 586 | + 'targets' => T_STRING, |
| 587 | + 'expectedTarget' => T_VARIABLE, |
| 588 | + ], |
| 589 | + 'Break should be start for contents of the break statement - contents' => [ |
| 590 | + 'testMarker' => '/* testInsideCaseBreakStatement */', |
| 591 | + 'targets' => T_LNUMBER, |
| 592 | + 'expectedTarget' => T_BREAK, |
| 593 | + ], |
| 594 | + 'Continue should be start for contents of the continue statement - contents' => [ |
| 595 | + 'testMarker' => '/* testInsideCaseContinueStatement */', |
| 596 | + 'targets' => T_LNUMBER, |
| 597 | + 'expectedTarget' => T_CONTINUE, |
| 598 | + ], |
| 599 | + 'Return should be start for contents of the return statement - contents' => [ |
| 600 | + 'testMarker' => '/* testInsideCaseReturnStatement */', |
| 601 | + 'targets' => T_FALSE, |
| 602 | + 'expectedTarget' => T_RETURN, |
| 603 | + ], |
| 604 | + 'Exit should be start for contents of the exit statement - close parenthesis' => [ |
| 605 | + // Note: not sure if this is actually correct - should this be the open parenthesis ? |
| 606 | + 'testMarker' => '/* testInsideCaseExitStatement */', |
| 607 | + 'targets' => T_CLOSE_PARENTHESIS, |
| 608 | + 'expectedTarget' => T_EXIT, |
| 609 | + ], |
| 610 | + 'Throw should be start for contents of the throw statement - new keyword' => [ |
| 611 | + 'testMarker' => '/* testInsideCaseThrowStatement */', |
| 612 | + 'targets' => T_NEW, |
| 613 | + 'expectedTarget' => T_THROW, |
| 614 | + ], |
| 615 | + 'Throw should be start for contents of the throw statement - exception name' => [ |
| 616 | + 'testMarker' => '/* testInsideCaseThrowStatement */', |
| 617 | + 'targets' => T_STRING, |
| 618 | + 'expectedTarget' => T_THROW, |
| 619 | + ], |
| 620 | + 'Throw should be start for contents of the throw statement - close parenthesis' => [ |
| 621 | + 'testMarker' => '/* testInsideCaseThrowStatement */', |
| 622 | + 'targets' => T_CLOSE_PARENTHESIS, |
| 623 | + 'expectedTarget' => T_THROW, |
| 624 | + ], |
| 625 | + 'Default keyword should be start of default statement - default itself' => [ |
| 626 | + 'testMarker' => '/* testDefaultStatement */', |
| 627 | + 'targets' => T_DEFAULT, |
| 628 | + 'expectedTarget' => T_DEFAULT, |
| 629 | + ], |
| 630 | + 'Return should be start for contents of the return statement (inside default) - variable' => [ |
| 631 | + 'testMarker' => '/* testInsideDefaultContinueStatement */', |
| 632 | + 'targets' => T_VARIABLE, |
| 633 | + 'expectedTarget' => T_CONTINUE, |
| 634 | + ], |
| 635 | + ]; |
| 636 | + |
| 637 | + }//end dataFindStartInsideSwitchCaseDefaultStatements() |
| 638 | + |
| 639 | + |
508 | 640 | }//end class
|
0 commit comments