/** * Processes single-line calls. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * @param int $openBracket The position of the opening bracket * in the stack passed in $tokens. * @param array $tokens The stack of tokens that make up * the file. * * @return void */ public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens) { // If the first argument is on a new line, this is a multi-line // function call, even if there is only one argument. $next = $phpcsFile->findNext(Tokens::$emptyTokens, $openBracket + 1, null, true); if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { return true; } $closeBracket = $tokens[$openBracket]['parenthesis_closer']; $end = $phpcsFile->findEndOfStatement($openBracket + 1); while ($tokens[$end]['code'] === T_COMMA) { // If the next bit of code is not on the same line, this is a // multi-line function call. $next = $phpcsFile->findNext(Tokens::$emptyTokens, $end + 1, $closeBracket, true); if ($next === false) { return false; } if ($tokens[$next]['line'] !== $tokens[$end]['line']) { return true; } $end = $phpcsFile->findEndOfStatement($next); } // We've reached the last argument, so see if the next content // (should be the close bracket) is also on the same line. $next = $phpcsFile->findNext(Tokens::$emptyTokens, $end + 1, $closeBracket, true); if ($next !== false && $tokens[$next]['line'] !== $tokens[$end]['line']) { return true; } return false; }
/** * Processes multi-line calls. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * @param int $openBracket The position of the opening bracket * in the stack passed in $tokens. * @param array $tokens The stack of tokens that make up * the file. * * @return void */ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $tokens) { // We need to work out how far indented the function // call itself is, so we can work out how far to // indent the arguments. $start = $phpcsFile->findStartOfStatement($stackPtr); foreach (array('stackPtr', 'start') as $checkToken) { $x = ${$checkToken}; for ($i = $x - 1; $i >= 0; $i--) { if ($tokens[$i]['line'] !== $tokens[$x]['line']) { $i++; break; } } if ($i <= 0) { $functionIndent = 0; } else { if ($tokens[$i]['code'] === T_WHITESPACE) { $functionIndent = strlen($tokens[$i]['content']); } else { if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) { $functionIndent = 0; } else { $trimmed = ltrim($tokens[$i]['content']); if ($trimmed === '') { if ($tokens[$i]['code'] === T_INLINE_HTML) { $functionIndent = strlen($tokens[$i]['content']); } else { $functionIndent = $tokens[$i]['column'] - 1; } } else { $functionIndent = strlen($tokens[$i]['content']) - strlen($trimmed); } } } } $varName = $checkToken . 'Indent'; ${$varName} = $functionIndent; } //end foreach $functionIndent = max($startIndent, $stackPtrIndent); $next = $phpcsFile->findNext(Tokens::$emptyTokens, $openBracket + 1, null, true); if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) { $error = 'Opening parenthesis of a multi-line function call must be the last content on the line'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpenBracket'); if ($fix === true) { $phpcsFile->fixer->addContent($openBracket, $phpcsFile->eolChar . str_repeat(' ', $functionIndent + $this->indent)); } } $closeBracket = $tokens[$openBracket]['parenthesis_closer']; $prev = $phpcsFile->findPrevious(T_WHITESPACE, $closeBracket - 1, null, true); if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) { $error = 'Closing parenthesis of a multi-line function call must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $closeBracket, 'CloseBracketLine'); if ($fix === true) { $phpcsFile->fixer->addContentBefore($closeBracket, $phpcsFile->eolChar . str_repeat(' ', $functionIndent + $this->indent)); } } // Each line between the parenthesis should be indented n spaces. $lastLine = $tokens[$openBracket]['line'] - 1; $argStart = null; $argEnd = null; $inArg = false; // Start processing at the first argument. $i = $phpcsFile->findNext(T_WHITESPACE, $openBracket + 1, null, true); if ($tokens[$i - 1]['code'] === T_WHITESPACE && $tokens[$i - 1]['line'] === $tokens[$i]['line']) { // Make sure we check the indent. $i--; } for ($i; $i < $closeBracket; $i++) { if ($i > $argStart && $i < $argEnd) { $inArg = true; } else { $inArg = false; } if ($tokens[$i]['line'] !== $lastLine) { $lastLine = $tokens[$i]['line']; // Ignore heredoc indentation. if (isset(Tokens::$heredocTokens[$tokens[$i]['code']]) === true) { continue; } // Ignore multi-line string indentation. if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true && $tokens[$i]['code'] === $tokens[$i - 1]['code']) { continue; } // Ignore inline HTML. if ($tokens[$i]['code'] === T_INLINE_HTML) { continue; } if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) { // We changed lines, so this should be a whitespace indent token, but first make // sure it isn't a blank line because we don't need to check indent unless there // is actually some code to indent. if ($tokens[$i]['code'] === T_WHITESPACE) { $nextCode = $phpcsFile->findNext(T_WHITESPACE, $i + 1, $closeBracket + 1, true); if ($tokens[$nextCode]['line'] !== $lastLine) { if ($inArg === false) { $error = 'Empty lines are not allowed in multi-line function calls'; $fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine'); if ($fix === true) { $phpcsFile->fixer->replaceToken($i, ''); } } continue; } } else { $nextCode = $i; } if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) { // Closing brace needs to be indented to the same level // as the function call. $inArg = false; $expectedIndent = $functionIndent; } else { $expectedIndent = $functionIndent + $this->indent; } if ($tokens[$i]['code'] !== T_WHITESPACE && $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE) { // Just check if it is a multi-line block comment. If so, we can // calculate the indent from the whitespace before the content. if ($tokens[$i]['code'] === T_COMMENT && $tokens[$i - 1]['code'] === T_COMMENT) { $trimmed = ltrim($tokens[$i]['content']); $foundIndent = strlen($tokens[$i]['content']) - strlen($trimmed); } else { $foundIndent = 0; } } else { $foundIndent = strlen($tokens[$i]['content']); } if ($foundIndent < $expectedIndent || $inArg === false && $expectedIndent !== $foundIndent) { $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s'; $data = array($expectedIndent, $foundIndent); $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); if ($fix === true) { $padding = str_repeat(' ', $expectedIndent); if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($i, $padding); } else { if ($tokens[$i]['code'] === T_COMMENT) { $comment = $padding . ltrim($tokens[$i]['content']); $phpcsFile->fixer->replaceToken($i, $comment); } else { $phpcsFile->fixer->replaceToken($i, $padding); } } } } //end if } else { $nextCode = $i; } //end if if ($inArg === false) { $argStart = $nextCode; $argEnd = $phpcsFile->findEndOfStatement($nextCode); } } //end if // If we are within an argument we should be ignoring commas // as these are not signaling the end of an argument. if ($inArg === false && $tokens[$i]['code'] === T_COMMA) { $next = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), $i + 1, $closeBracket, true); if ($next === false) { continue; } if ($this->allowMultipleArguments === false) { // Comma has to be the last token on the line. if ($tokens[$i]['line'] === $tokens[$next]['line']) { $error = 'Only one argument is allowed per line in a multi-line function call'; $fix = $phpcsFile->addFixableError($error, $next, 'MultipleArguments'); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); for ($x = $next - 1; $x > $i; $x--) { if ($tokens[$x]['code'] !== T_WHITESPACE) { break; } $phpcsFile->fixer->replaceToken($x, ''); } $phpcsFile->fixer->addContentBefore($next, $phpcsFile->eolChar . str_repeat(' ', $functionIndent + $this->indent)); $phpcsFile->fixer->endChangeset(); } } } //end if $argStart = $next; $argEnd = $phpcsFile->findEndOfStatement($next); } //end if } //end for }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. * * @return void */ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // We can't process SWITCH statements unless we know where they start and end. if (isset($tokens[$stackPtr]['scope_opener']) === false || isset($tokens[$stackPtr]['scope_closer']) === false) { return; } $switch = $tokens[$stackPtr]; $nextCase = $stackPtr; $caseAlignment = $switch['column'] + $this->indent; $caseCount = 0; $foundDefault = false; while (($nextCase = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_SWITCH), $nextCase + 1, $switch['scope_closer'])) !== false) { // Skip nested SWITCH statements; they are handled on their own. if ($tokens[$nextCase]['code'] === T_SWITCH) { $nextCase = $tokens[$nextCase]['scope_closer']; continue; } if ($tokens[$nextCase]['code'] === T_DEFAULT) { $type = 'Default'; $foundDefault = true; } else { $type = 'Case'; $caseCount++; } if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) { $expected = strtolower($tokens[$nextCase]['content']); $error = strtoupper($type) . ' keyword must be lowercase; expected "%s" but found "%s"'; $data = array($expected, $tokens[$nextCase]['content']); $fix = $phpcsFile->addFixableError($error, $nextCase, $type . 'NotLower', $data); if ($fix === true) { $phpcsFile->fixer->replaceToken($nextCase, $expected); } } if ($tokens[$nextCase]['column'] !== $caseAlignment) { $error = strtoupper($type) . ' keyword must be indented ' . $this->indent . ' spaces from SWITCH keyword'; $fix = $phpcsFile->addFixableError($error, $nextCase, $type . 'Indent'); if ($fix === true) { $padding = str_repeat(' ', $caseAlignment - 1); if ($tokens[$nextCase]['column'] === 1 || $tokens[$nextCase - 1]['code'] !== T_WHITESPACE) { $phpcsFile->fixer->addContentBefore($nextCase, $padding); } else { $phpcsFile->fixer->replaceToken($nextCase - 1, $padding); } } } if ($type === 'Case' && ($tokens[$nextCase + 1]['type'] !== 'T_WHITESPACE' || $tokens[$nextCase + 1]['content'] !== ' ')) { $error = 'CASE keyword must be followed by a single space'; $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase'); if ($fix === true) { if ($tokens[$nextCase + 1]['type'] !== 'T_WHITESPACE') { $phpcsFile->fixer->addContent($nextCase, ' '); } else { $phpcsFile->fixer->replaceToken($nextCase + 1, ' '); } } } if (isset($tokens[$nextCase]['scope_opener']) === false) { $error = 'Possible parse error: CASE missing opening colon'; $phpcsFile->addWarning($error, $nextCase, 'MissingColon'); continue; } $opener = $tokens[$nextCase]['scope_opener']; if ($tokens[$opener - 1]['type'] === 'T_WHITESPACE') { $error = 'There must be no space before the colon in a ' . strtoupper($type) . ' statement'; $fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon' . $type); if ($fix === true) { $phpcsFile->fixer->replaceToken($opener - 1, ''); } } $nextBreak = $tokens[$nextCase]['scope_closer']; if ($tokens[$nextBreak]['code'] === T_BREAK || $tokens[$nextBreak]['code'] === T_RETURN || $tokens[$nextBreak]['code'] === T_CONTINUE || $tokens[$nextBreak]['code'] === T_THROW || $tokens[$nextBreak]['code'] === T_EXIT) { if ($tokens[$nextBreak]['scope_condition'] === $nextCase) { // Only need to check a couple of things once, even if the // break is shared between multiple case statements, or even // the default case. if ($tokens[$nextBreak]['column'] !== $caseAlignment) { $error = 'Case breaking statement must be indented ' . $this->indent . ' spaces from SWITCH keyword'; $fix = $phpcsFile->addFixableError($error, $nextBreak, 'BreakIndent'); if ($fix === true) { $padding = str_repeat(' ', $caseAlignment - 1); if ($tokens[$nextBreak]['column'] === 1 || $tokens[$nextBreak - 1]['code'] !== T_WHITESPACE) { $phpcsFile->fixer->addContentBefore($nextBreak, $padding); } else { $phpcsFile->fixer->replaceToken($nextBreak - 1, $padding); } } } $prev = $phpcsFile->findPrevious(T_WHITESPACE, $nextBreak - 1, $stackPtr, true); if ($tokens[$prev]['line'] !== $tokens[$nextBreak]['line'] - 1) { $error = 'Blank lines are not allowed before case breaking statements'; $phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak'); } $nextLine = $tokens[$tokens[$stackPtr]['scope_closer']]['line']; $semicolon = $phpcsFile->findEndOfStatement($nextBreak); for ($i = $semicolon + 1; $i < $tokens[$stackPtr]['scope_closer']; $i++) { if ($tokens[$i]['type'] !== 'T_WHITESPACE') { $nextLine = $tokens[$i]['line']; break; } } if ($type === 'Case') { // Ensure the BREAK statement is followed by // a single blank line, or the end switch brace. if ($nextLine !== $tokens[$semicolon]['line'] + 2 && $i !== $tokens[$stackPtr]['scope_closer']) { $error = 'Case breaking statements must be followed by a single blank line'; $fix = $phpcsFile->addFixableError($error, $nextBreak, 'SpacingAfterBreak'); if ($fix === true) { $phpcsFile->fixer->beginChangeset(); for ($i = $semicolon + 1; $i <= $tokens[$stackPtr]['scope_closer']; $i++) { if ($tokens[$i]['line'] === $nextLine) { $phpcsFile->fixer->addNewlineBefore($i); break; } if ($tokens[$i]['line'] === $tokens[$semicolon]['line']) { continue; } $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->endChangeset(); } } //end if } else { // Ensure the BREAK statement is not followed by a blank line. if ($nextLine !== $tokens[$semicolon]['line'] + 1) { $error = 'Blank lines are not allowed after the DEFAULT case\'s breaking statement'; $phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak'); } } //end if $caseLine = $tokens[$nextCase]['line']; $nextLine = $tokens[$nextBreak]['line']; for ($i = $opener + 1; $i < $nextBreak; $i++) { if ($tokens[$i]['type'] !== 'T_WHITESPACE') { $nextLine = $tokens[$i]['line']; break; } } if ($nextLine !== $caseLine + 1) { $error = 'Blank lines are not allowed after ' . strtoupper($type) . ' statements'; $phpcsFile->addError($error, $nextCase, 'SpacingAfter' . $type); } } //end if if ($tokens[$nextBreak]['code'] === T_BREAK) { if ($type === 'Case') { // Ensure empty CASE statements are not allowed. // They must have some code content in them. A comment is not enough. // But count RETURN statements as valid content if they also // happen to close the CASE statement. $foundContent = false; for ($i = $tokens[$nextCase]['scope_opener'] + 1; $i < $nextBreak; $i++) { if ($tokens[$i]['code'] === T_CASE) { $i = $tokens[$i]['scope_opener']; continue; } if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false) { $foundContent = true; break; } } if ($foundContent === false) { $error = 'Empty CASE statements are not allowed'; $phpcsFile->addError($error, $nextCase, 'EmptyCase'); } } else { // Ensure empty DEFAULT statements are not allowed. // They must (at least) have a comment describing why // the default case is being ignored. $foundContent = false; for ($i = $tokens[$nextCase]['scope_opener'] + 1; $i < $nextBreak; $i++) { if ($tokens[$i]['type'] !== 'T_WHITESPACE') { $foundContent = true; break; } } if ($foundContent === false) { $error = 'Comment required for empty DEFAULT case'; $phpcsFile->addError($error, $nextCase, 'EmptyDefault'); } } //end if } //end if } else { if ($type === 'Default') { $error = 'DEFAULT case must have a breaking statement'; $phpcsFile->addError($error, $nextCase, 'DefaultNoBreak'); } } //end if } //end while if ($foundDefault === false) { $error = 'All SWITCH statements must contain a DEFAULT case'; $phpcsFile->addError($error, $stackPtr, 'MissingDefault'); } if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) { $error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword'; $phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign'); } if ($caseCount === 0) { $error = 'SWITCH statements must contain at least one CASE statement'; $phpcsFile->addError($error, $stackPtr, 'MissingCase'); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in * the stack passed in $tokens. * * @return void */ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); // If this token is preceded with an "or", it only relates to one line // and should be ignored. For example: fopen() or die(). $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true); if ($tokens[$prev]['code'] === T_LOGICAL_OR || $tokens[$prev]['code'] === T_BOOLEAN_OR) { return; } // Check if this token is actually part of a one-line IF or ELSE statement. for ($i = $stackPtr - 1; $i > 0; $i--) { if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) { $i = $tokens[$i]['parenthesis_opener']; continue; } else { if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { continue; } } break; } if ($tokens[$i]['code'] === T_IF || $tokens[$i]['code'] === T_ELSE || $tokens[$i]['code'] === T_ELSEIF) { return; } if ($tokens[$stackPtr]['code'] === T_RETURN) { $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); if ($tokens[$next]['code'] === T_SEMICOLON) { $next = $phpcsFile->findNext(T_WHITESPACE, $next + 1, null, true); if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) { // If this is the closing brace of a function // then this return statement doesn't return anything // and is not required anyway. $owner = $tokens[$next]['scope_condition']; if ($tokens[$owner]['code'] === T_FUNCTION) { $warning = 'Empty return statement not required here'; $phpcsFile->addWarning($warning, $stackPtr, 'ReturnNotRequired'); return; } } } } if (isset($tokens[$stackPtr]['scope_opener']) === true) { $owner = $tokens[$stackPtr]['scope_condition']; if ($tokens[$owner]['code'] === T_CASE || $tokens[$owner]['code'] === T_DEFAULT) { // This token closes the scope of a CASE or DEFAULT statement // so any code between this statement and the next CASE, DEFAULT or // end of SWITCH token will not be executable. $end = $phpcsFile->findEndOfStatement($stackPtr); $next = $phpcsFile->findNext(array(T_CASE, T_DEFAULT, T_CLOSE_CURLY_BRACKET), $end + 1); if ($next !== false) { $lastLine = $tokens[$end]['line']; for ($i = $stackPtr + 1; $i < $next; $i++) { if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { continue; } $line = $tokens[$i]['line']; if ($line > $lastLine) { $type = substr($tokens[$stackPtr]['type'], 2); $warning = 'Code after %s statement cannot be executed'; $data = array($type); $phpcsFile->addWarning($warning, $i, 'Unreachable', $data); $lastLine = $line; } } } //end if // That's all we have to check for these types of statements. return; } //end if } //end if // This token may be part of an inline condition. // If we find a closing parenthesis that belongs to a condition // we should ignore this token. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true); if (isset($tokens[$prev]['parenthesis_owner']) === true) { $owner = $tokens[$prev]['parenthesis_owner']; $ignore = array(T_IF => true, T_ELSE => true, T_ELSEIF => true); if (isset($ignore[$tokens[$owner]['code']]) === true) { return; } } $ourConditions = array_keys($tokens[$stackPtr]['conditions']); if (empty($ourConditions) === false) { $condition = array_pop($ourConditions); if (isset($tokens[$condition]['scope_closer']) === false) { return; } // Special case for BREAK statements sitting directly inside SWITCH // statements. If we get to this point, we know the BREAK is not being // used to close a CASE statement, so it is most likely non-executable // code itself (as is the case when you put return; break; at the end of // a case). So we need to ignore this token. if ($tokens[$condition]['code'] === T_SWITCH && $tokens[$stackPtr]['code'] === T_BREAK) { return; } $closer = $tokens[$condition]['scope_closer']; // If the closer for our condition is shared with other openers, // we will need to throw errors from this token to the next // shared opener (if there is one), not to the scope closer. $nextOpener = null; for ($i = $stackPtr + 1; $i < $closer; $i++) { if (isset($tokens[$i]['scope_closer']) === true) { if ($tokens[$i]['scope_closer'] === $closer) { // We found an opener that shares the same // closing token as us. $nextOpener = $i; break; } } } //end for if ($nextOpener === null) { $end = $closer; } else { $end = $nextOpener - 1; } } else { // This token is in the global scope. if ($tokens[$stackPtr]['code'] === T_BREAK) { return; } // Throw an error for all lines until the end of the file. $end = $phpcsFile->numTokens - 1; } //end if // Find the semicolon that ends this statement, skipping // nested statements like FOR loops and closures. for ($start = $stackPtr + 1; $start < $phpcsFile->numTokens; $start++) { if ($start === $end) { break; } if ($tokens[$start]['code'] === T_OPEN_PARENTHESIS) { $start = $tokens[$start]['parenthesis_closer']; continue; } if ($tokens[$start]['code'] === T_OPEN_CURLY_BRACKET) { $start = $tokens[$start]['bracket_closer']; continue; } if ($tokens[$start]['code'] === T_SEMICOLON) { break; } } //end for $lastLine = $tokens[$start]['line']; for ($i = $start + 1; $i < $end; $i++) { if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true || isset(Tokens::$bracketTokens[$tokens[$i]['code']]) === true) { continue; } // Skip whole functions and classes/interfaces because they are not // technically executed code, but rather declarations that may be used. if ($tokens[$i]['code'] === T_FUNCTION || $tokens[$i]['code'] === T_CLASS || $tokens[$i]['code'] === T_INTERFACE) { $i = $tokens[$i]['scope_closer']; continue; } $line = $tokens[$i]['line']; if ($line > $lastLine) { $type = substr($tokens[$stackPtr]['type'], 2); $warning = 'Code after %s statement cannot be executed'; $data = array($type); $phpcsFile->addWarning($warning, $i, 'Unreachable', $data); $lastLine = $line; } } //end for }