From f7711ce7933c64d7ae8a50e050bdf1a94188bdbd Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 11 Jul 2012 11:48:13 +1000 Subject: [PATCH] Fixed duplicate message codes in Generic OpeningFunctionBraceKernighanRitchieSniff and also doing the checks in a different way so that closures can be supported. --- ...ningFunctionBraceKernighanRitchieSniff.php | 40 +++++++++++-------- package.xml | 3 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php b/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php index e634d0ed4e..feac720091 100644 --- a/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php +++ b/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php @@ -77,26 +77,34 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) return; } - // Checks that the closing parenthesis and the opening brace are - // separated by a whitespace character. - $closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column']; - $braceColumn = $tokens[$openingBrace]['column']; - - $columnDifference = ($braceColumn - $closerColumn); + $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; + if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) { + $length = 0; + } else if ($tokens[($closeBracket + 1)]['content'] === "\t") { + $length = '\t'; + } else { + $length = strlen($tokens[($closeBracket + 1)]['content']); + } - if ($columnDifference !== 2) { - $error = 'Expected 1 space between the closing parenthesis and the opening brace; found %s'; - $data = array(($columnDifference - 1)); - $phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data); + if ($length !== 1) { + $error = 'Expected 1 space after closing parenthesis; found %s'; + $data = array($length); + $phpcsFile->addError($error, $openingBrace, 'SpaceAfterBracket', $data); return; } - // Check that a tab was not used instead of a space. - $spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1); - $spaceContent = $tokens[$spaceTokenPtr]['content']; - if ($spaceContent !== ' ') { - $error = 'Expected a single space character between closing parenthesis and opening brace; found %s'; - $data = array($spaceContent); + $closeBrace = $tokens[$stackPtr]['scope_opener']; + if ($tokens[($closeBrace - 1)]['code'] !== T_WHITESPACE) { + $length = 0; + } else if ($tokens[($closeBrace - 1)]['content'] === "\t") { + $length = '\t'; + } else { + $length = strlen($tokens[($closeBrace - 1)]['content']); + } + + if ($length !== 1) { + $error = 'Expected 1 space before opening brace; found %s'; + $data = array($length); $phpcsFile->addError($error, $openingBrace, 'SpaceBeforeBrace', $data); return; } diff --git a/package.xml b/package.xml index 6f51d0b712..3b53777304 100644 --- a/package.xml +++ b/package.xml @@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Exclude rules written for Unix systems will now work correctly on Windows -- Thanks to Walter Tamboer for the patch - The PHP tokenizer now recognises T_RETURN as a valid closer for T_CASE and T_DEFAULT inside switch statements + - Fixed duplicate message codes in Generic OpeningFunctionBraceKernighanRitchieSniff - Fixed bug #18651 : PHPunit Test cases for custom standards are not working on Windows - Fixed bug #19416 : Shorthand arrays cause bracket spacing errors - Fixed bug #19421 : phpcs doesn't recognize ${x} as equivalent to $x @@ -50,7 +51,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed bug #19471 : phpcs on Windows, when using Zend standard, doesn't catch problems -- Thanks to Ivan Habunek for the patch - Fixed bug #19478 : Incorrect indent detection in PEAR standard - -- Thanks to Shane Auckland + -- Thanks to Shane Auckland for the patch - Fixed bug #19483 : Blame Reports fail with space in directory name