-
-
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/FunctionDeclarations::get[Method]Properties(): bug fix - skip …
…over closure use statements Sister-PR to upstream PR PHPCSStandards/PHP_CodeSniffer 421 This PR improves performance of the `BCFile::getMethodProperties()` and the `FunctionDeclarations::getProperties()` methods and prevents incorrect return type information for closure `use` clauses containing invalid variable imports in the `use` clause (defensive coding). Closure `use` statements can only import plain variables, not properties or other more complex variables. As things were, when such "illegal" variables were imported in a closure `use`, the information for the return type could get mangled. While this would be a parse error, for the purposes of static analysis, the `BCFile::getMethodProperties()` method and the `FunctionDeclarations::getProperties()` method should still handle this correctly. This commit updates both methods to always skip over the complete `use` clause, which prevents the issue and improves performance as the same time (less token walking). Includes unit tests.
- Loading branch information
Showing
7 changed files
with
405 additions
and
2 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
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/GetMethodPropertiesParseError1Test.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 | ||
|
||
/* testParseError */ | ||
// Intentional parse error. | ||
$incompleteUse = function(int $a, string $b = '') use(&$import, |
54 changes: 54 additions & 0 deletions
54
Tests/BackCompat/BCFile/GetMethodPropertiesParseError1Test.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,54 @@ | ||
<?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\TestUtils\UtilityMethodTestCase; | ||
use PHPCSUtils\Tokens\Collections; | ||
|
||
/** | ||
* Tests for the \PHPCSUtils\BackCompat\BCFile::getMethodProperties method. | ||
* | ||
* @covers \PHPCSUtils\BackCompat\BCFile::getMethodProperties | ||
* | ||
* @group functiondeclarations | ||
* | ||
* @since 1.0.11 | ||
*/ | ||
final class GetMethodPropertiesParseError1Test extends UtilityMethodTestCase | ||
{ | ||
|
||
/** | ||
* Test handling of closure declarations with an incomplete use clause. | ||
* | ||
* @return void | ||
*/ | ||
public function testParseError() | ||
{ | ||
$target = $this->getTargetToken('/* testParseError */', Collections::functionDeclarationTokens()); | ||
$result = BCFile::getMethodProperties(self::$phpcsFile, $target); | ||
|
||
$expected = [ | ||
'scope' => 'public', | ||
'scope_specified' => false, | ||
'return_type' => '', | ||
'return_type_token' => false, | ||
'return_type_end_token' => false, | ||
'nullable_return_type' => false, | ||
'is_abstract' => false, | ||
'is_final' => false, | ||
'is_static' => false, | ||
'has_body' => false, | ||
]; | ||
|
||
$this->assertSame($expected, $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
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
Oops, something went wrong.