Skip to content

Commit 63f0957

Browse files
committed
Squiz.ControlStructures.InlineIfDeclaration is now able to fix the spacing errors it reports
1 parent 7a99e69 commit 63f0957

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9292
if ($spaceBefore !== 1) {
9393
$error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
9494
$data = array($spaceBefore);
95-
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data);
95+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeThen', $data);
96+
if ($fix === true) {
97+
if ($spaceBefore === 0) {
98+
$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
99+
} else {
100+
$phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
101+
}
102+
}
96103
}
97104

98105
// 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)
104111
$inlineElse = $next;
105112
if ($inlineElse !== ($stackPtr + 1)) {
106113
$error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE';
107-
$phpcsFile->addError($error, $stackPtr, 'ElvisSpacing');
114+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ElvisSpacing');
115+
if ($fix === true) {
116+
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
117+
}
108118
}
109119
} else {
110120
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
111121
if ($spaceAfter !== 1) {
112122
$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
113123
$data = array($spaceAfter);
114-
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
124+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterThen', $data);
125+
if ($spaceAfter === 0) {
126+
$phpcsFile->fixer->addContent($stackPtr, ' ');
127+
} else {
128+
$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
129+
}
115130
}
116131

117132
// Make sure the ELSE has the correct spacing.
@@ -121,7 +136,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
121136
if ($spaceBefore !== 1) {
122137
$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
123138
$data = array($spaceBefore);
124-
$phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
139+
$fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingBeforeElse', $data);
140+
if ($fix === true) {
141+
if ($spaceBefore === 0) {
142+
$phpcsFile->fixer->addContentBefore($inlineElse, ' ');
143+
} else {
144+
$phpcsFile->fixer->replaceToken(($inlineElse - 1), ' ');
145+
}
146+
}
125147
}
126148
}//end if
127149

@@ -130,7 +152,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
130152
if ($spaceAfter !== 1) {
131153
$error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
132154
$data = array($spaceAfter);
133-
$phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data);
155+
$fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingAfterElse', $data);
156+
if ($spaceAfter === 0) {
157+
$phpcsFile->fixer->addContent($inlineElse, ' ');
158+
} else {
159+
$phpcsFile->fixer->replaceToken(($inlineElse + 1), ' ');
160+
}
134161
}
135162

136163
}//end process()

CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ rand(0, 1) ? 'ěščřžýáí' : NULL;
4343

4444
$c = ($argv[1]) ? : "";
4545
$filepath = realpath($argv[1]) ?: $argv[1];
46+
$c = ($argv[1]) ? /* comment */ : "";
47+
$c = ($argv[1]) ?
48+
: "";

CodeSniffer/Standards/Squiz/Tests/ControlStructures/InlineIfDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function getErrorList()
6868
20 => 1,
6969
24 => 4,
7070
44 => 1,
71+
47 => 1,
7172
);
7273

7374
}//end getErrorList()

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3333
-- Thanks to Juliette Reinders Folmer for the patch
3434
- Squiz.ControlStructures.InlineIfDeclaration now supports the elvis operator
3535
-- As this is not a real PHP operator, it enforces no spaces beteen ? and : when the THEN statement is empty
36+
- Squiz.ControlStructures.InlineIfDeclaration is now able to fix the spacing errors it reports
3637
- Fixed bug #1340 : STDIN file contents not being populated in some cases
3738
-- Thanks to David Biňovec for the patch
3839
- Fixed bug #1344 : PEAR.Functions.FunctionCallSignatureSniff throws error for blank comment lines

0 commit comments

Comments
 (0)