You are here

SpaceInlineIfSniff.php in Coder 8.2

File

coder_sniffer/Drupal/Sniffs/Formatting/SpaceInlineIfSniff.php
View source
<?php

/**
 * \Drupal\Sniffs\Formatting\SpaceInlineIfSniff.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
namespace Drupal\Sniffs\Formatting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
 * Checks that there is no space between "?" and ":" inline if/else statements.
 *
 * @category PHP
 * @package  PHP_CodeSniffer
 * @link     http://pear.php.net/package/PHP_CodeSniffer
 */
class SpaceInlineIfSniff implements Sniff {

  /**
   * Returns an array of tokens this test wants to listen for.
   *
   * @return array
   */
  public function register() {
    return array(
      T_INLINE_ELSE,
    );
  }

  //end register()

  /**
   * Processes this test, when one of its tokens is encountered.
   *
   * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
   * @param int                         $stackPtr  The position of the current token in
   *                                               the stack passed in $tokens.
   *
   * @return void
   */
  public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile
      ->getTokens();

    // Handle the short ternary operator (?:) introduced in PHP 5.3.
    $previous = $phpcsFile
      ->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
    if ($tokens[$previous]['code'] === T_INLINE_THEN) {
      if ($previous !== $stackPtr - 1) {
        $error = 'There must be no space between ? and :';
        $phpcsFile
          ->addError($error, $stackPtr, 'SpaceInlineElse');
      }
    }

    //end if
  }

}

//end class

Classes

Namesort descending Description
SpaceInlineIfSniff Checks that there is no space between "?" and ":" inline if/else statements.