You are here

class OptionsTSniff in Coder 8.2

Same name and namespace in other branches
  1. 8.3 coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php \DrupalPractice\Sniffs\General\OptionsTSniff
  2. 8.3.x coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php \DrupalPractice\Sniffs\General\OptionsTSniff

Checks that values in #otions form arrays are translated.

@category PHP @package PHP_CodeSniffer @link http://pear.php.net/package/PHP_CodeSniffer

Hierarchy

  • class \DrupalPractice\Sniffs\General\OptionsTSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of OptionsTSniff

File

coder_sniffer/DrupalPractice/Sniffs/General/OptionsTSniff.php, line 22

Namespace

DrupalPractice\Sniffs\General
View source
class OptionsTSniff implements Sniff {

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

  //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 function
   *                                               name in the stack.
   *
   * @return void
   */
  public function process(File $phpcsFile, $stackPtr) {

    // Look for the string "#options".
    $tokens = $phpcsFile
      ->getTokens();
    if ($tokens[$stackPtr]['content'] !== '"#options"' && $tokens[$stackPtr]['content'] !== "'#options'") {
      return;
    }

    // Look for an opening array pattern that starts to define #options
    // values.
    $statementEnd = $phpcsFile
      ->findNext(T_SEMICOLON, $stackPtr + 1);
    $arrayString = $phpcsFile
      ->getTokensAsString($stackPtr + 1, $statementEnd - $stackPtr);

    // Cut out all the white space.
    $arrayString = preg_replace('/\\s+/', '', $arrayString);
    if (strpos($arrayString, '=>array(') !== 0 && strpos($arrayString, ']=array(') !== 0) {
      return;
    }

    // We only search within the #options array.
    $arrayToken = $phpcsFile
      ->findNext(T_ARRAY, $stackPtr + 1);
    $statementEnd = $tokens[$arrayToken]['parenthesis_closer'];

    // Go through the array by examining stuff after "=>".
    $arrow = $phpcsFile
      ->findNext(T_DOUBLE_ARROW, $stackPtr + 1, $statementEnd, false, null, true);
    while ($arrow !== false) {
      $arrayValue = $phpcsFile
        ->findNext(T_WHITESPACE, $arrow + 1, $statementEnd, true);

      // We are only interested in string literals that are not numbers
      // and more than 3 characters long.
      if ($tokens[$arrayValue]['code'] === T_CONSTANT_ENCAPSED_STRING && is_numeric(substr($tokens[$arrayValue]['content'], 1, -1)) === false && strlen($tokens[$arrayValue]['content']) > 5) {

        // We need to make sure that the string is the one and only part
        // of the array value.
        $afterValue = $phpcsFile
          ->findNext(T_WHITESPACE, $arrayValue + 1, $statementEnd, true);
        if ($tokens[$afterValue]['code'] === T_COMMA || $tokens[$afterValue]['code'] === T_CLOSE_PARENTHESIS) {
          $warning = '#options values usually have to run through t() for translation';
          $phpcsFile
            ->addWarning($warning, $arrayValue, 'TforValue');
        }
      }
      $arrow = $phpcsFile
        ->findNext(T_DOUBLE_ARROW, $arrow + 1, $statementEnd, false, null, true);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OptionsTSniff::process public function Processes this test, when one of its tokens is encountered.
OptionsTSniff::register public function Returns an array of tokens this test wants to listen for.