Skip to content

Commit

Permalink
Squiz.ControlStructures.InlineIfDeclaration is now able to fix the sp…
Browse files Browse the repository at this point in the history
…acing errors it reports
  • Loading branch information
gsherwood committed Feb 20, 2017
1 parent 7a99e69 commit 63f0957
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ rand(0, 1) ? 'ěščřžýáí' : NULL;

$c = ($argv[1]) ? : "";
$filepath = realpath($argv[1]) ?: $argv[1];
$c = ($argv[1]) ? /* comment */ : "";
$c = ($argv[1]) ?
: "";
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function getErrorList()
20 => 1,
24 => 4,
44 => 1,
47 => 1,
);

}//end getErrorList()
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 63f0957

Please sign in to comment.