Skip to content

Commit 73eea42

Browse files
authored
Merge pull request #740 from PHPCSStandards/feature/functiondeclarations-getproperties-more-defensive-coding
FunctionDeclarations::getProperties(): add more defensive coding
2 parents 647249c + d5ac1fb commit 73eea42

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

PHPCSUtils/Utils/FunctionDeclarations.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public static function getName(File $phpcsFile, $stackPtr)
150150
* - `"has_body"` index could be set to `true` for functions without body in the case of
151151
* parse errors or live coding.
152152
* - Defensive coding against incorrect calls to this method.
153+
* - Defensive coding against incorrect results due to parse errors in the code under scan.
153154
* - More efficient checking whether a function has a body.
154155
* - Support for PHP 8.0 identifier name tokens in return types, cross-version PHP & PHPCS.
155156
* - The results of this function call are cached during a PHPCS run for faster response times.
@@ -277,7 +278,10 @@ public static function getProperties(File $phpcsFile, $stackPtr)
277278
break;
278279
}
279280

280-
if ($scopeOpener === null && $tokens[$i]['code'] === \T_SEMICOLON) {
281+
if ($scopeOpener === null
282+
&& ($tokens[$i]['code'] === \T_SEMICOLON
283+
|| $tokens[$i]['code'] === \T_OPEN_CURLY_BRACKET)
284+
) {
281285
// End of abstract/interface function definition.
282286
break;
283287
}

Tests/Utils/FunctionDeclarations/GetPropertiesDiffTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
/* testInvalidTypeParseError */
4+
// This test MUST have code containing name tokens after this code sample.
5+
function InvalidType($param): ?\&\SomeType {}
6+
37
/* testMessyPhpcsAnnotationsMethod */
48
trait FooTrait {
59
/**

Tests/Utils/FunctionDeclarations/GetPropertiesDiffTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ public function testNonExistentToken()
5858
FunctionDeclarations::getProperties(self::$phpcsFile, 10000);
5959
}
6060

61+
/**
62+
* Test that for invalid code, the return type parsing doesn't grab too many tokens.
63+
*
64+
* @return void
65+
*/
66+
public function testInvalidTypeParseError()
67+
{
68+
$php8Names = parent::usesPhp8NameTokens();
69+
70+
// Offsets are relative to the T_FUNCTION token.
71+
$expected = [
72+
'scope' => 'public',
73+
'scope_specified' => false,
74+
'return_type' => ($php8Names === true) ? '?\\\\SomeType' : '?\\&\\SomeType',
75+
'return_type_token' => 9,
76+
'return_type_end_token' => ($php8Names === true) ? 11 : 12,
77+
'nullable_return_type' => true,
78+
'is_abstract' => false,
79+
'is_final' => false,
80+
'is_static' => false,
81+
'has_body' => true,
82+
];
83+
84+
$this->getPropertiesTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
85+
}
86+
6187
/**
6288
* Test handling of the PHPCS 3.2.0+ annotations between the keywords.
6389
*

0 commit comments

Comments
 (0)