Permalink
Browse files

Fixed bug #19290 : Generic indent sniffer fails for anonymous functions

  • Loading branch information...
1 parent 5503581 commit 53ec9ec0157c749bf92a566a88d1ffca4fc1e283 @gsherwood gsherwood committed Feb 21, 2012
@@ -235,12 +235,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// This is a special condition for T_DOC_COMMENT and C-style
// comments, which contain whitespace between each line.
- $comments = array(
- T_COMMENT,
- T_DOC_COMMENT
- );
-
- if (in_array($tokens[$firstToken]['code'], $comments) === true) {
+ if (in_array($tokens[$firstToken]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) {
$content = trim($tokens[$firstToken]['content']);
if (preg_match('|^/\*|', $content) !== 0) {
// Check to see if the end of the comment is on the same line
@@ -311,11 +306,16 @@ protected function calculateExpectedIndent(array $tokens, $stackPtr)
{
$conditionStack = array();
+ $inParenthesis = false;
+ if (isset($tokens[$stackPtr]['nested_parenthesis']) === true
+ && empty($tokens[$stackPtr]['nested_parenthesis']) === false
+ ) {
+ $inParenthesis = true;
+ }
+
// Empty conditions array (top level structure).
if (empty($tokens[$stackPtr]['conditions']) === true) {
- if (isset($tokens[$stackPtr]['nested_parenthesis']) === true
- && empty($tokens[$stackPtr]['nested_parenthesis']) === false
- ) {
+ if ($inParenthesis === true) {
// Wrapped in parenthesis means it is probably in a
// function call (like a closure) so we have to assume indent
// is correct here and someone else will check it more
@@ -326,16 +326,29 @@ protected function calculateExpectedIndent(array $tokens, $stackPtr)
}
}
+ $indent = 0;
+
$tokenConditions = $tokens[$stackPtr]['conditions'];
foreach ($tokenConditions as $id => $condition) {
// If it's an indenting scope ie. it's not in our array of
- // scopes that don't indent, add it to our condition stack.
+ // scopes that don't indent, increase indent.
if (in_array($condition, $this->nonIndentingScopes) === false) {
- $conditionStack[$id] = $condition;
+ if ($condition === T_CLOSURE && $inParenthesis === true) {
+ // Closures cause problems with indents when they are
+ // used as function arguments because the code inside them
+ // is not technically inside the function yet, so the indent
+ // is always off by one. So instead, use the
+ // indent of the closure as the base value.
+ $indent = ($tokens[$id]['column'] - 1);
+ }
+
+ $indent += $this->indent;
}
}
- $indent = ((count($conditionStack) * $this->indent) + 1);
+ // Increase by 1 to indiciate that the code should start at a specific column.
+ // E.g., code indented 4 spaces should start at column 5.
+ $indent++;
return $indent;
}//end calculateExpectedIndent()
@@ -372,4 +372,29 @@ class a {
);
}
}
+
+some_function(
+ function() {
+ $a = 403;
+ if ($a === 404) {
+ $a = 403;
+ }
+ }
+);
+
+some_function(
+ function() {
+ $a = 403;
+ if ($a === 404) {
+ $a = 403;
+ }
+ }
+);
+
+$myFunction = function() {
+ $a = 403;
+ if ($a === 404) {
+ $a = 403;
+ }
+}
?>
@@ -64,6 +64,9 @@ public function getErrorList()
311 => 1,
336 => 1,
349 => 1,
+ 380 => 1,
+ 387 => 1,
+ 397 => 1,
);
}//end getErrorList()
View
@@ -35,6 +35,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- In particular, allows the Drupal CS to work without needing to symlink it into the PHPCS install
-- Thanks to Peter Philipp for the patch
- Fixed an issue in AbstractPatternSniff where comments were not being ignored in some cases
+ - Fixed bug #19290 : Generic indent sniffer fails for anonymous functions
</notes>
<contents>
<dir name="/">

0 comments on commit 53ec9ec

Please sign in to comment.