diff --git a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php index d9fd1bc0db..945a69c399 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php @@ -92,7 +92,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($spaceBefore !== 1) { $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found'; $data = array($spaceBefore); - $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeThen', $data); + if ($fix === true) { + if ($spaceBefore === 0) { + $phpcsFile->fixer->addContentBefore($stackPtr, ' '); + } else { + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' '); + } + } } // If there is no content between the ? and the : operators, then they are @@ -104,14 +111,22 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $inlineElse = $next; if ($inlineElse !== ($stackPtr + 1)) { $error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE'; - $phpcsFile->addError($error, $stackPtr, 'ElvisSpacing'); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ElvisSpacing'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); + } } } else { $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1)); if ($spaceAfter !== 1) { $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found'; $data = array($spaceAfter); - $phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterThen', $data); + if ($spaceAfter === 0) { + $phpcsFile->fixer->addContent($stackPtr, ' '); + } else { + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); + } } // Make sure the ELSE has the correct spacing. @@ -121,7 +136,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($spaceBefore !== 1) { $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found'; $data = array($spaceBefore); - $phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data); + $fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingBeforeElse', $data); + if ($fix === true) { + if ($spaceBefore === 0) { + $phpcsFile->fixer->addContentBefore($inlineElse, ' '); + } else { + $phpcsFile->fixer->replaceToken(($inlineElse - 1), ' '); + } + } } }//end if @@ -130,7 +152,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($spaceAfter !== 1) { $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found'; $data = array($spaceAfter); - $phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data); + $fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingAfterElse', $data); + if ($spaceAfter === 0) { + $phpcsFile->fixer->addContent($inlineElse, ' '); + } else { + $phpcsFile->fixer->replaceToken(($inlineElse + 1), ' '); + } } }//end process() diff --git a/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.inc b/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.inc index 5000d9687a..f54ed8af04 100644 --- a/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.inc +++ b/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.inc @@ -43,3 +43,6 @@ rand(0, 1) ? 'ěščřžýáí' : NULL; $c = ($argv[1]) ? : ""; $filepath = realpath($argv[1]) ?: $argv[1]; +$c = ($argv[1]) ? /* comment */ : ""; +$c = ($argv[1]) ? +: ""; diff --git a/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.php b/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.php index e41407f29a..4d2af7bd9b 100644 --- a/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.php +++ b/CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.php @@ -68,6 +68,7 @@ public function getErrorList() 20 => 1, 24 => 4, 44 => 1, + 47 => 1, ); }//end getErrorList() diff --git a/package.xml b/package.xml index 6f8fc205ed..f3fe7dad9f 100644 --- a/package.xml +++ b/package.xml @@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- Thanks to Juliette Reinders Folmer for the patch - Squiz.ControlStructures.InlineIfDeclaration now supports the elvis operator -- As this is not a real PHP operator, it enforces no spaces beteen ? and : when the THEN statement is empty + - Squiz.ControlStructures.InlineIfDeclaration is now able to fix the spacing errors it reports - Fixed bug #1340 : STDIN file contents not being populated in some cases -- Thanks to David Biňovec for the patch - Fixed bug #1344 : PEAR.Functions.FunctionCallSignatureSniff throws error for blank comment lines