@@ -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 }
0 commit comments