Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions PHPCSUtils/Utils/PassedParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ final class PassedParameters
/**
* Checks if any parameters have been passed.
*
* - If passed a `T_STRING`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE`, `T_NAME_QUALIFIED`
* - If passed a `T_STRING`, `T_NAME_FULLY_QUALIFIED`, `T_NAME_RELATIVE`, `T_NAME_QUALIFIED`,
* or `T_VARIABLE` stack pointer, it will treat it as a function call.
* If a `T_STRING` or `T_VARIABLE` which is *not* a function call is passed, the behaviour is
* undetermined.
* - If passed a `T_ANON_CLASS` stack pointer, it will accept it as a class instantiation.
* - If passed a `T_SELF`, `T_STATIC` or `T_PARENT` stack pointer, it will accept it as a
* class instantiation function call when used like `new self()`.
* class instantiation function call when used like `new self()` (with or without parenthesis).
* When these hierarchiecal keywords are not preceded by the `new` keyword, parenthesis
* will be required for the token to be accepted.
* - If passed a `T_ARRAY` or `T_OPEN_SHORT_ARRAY` stack pointer, it will detect
* whether the array has values or is empty.
* For purposes of backward-compatibility with older PHPCS versions, `T_OPEN_SQUARE_BRACKET`
Expand Down Expand Up @@ -89,9 +91,13 @@ public static function hasParameters(File $phpcsFile, $stackPtr, $isShortArray =
);
}

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

$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($next === false) {
return false;
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/Utils/PassedParameters/HasParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ class HierarchyKeywordsAsMethodNames {
/* testHasParamsFunctionCall8 */
$a = $this->parent(true);
}

public function callGlobalFunctionsUsingKeywords() {
/* testHasParamsFunctionCall9 */
$a = self(true);
/* testHasParamsFunctionCall10 */
$a = static(true);
/* testHasParamsFunctionCall11 */
$a = parent(true);
}
}

/* testNoParamsFunctionCallFullyQualified */
Expand Down
29 changes: 24 additions & 5 deletions Tests/Utils/PassedParameters/HasParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public function testNotAShortArray()
*
* @dataProvider dataHasParameters
*
* @param string $testMarker The comment which prefaces the target token in the test file.
* @param int|string $targetType The type of token to look for.
* @param bool $expected Whether or not the function/array has parameters/values.
* @param string $targetContent Optional. The content of the target token to find.
* Defaults to null (ignore content).
* @param string $testMarker The comment which prefaces the target token in the test file.
* @param int|string|array $targetType The type(s) of token to look for.
* @param bool $expected Whether or not the function/array has parameters/values.
* @param string $targetContent Optional. The content of the target token to find.
* Defaults to null (ignore content).
*
* @return void
*/
Expand Down Expand Up @@ -231,6 +231,25 @@ public function dataHasParameters()
'expected' => true,
'targetContent' => 'parent',
],
'has-params-function-call-9-self-as-global-function-name' => [
'testMarker' => '/* testHasParamsFunctionCall9 */',
'targetType' => [\T_STRING, \T_SELF],
'expected' => true,
'targetContent' => 'self',
],
// Parse error in PHP, but not our concern.
'has-params-function-call-10-static-as-global-function-name' => [
'testMarker' => '/* testHasParamsFunctionCall10 */',
'targetType' => [\T_STRING, \T_STATIC],
'expected' => true,
'targetContent' => 'static',
],
'has-params-function-call-11-parent-as-global-function-name' => [
'testMarker' => '/* testHasParamsFunctionCall11 */',
'targetType' => [\T_STRING, \T_PARENT],
'expected' => true,
'targetContent' => 'parent',
],

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