Skip to content

Commit 7a99e69

Browse files
committed
Squiz.ControlStructures.InlineIfDeclaration now supports the elvis operator (ref #1311)
1 parent eff63ec commit 7a99e69

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

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

+32-21
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
8686
if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
8787
$error = 'Inline shorthand IF statement requires brackets around comparison';
8888
$phpcsFile->addError($error, $stackPtr, 'NoBrackets');
89-
return;
9089
}
9190

9291
$spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
@@ -96,26 +95,38 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9695
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data);
9796
}
9897

99-
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
100-
if ($spaceAfter !== 1) {
101-
$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
102-
$data = array($spaceAfter);
103-
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
104-
}
105-
106-
// Make sure the ELSE has the correct spacing.
107-
$inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
108-
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
109-
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
110-
111-
$spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
112-
if ($spaceBefore !== 1) {
113-
$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
114-
$data = array($spaceBefore);
115-
$phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
116-
}
117-
118-
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
98+
// If there is no content between the ? and the : operators, then they are
99+
// trying to replicate an elvis operator, even though PHP doesn't have one.
100+
// In this case, we want no spaces between the two operators so ?: looks like
101+
// an operator itself.
102+
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
103+
if ($tokens[$next]['code'] === T_INLINE_ELSE) {
104+
$inlineElse = $next;
105+
if ($inlineElse !== ($stackPtr + 1)) {
106+
$error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE';
107+
$phpcsFile->addError($error, $stackPtr, 'ElvisSpacing');
108+
}
109+
} else {
110+
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
111+
if ($spaceAfter !== 1) {
112+
$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
113+
$data = array($spaceAfter);
114+
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
115+
}
116+
117+
// Make sure the ELSE has the correct spacing.
118+
$inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
119+
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
120+
$spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
121+
if ($spaceBefore !== 1) {
122+
$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
123+
$data = array($spaceBefore);
124+
$phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
125+
}
126+
}//end if
127+
128+
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
129+
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
119130
if ($spaceAfter !== 1) {
120131
$error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
121132
$data = array($spaceAfter);

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

+3
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ $config = function() {
4040
};
4141

4242
rand(0, 1) ? 'ěščřžýáí' : NULL;
43+
44+
$c = ($argv[1]) ? : "";
45+
$filepath = realpath($argv[1]) ?: $argv[1];

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

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function getErrorList()
6767
13 => 1,
6868
20 => 1,
6969
24 => 4,
70+
44 => 1,
7071
);
7172

7273
}//end getErrorList()

package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
3131
- Squiz.Commenting.FunctionComment now properly fixes pipe-seperated param types
3232
- Squiz.Commenting.FunctionComment now works correctly when function return types also contain a comment
3333
-- Thanks to Juliette Reinders Folmer for the patch
34+
- Squiz.ControlStructures.InlineIfDeclaration now supports the elvis operator
35+
-- As this is not a real PHP operator, it enforces no spaces beteen ? and : when the THEN statement is empty
3436
- Fixed bug #1340 : STDIN file contents not being populated in some cases
3537
-- Thanks to David Biňovec for the patch
3638
- Fixed bug #1344 : PEAR.Functions.FunctionCallSignatureSniff throws error for blank comment lines

0 commit comments

Comments
 (0)