-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BCFile::getDeclarationName(): sync with upstream
The upstream File::getDeclarationName() method is slated to receive a bug fix in the PHPCS 3.12.0 release to prevent incorrect results for unfinished closures with typed parameters. The PHPCSUtils native `ObjectDeclarations::getName()` method already handled this correct. This commit polyfills the upstream method via the BCFile class and syncs the tests with the upstream changes. Ref: PHPCSStandards/PHP_CodeSniffer 816
- Loading branch information
Showing
9 changed files
with
307 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Tests/BackCompat/BCFile/GetDeclarationNameParseError1Test.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
/* testLiveCoding */ | ||
// Intentional parse error. This must be the only test in the file. | ||
function // Comment. |
61 changes: 61 additions & 0 deletions
61
Tests/BackCompat/BCFile/GetDeclarationNameParseError1Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. | ||
* | ||
* @package PHPCSUtils | ||
* @copyright 2019-2020 PHPCSUtils Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCSStandards/PHPCSUtils | ||
*/ | ||
|
||
namespace PHPCSUtils\Tests\BackCompat\BCFile; | ||
|
||
use PHPCSUtils\BackCompat\BCFile; | ||
use PHPCSUtils\Tests\PolyfilledTestCase; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\BackCompat\BCFile::getDeclarationName() method. | ||
* | ||
* @covers \PHPCSUtils\BackCompat\BCFile::getDeclarationName | ||
* | ||
* @group objectdeclarations | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class GetDeclarationNameParseError1Test extends PolyfilledTestCase | ||
{ | ||
|
||
/** | ||
* Test receiving "null" in case of a parse error. | ||
* | ||
* @dataProvider dataGetDeclarationNameNull | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param int|string $targetType Token type of the token to get as stackPtr. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetDeclarationNameNull($testMarker, $targetType) | ||
{ | ||
$target = $this->getTargetToken($testMarker, $targetType); | ||
$result = BCFile::getDeclarationName(self::$phpcsFile, $target); | ||
$this->assertNull($result); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testGetDeclarationNameNull() For the array format. | ||
* | ||
* @return array<string, array<string, int|string>> | ||
*/ | ||
public static function dataGetDeclarationNameNull() | ||
{ | ||
return [ | ||
'unfinished function/live coding' => [ | ||
'testMarker' => '/* testLiveCoding */', | ||
'targetType' => \T_FUNCTION, | ||
], | ||
]; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Tests/BackCompat/BCFile/GetDeclarationNameParseError2Test.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
/* testLiveCoding */ | ||
// Intentional parse error/live coding. This must be the only test in the file. | ||
// Safeguarding that the utility method does not confuse the `string` type with a function name. | ||
$closure = function (string $param) use ($var |
61 changes: 61 additions & 0 deletions
61
Tests/BackCompat/BCFile/GetDeclarationNameParseError2Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. | ||
* | ||
* @package PHPCSUtils | ||
* @copyright 2019-2020 PHPCSUtils Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCSStandards/PHPCSUtils | ||
*/ | ||
|
||
namespace PHPCSUtils\Tests\BackCompat\BCFile; | ||
|
||
use PHPCSUtils\BackCompat\BCFile; | ||
use PHPCSUtils\Tests\PolyfilledTestCase; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\BackCompat\BCFile::getDeclarationName() method. | ||
* | ||
* @covers \PHPCSUtils\BackCompat\BCFile::getDeclarationName | ||
* | ||
* @group objectdeclarations | ||
* | ||
* @since 1.1.0 | ||
*/ | ||
class GetDeclarationNameParseError2Test extends PolyfilledTestCase | ||
{ | ||
|
||
/** | ||
* Test receiving "null" in case of a parse error. | ||
* | ||
* @dataProvider dataGetDeclarationNameNull | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param int|string $targetType Token type of the token to get as stackPtr. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetDeclarationNameNull($testMarker, $targetType) | ||
{ | ||
$target = $this->getTargetToken($testMarker, $targetType); | ||
$result = BCFile::getDeclarationName(self::$phpcsFile, $target); | ||
$this->assertNull($result); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @see testGetDeclarationNameNull() For the array format. | ||
* | ||
* @return array<string, array<string, int|string>> | ||
*/ | ||
public static function dataGetDeclarationNameNull() | ||
{ | ||
return [ | ||
'unfinished closure/live coding' => [ | ||
'testMarker' => '/* testLiveCoding */', | ||
'targetType' => \T_FUNCTION, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. | ||
* | ||
* @package PHPCSUtils | ||
* @copyright 2019-2020 PHPCSUtils Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCSStandards/PHPCSUtils | ||
*/ | ||
|
||
namespace PHPCSUtils\Tests\Utils\ObjectDeclarations; | ||
|
||
use PHPCSUtils\Tests\BackCompat\BCFile\GetDeclarationNameParseError1Test as BCFile_GetDeclarationNameParseError1Test; | ||
use PHPCSUtils\Utils\ObjectDeclarations; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\Utils\ObjectDeclarations::getName() method. | ||
* | ||
* @covers \PHPCSUtils\Utils\ObjectDeclarations::getName | ||
* | ||
* @group objectdeclarations | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
final class GetNameParseError1Test extends BCFile_GetDeclarationNameParseError1Test | ||
{ | ||
|
||
/** | ||
* Full path to the test case file associated with this test class. | ||
* | ||
* @var string | ||
*/ | ||
protected static $caseFile = ''; | ||
|
||
/** | ||
* Initialize PHPCS & tokenize the test case file. | ||
* | ||
* Overloaded to re-use the `$caseFile` from the BCFile test. | ||
* | ||
* @beforeClass | ||
* | ||
* @return void | ||
*/ | ||
public static function setUpTestFile() | ||
{ | ||
self::$caseFile = \dirname(\dirname(__DIR__)) . '/BackCompat/BCFile/GetDeclarationNameParseError1Test.inc'; | ||
parent::setUpTestFile(); | ||
} | ||
|
||
/** | ||
* Test receiving "null" in case of a parse error. | ||
* | ||
* @dataProvider dataGetDeclarationNameNull | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param int|string $targetType Token type of the token to get as stackPtr. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetDeclarationNameNull($testMarker, $targetType) | ||
{ | ||
$target = $this->getTargetToken($testMarker, $targetType); | ||
$result = ObjectDeclarations::getName(self::$phpcsFile, $target); | ||
$this->assertNull($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. | ||
* | ||
* @package PHPCSUtils | ||
* @copyright 2019-2020 PHPCSUtils Contributors | ||
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 | ||
* @link https://github.com/PHPCSStandards/PHPCSUtils | ||
*/ | ||
|
||
namespace PHPCSUtils\Tests\Utils\ObjectDeclarations; | ||
|
||
use PHPCSUtils\Tests\BackCompat\BCFile\GetDeclarationNameParseError2Test as BCFile_GetDeclarationNameParseError2Test; | ||
use PHPCSUtils\Utils\ObjectDeclarations; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\Utils\ObjectDeclarations::getName() method. | ||
* | ||
* @covers \PHPCSUtils\Utils\ObjectDeclarations::getName | ||
* | ||
* @group objectdeclarations | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
final class GetNameParseError2Test extends BCFile_GetDeclarationNameParseError2Test | ||
{ | ||
|
||
/** | ||
* Full path to the test case file associated with this test class. | ||
* | ||
* @var string | ||
*/ | ||
protected static $caseFile = ''; | ||
|
||
/** | ||
* Initialize PHPCS & tokenize the test case file. | ||
* | ||
* Overloaded to re-use the `$caseFile` from the BCFile test. | ||
* | ||
* @beforeClass | ||
* | ||
* @return void | ||
*/ | ||
public static function setUpTestFile() | ||
{ | ||
self::$caseFile = \dirname(\dirname(__DIR__)) . '/BackCompat/BCFile/GetDeclarationNameParseError2Test.inc'; | ||
parent::setUpTestFile(); | ||
} | ||
|
||
/** | ||
* Test receiving "null" in case of a parse error. | ||
* | ||
* @dataProvider dataGetDeclarationNameNull | ||
* | ||
* @param string $testMarker The comment which prefaces the target token in the test file. | ||
* @param int|string $targetType Token type of the token to get as stackPtr. | ||
* | ||
* @return void | ||
*/ | ||
public function testGetDeclarationNameNull($testMarker, $targetType) | ||
{ | ||
$target = $this->getTargetToken($testMarker, $targetType); | ||
$result = ObjectDeclarations::getName(self::$phpcsFile, $target); | ||
$this->assertNull($result); | ||
} | ||
} |