Chris@0: getTokens(); Chris@0: Chris@0: $commaOrColon = $phpcsFile->findNext([T_SEMICOLON, T_COLON, T_COMMA], ($stackPtr + 1)); Chris@0: if ($commaOrColon === false) { Chris@0: // Syntax error, nothing we can do. Chris@0: return; Chris@0: } Chris@0: Chris@0: // Search for an opening parenthesis in the current statement until the Chris@0: // next semicolon or comma. Chris@0: $nextParenthesis = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($stackPtr + 1), $commaOrColon); Chris@0: if ($nextParenthesis === false) { Chris@0: $error = 'Calling class constructors must always include parentheses'; Chris@17: $constructor = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); Chris@0: // We can invoke the fixer if we know this is a static constructor Chris@0: // function call or constructor calls with namespaces, example Chris@0: // "new \DOMDocument;" or constructor with class names in variables Chris@0: // "new $controller;". Chris@0: if ($tokens[$constructor]['code'] === T_STRING Chris@0: || $tokens[$constructor]['code'] === T_NS_SEPARATOR Chris@0: || ($tokens[$constructor]['code'] === T_VARIABLE Chris@0: && $tokens[($constructor + 1)]['code'] === T_SEMICOLON) Chris@0: ) { Chris@0: // Scan to the end of possible string\namespace parts. Chris@0: $nextConstructorPart = $constructor; Chris@0: while (true) { Chris@0: $nextConstructorPart = $phpcsFile->findNext( Chris@17: Tokens::$emptyTokens, Chris@0: ($nextConstructorPart + 1), Chris@0: null, Chris@0: true, Chris@0: null, Chris@0: true Chris@0: ); Chris@0: if ($nextConstructorPart === false Chris@0: || ($tokens[$nextConstructorPart]['code'] !== T_STRING Chris@0: && $tokens[$nextConstructorPart]['code'] !== T_NS_SEPARATOR) Chris@0: ) { Chris@0: break; Chris@0: } Chris@0: Chris@0: $constructor = $nextConstructorPart; Chris@0: } Chris@0: Chris@0: $fix = $phpcsFile->addFixableError($error, $constructor, 'ParenthesisMissing'); Chris@0: if ($fix === true) { Chris@0: $phpcsFile->fixer->addContent($constructor, '()'); Chris@0: } Chris@0: Chris@0: // We can invoke the fixer if we know this is a Chris@0: // constructor call with class names in an array Chris@0: // example "new $controller[$i];". Chris@0: } else if ($tokens[$constructor]['code'] === T_VARIABLE Chris@0: && $tokens[($constructor + 1)]['code'] === T_OPEN_SQUARE_BRACKET Chris@0: ) { Chris@0: // Scan to the end of possible multilevel arrays. Chris@0: $nextConstructorPart = $constructor; Chris@0: do { Chris@0: $nextConstructorPart = $tokens[($nextConstructorPart + 1)]['bracket_closer']; Chris@0: } while ($tokens[($nextConstructorPart + 1)]['code'] === T_OPEN_SQUARE_BRACKET); Chris@0: Chris@0: $fix = $phpcsFile->addFixableError($error, $nextConstructorPart, 'ParenthesisMissing'); Chris@0: if ($fix === true) { Chris@0: $phpcsFile->fixer->addContent($nextConstructorPart, '()'); Chris@0: } Chris@0: } else { Chris@0: $phpcsFile->addError($error, $stackPtr, 'ParenthesisMissing'); Chris@0: }//end if Chris@0: }//end if Chris@0: Chris@0: }//end process() Chris@0: Chris@0: Chris@0: }//end class