Skip to content

Commit c0b92f7

Browse files
authored
Merge pull request #452 from PHPCSStandards/utils/passedparameters-allow-for-hierachical-name-function-call
PassedParameters::hasParameters(): allow for self/parent/static as function call
2 parents bcefeb2 + 1c47daf commit c0b92f7

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

PHPCSUtils/Utils/PassedParameters.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ final class PassedParameters
4747
/**
4848
* Checks if any parameters have been passed.
4949
*
50-
* - If passed a `T_STRING`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE`, `T_NAME_QUALIFIED`
50+
* - If passed a `T_STRING`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE`, `T_NAME_QUALIFIED`,
5151
* or `T_VARIABLE` stack pointer, it will treat it as a function call.
5252
* If a `T_STRING` or `T_VARIABLE` which is *not* a function call is passed, the behaviour is
5353
* undetermined.
5454
* - If passed a `T_ANON_CLASS` stack pointer, it will accept it as a class instantiation.
5555
* - If passed a `T_SELF`, `T_STATIC` or `T_PARENT` stack pointer, it will accept it as a
56-
* class instantiation function call when used like `new self()`.
56+
* class instantiation function call when used like `new self()` (with or without parenthesis).
57+
* When these hierarchiecal keywords are not preceded by the `new` keyword, parenthesis
58+
* will be required for the token to be accepted.
5759
* - If passed a `T_ARRAY` or `T_OPEN_SHORT_ARRAY` stack pointer, it will detect
5860
* whether the array has values or is empty.
5961
* For purposes of backward-compatibility with older PHPCS versions, `T_OPEN_SQUARE_BRACKET`
@@ -89,9 +91,13 @@ public static function hasParameters(File $phpcsFile, $stackPtr, $isShortArray =
8991
);
9092
}
9193

94+
// Only accept self/static/parent if preceded by `new` or followed by an open parenthesis.
95+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
9296
if (isset(Collections::ooHierarchyKeywords()[$tokens[$stackPtr]['code']]) === true) {
9397
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
94-
if ($tokens[$prev]['code'] !== \T_NEW) {
98+
if ($tokens[$prev]['code'] !== \T_NEW
99+
&& ($next !== false && $tokens[$next]['code'] !== \T_OPEN_PARENTHESIS)
100+
) {
95101
throw new RuntimeException(
96102
'The hasParameters() method expects a function call, array, isset or unset token to be passed.'
97103
);
@@ -107,7 +113,6 @@ public static function hasParameters(File $phpcsFile, $stackPtr, $isShortArray =
107113
);
108114
}
109115

110-
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
111116
if ($next === false) {
112117
return false;
113118
}

Tests/Utils/PassedParameters/HasParametersTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ class HierarchyKeywordsAsMethodNames {
7474
/* testHasParamsFunctionCall8 */
7575
$a = $this->parent(true);
7676
}
77+
78+
public function callGlobalFunctionsUsingKeywords() {
79+
/* testHasParamsFunctionCall9 */
80+
$a = self(true);
81+
/* testHasParamsFunctionCall10 */
82+
$a = static(true);
83+
/* testHasParamsFunctionCall11 */
84+
$a = parent(true);
85+
}
7786
}
7887

7988
/* testNoParamsFunctionCallFullyQualified */

Tests/Utils/PassedParameters/HasParametersTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public function testNotAShortArray()
124124
*
125125
* @dataProvider dataHasParameters
126126
*
127-
* @param string $testMarker The comment which prefaces the target token in the test file.
128-
* @param int|string $targetType The type of token to look for.
129-
* @param bool $expected Whether or not the function/array has parameters/values.
130-
* @param string $targetContent Optional. The content of the target token to find.
131-
* Defaults to null (ignore content).
127+
* @param string $testMarker The comment which prefaces the target token in the test file.
128+
* @param int|string|array $targetType The type(s) of token to look for.
129+
* @param bool $expected Whether or not the function/array has parameters/values.
130+
* @param string $targetContent Optional. The content of the target token to find.
131+
* Defaults to null (ignore content).
132132
*
133133
* @return void
134134
*/
@@ -231,6 +231,25 @@ public function dataHasParameters()
231231
'expected' => true,
232232
'targetContent' => 'parent',
233233
],
234+
'has-params-function-call-9-self-as-global-function-name' => [
235+
'testMarker' => '/* testHasParamsFunctionCall9 */',
236+
'targetType' => [\T_STRING, \T_SELF],
237+
'expected' => true,
238+
'targetContent' => 'self',
239+
],
240+
// Parse error in PHP, but not our concern.
241+
'has-params-function-call-10-static-as-global-function-name' => [
242+
'testMarker' => '/* testHasParamsFunctionCall10 */',
243+
'targetType' => [\T_STRING, \T_STATIC],
244+
'expected' => true,
245+
'targetContent' => 'static',
246+
],
247+
'has-params-function-call-11-parent-as-global-function-name' => [
248+
'testMarker' => '/* testHasParamsFunctionCall11 */',
249+
'targetType' => [\T_STRING, \T_PARENT],
250+
'expected' => true,
251+
'targetContent' => 'parent',
252+
],
234253

235254
'no-params-function-call-fully-qualified' => [
236255
'testMarker' => '/* testNoParamsFunctionCallFullyQualified */',

0 commit comments

Comments
 (0)