Skip to content

Commit

Permalink
Fix ternary check.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 2, 2024
1 parent 8a751c2 commit 2343186
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion PSR2R/Sniffs/ControlStructures/TernarySpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Asserts single space between ternary operator parts (? and :) and surroundings.
* Also asserts no whitespace between short ternary operator (?:), which was introduced in PHP 5.3.
*
* @see https://github.com/dereuromark/codesniffer-standards/blob/master/MyCakePHP/Sniffs/WhiteSpace/TernarySpacingSniff.php
* @author Mark Scherer
* @license MIT
*/
Expand Down Expand Up @@ -39,13 +38,21 @@ public function process(File $phpcsFile, $stackPtr) {
*/
protected function assertSpaceBefore(File $phpcsFile, int $stackPtr): void {
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if (!$previous) {
return;
}

if ($stackPtr - $previous > 1) {
$this->assertSingleSpaceBeforeIfNotMultiline($phpcsFile, $stackPtr, $previous);

return;
}

$tokens = $phpcsFile->getTokens();
if ($tokens[$previous]['code'] === 'PHPCS_T_INLINE_THEN') {
return;
}

$content = $tokens[$stackPtr]['content'];
$error = 'There must be a single space before ternary operator part `' . $content . '`';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeInlineThen');
Expand Down Expand Up @@ -133,6 +140,10 @@ protected function assertNoSpaceBetween(File $phpcsFile, int $thenIndex, int $el
*/
protected function assertSpaceAfter(File $phpcsFile, int $stackPtr): void {
$nextIndex = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
if (!$nextIndex) {
return;
}

if ($nextIndex - $stackPtr > 1) {
$this->assertSingleSpaceAfterIfNotMultiline($phpcsFile, $stackPtr, $nextIndex);

Expand Down

0 comments on commit 2343186

Please sign in to comment.